aboutsummaryrefslogtreecommitdiffstats
path: root/emulators
diff options
context:
space:
mode:
authorMahdi Mokhtari <mmokhi@FreeBSD.org>2017-07-19 00:39:27 +0800
committerMahdi Mokhtari <mmokhi@FreeBSD.org>2017-07-19 00:39:27 +0800
commit7f06d5b68d3929faa7a2a94f12585a9f4a45f159 (patch)
tree1576fa79c6b24aafd93166d1b1e63e6f732360a4 /emulators
parent5c3f44f395ff17f6fae19889bd67df704df295af (diff)
downloadfreebsd-ports-gnome-7f06d5b68d3929faa7a2a94f12585a9f4a45f159.tar.gz
freebsd-ports-gnome-7f06d5b68d3929faa7a2a94f12585a9f4a45f159.tar.zst
freebsd-ports-gnome-7f06d5b68d3929faa7a2a94f12585a9f4a45f159.zip
emulators/virtualbox-ose-additions: Add support for VirtualBox SharedFolder
This is an experimental implementation of VirtualBox SharedFolder subsystem support for FreeBSD. The implementation is based on github.com/lwhsu/freebsd-vboxfs. It's tested and worked as expected. The locking mechanism may need enhancements. Reviewed by: jkim (vbox), mat (mentor) Approved by: jkim (vbox), mat (mentor) Sponsored by: Netzkommune GmbH Differential Revision: https://reviews.freebsd.org/D11602
Diffstat (limited to 'emulators')
-rw-r--r--emulators/virtualbox-ose-additions/Makefile27
-rw-r--r--emulators/virtualbox-ose-additions/pkg-plist2
-rw-r--r--emulators/virtualbox-ose/files/patch-src-VBox-Runtime-r0drv-freebsd-memobj-r0drv-freebsd.c121
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile38
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile.kmk15
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_Makefile.kmk27
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.856
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.c171
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_Makefile.kmk49
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs.h470
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__prov.c1024
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vfsops.c664
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vnops.c1510
-rw-r--r--emulators/virtualbox-ose/files/patch-src_VBox_Runtime_r0drv_freebsd_memobj-r0drv-freebsd.c162
14 files changed, 4194 insertions, 142 deletions
diff --git a/emulators/virtualbox-ose-additions/Makefile b/emulators/virtualbox-ose-additions/Makefile
index 160214394f7f..0ca7e7549af6 100644
--- a/emulators/virtualbox-ose-additions/Makefile
+++ b/emulators/virtualbox-ose-additions/Makefile
@@ -3,7 +3,7 @@
PORTNAME= virtualbox-ose
PORTVERSION= 5.1.22
-PORTREVISION= 1
+PORTREVISION= 2
CATEGORIES= emulators
MASTER_SITES= http://download.virtualbox.org/virtualbox/${PORTVERSION}/
PKGNAMESUFFIX?= -additions
@@ -70,7 +70,8 @@ INPUTDIR= ${PREFIX}/lib/xorg/modules/input
VBOX_BIN= ${WRKSRC}/out/${KMK_ARCH}/${KMK_BUILDTYPE}/bin/additions
VBOX_SBINS= VBoxControl \
- VBoxService
+ VBoxService \
+ mount_vboxvfs
BUILD_WRKSRC= ${VBOX_BIN}/src
@@ -125,9 +126,9 @@ EXTRA_PATCHES+= ${PATCHDIR}/extrapatch-src-VBox-Additions-x11-VBoxClient-Makefil
.endif
.endif
-pre-everything::
.if ${ARCH} == "amd64"
.if !exists(/usr/lib32/libc.so)
+pre-everything::
@${ECHO} 'Requires 32-bit libraries installed under /usr/lib32.'
@${ECHO} 'Do: cd /usr/src; make build32 install32; service ldconfig restart'
@${FALSE}
@@ -177,47 +178,41 @@ do-install:
${MKDIR} ${STAGEDIR}${KMODDIR}
${INSTALL_KLD} ${VBOX_BIN}/src/vboxguest/vboxguest.ko \
${STAGEDIR}${KMODDIR}
-.if ${PORT_OPTIONS:MDEBUG}
- ${INSTALL_KLD} ${VBOX_BIN}/src/vboxguest/vboxguest.ko.symbols \
- ${STAGEDIR}${KMODDIR}
-.endif
-
+ ${INSTALL_KLD} ${VBOX_BIN}/vboxvfs.ko ${STAGEDIR}${KMODDIR}
${INSTALL_LIB} ${VBOX_BIN}/pam_vbox.so ${STAGEDIR}${PREFIX}/lib
${INSTALL_PROGRAM} ${VBOX_SBINS:S|^|${VBOX_BIN}/|} \
${STAGEDIR}${PREFIX}/sbin/
-.if ${PORT_OPTIONS:MX11}
+do-install-DEBUG-on:
+ ${INSTALL_KLD} ${VBOX_BIN}/src/vboxguest/vboxguest.ko.symbols \
+ ${STAGEDIR}${KMODDIR}
+
+do-install-X11-on:
${INSTALL_PROGRAM} ${VBOX_BIN}/VBoxClient ${STAGEDIR}${PREFIX}/bin/
${INSTALL_SCRIPT} \
${WRKSRC}/src/VBox/Additions/x11/Installer/98vboxadd-xclient \
${STAGEDIR}${PREFIX}/bin/VBoxClient-all
-
# X11 autostart
${MKDIR} ${STAGEDIR}${PREFIX}/etc/xdg/autostart/
${INSTALL_DATA} \
${WRKSRC}/src/VBox/Additions/x11/Installer/vboxclient.desktop \
${STAGEDIR}${PREFIX}/etc/xdg/autostart/
-
# KDE autostart
${MKDIR} ${STAGEDIR}${PREFIX}/share/autostart/
${INSTALL_DATA} \
${WRKSRC}/src/VBox/Additions/x11/Installer/vboxclient.desktop \
${STAGEDIR}${PREFIX}/share/autostart/
-
${MKDIR} ${STAGEDIR}${VIDEODIR}
${INSTALL_DATA} ${VBOX_BIN}/vboxvideo_drv_$$(${XORG_DRV_ABI}).so \
${STAGEDIR}${VIDEODIR}/vboxvideo_drv.so
-
${MKDIR} ${STAGEDIR}${INPUTDIR}
${INSTALL_DATA} ${VBOX_BIN}/vboxmouse_drv_$$(${XORG_DRV_ABI}).so \
${STAGEDIR}${INPUTDIR}/vboxmouse_drv.so
-.endif
-.if ${PORT_OPTIONS:MOPENGL}
+do-install-OPENGL-on:
${INSTALL_DATA} ${FILESDIR}/vboxvideo.conf \
${STAGEDIR}${PREFIX}/etc/libmap.d/
${INSTALL_DATA} ${VBOX_LIBS:S|^|${VBOX_BIN}/|} \
${STAGEDIR}${PREFIX}/lib/
-.endif
.include <bsd.port.post.mk>
diff --git a/emulators/virtualbox-ose-additions/pkg-plist b/emulators/virtualbox-ose-additions/pkg-plist
index 0f6bcedb7e91..305d4347f2e2 100644
--- a/emulators/virtualbox-ose-additions/pkg-plist
+++ b/emulators/virtualbox-ose-additions/pkg-plist
@@ -15,6 +15,8 @@ lib/pam_vbox.so
%%X11%%lib/xorg/modules/input/vboxmouse_drv.so
sbin/VBoxControl
sbin/VBoxService
+sbin/mount_vboxvfs
%%X11%%share/autostart/vboxclient.desktop
/%%KMODDIR%%/vboxguest.ko
+/%%KMODDIR%%/vboxvfs.ko
%%DEBUG%%/%%KMODDIR%%/vboxguest.ko.symbols
diff --git a/emulators/virtualbox-ose/files/patch-src-VBox-Runtime-r0drv-freebsd-memobj-r0drv-freebsd.c b/emulators/virtualbox-ose/files/patch-src-VBox-Runtime-r0drv-freebsd-memobj-r0drv-freebsd.c
deleted file mode 100644
index 8c8d73e574fe..000000000000
--- a/emulators/virtualbox-ose/files/patch-src-VBox-Runtime-r0drv-freebsd-memobj-r0drv-freebsd.c
+++ /dev/null
@@ -1,121 +0,0 @@
---- src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c.orig 2015-12-18 16:22:29 UTC
-+++ src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c
-@@ -168,7 +168,7 @@ DECLHIDDEN(int) rtR0MemObjNativeFree(RTR
- VM_OBJECT_LOCK(pMemFreeBSD->pObject);
- #endif
- vm_page_t pPage = vm_page_find_least(pMemFreeBSD->pObject, 0);
--#if __FreeBSD_version < 1000000
-+#if __FreeBSD_version < 900000
- vm_page_lock_queues();
- #endif
- for (vm_page_t pPage = vm_page_find_least(pMemFreeBSD->pObject, 0);
-@@ -177,7 +177,7 @@ DECLHIDDEN(int) rtR0MemObjNativeFree(RTR
- {
- vm_page_unwire(pPage, 0);
- }
--#if __FreeBSD_version < 1000000
-+#if __FreeBSD_version < 900000
- vm_page_unlock_queues();
- #endif
- #if __FreeBSD_version >= 1000030
-@@ -205,12 +205,12 @@ static vm_page_t rtR0MemObjFreeBSDContig
- vm_page_t pPages;
- int cTries = 0;
-
--#if __FreeBSD_version > 1000000
-+#if __FreeBSD_version >= 902508
- int fFlags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
- if (fWire)
- fFlags |= VM_ALLOC_WIRED;
-
-- while (cTries <= 1)
-+ while (1)
- {
- #if __FreeBSD_version >= 1000030
- VM_OBJECT_WLOCK(pObject);
-@@ -224,18 +224,23 @@ static vm_page_t rtR0MemObjFreeBSDContig
- #else
- VM_OBJECT_UNLOCK(pObject);
- #endif
-- if (pPages)
-+ if (pPages || cTries >= 1)
- break;
-+#if __FreeBSD_version >= 1100092
-+ if (!vm_page_reclaim_contig(fFlags, cPages, 0, VmPhysAddrHigh, uAlignment, 0))
-+ break;
-+#elif __FreeBSD_version >= 1000015
- vm_pageout_grow_cache(cTries, 0, VmPhysAddrHigh);
-+#else
-+ vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
-+#endif
- cTries++;
- }
--
-- return pPages;
- #else
-- while (cTries <= 1)
-+ while (1)
- {
- pPages = vm_phys_alloc_contig(cPages, 0, VmPhysAddrHigh, uAlignment, 0);
-- if (pPages)
-+ if (pPages || cTries >= 1)
- break;
- vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
- cTries++;
-@@ -243,11 +248,8 @@ static vm_page_t rtR0MemObjFreeBSDContig
-
- if (!pPages)
- return pPages;
--#if __FreeBSD_version >= 1000030
-- VM_OBJECT_WLOCK(pObject);
--#else
-+
- VM_OBJECT_LOCK(pObject);
--#endif
- for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
- {
- vm_page_t pPage = pPages + iPage;
-@@ -259,13 +261,9 @@ static vm_page_t rtR0MemObjFreeBSDContig
- atomic_add_int(&cnt.v_wire_count, 1);
- }
- }
--#if __FreeBSD_version >= 1000030
-- VM_OBJECT_WUNLOCK(pObject);
--#else
- VM_OBJECT_UNLOCK(pObject);
- #endif
- return pPages;
--#endif
- }
-
- static int rtR0MemObjFreeBSDPhysAllocHelper(vm_object_t pObject, u_long cPages,
-@@ -295,13 +293,13 @@ static int rtR0MemObjFreeBSDPhysAllocHel
- while (iPage-- > 0)
- {
- pPage = vm_page_lookup(pObject, iPage);
--#if __FreeBSD_version < 1000000
-+#if __FreeBSD_version < 900000
- vm_page_lock_queues();
- #endif
- if (fWire)
- vm_page_unwire(pPage, 0);
- vm_page_free(pPage);
--#if __FreeBSD_version < 1000000
-+#if __FreeBSD_version < 900000
- vm_page_unlock_queues();
- #endif
- }
-@@ -751,7 +749,12 @@ DECLHIDDEN(int) rtR0MemObjNativeMapUser(
- {
- /** @todo: is this needed?. */
- PROC_LOCK(pProc);
-- AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
-+ AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr +
-+#if __FreeBSD_version >= 1100077
-+ lim_max_proc(pProc, RLIMIT_DATA));
-+#else
-+ lim_max(pProc, RLIMIT_DATA));
-+#endif
- PROC_UNLOCK(pProc);
- }
- else
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile
new file mode 100644
index 000000000000..6575a0c518b8
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile
@@ -0,0 +1,38 @@
+--- src/VBox/Additions/freebsd/Makefile.orig 2017-04-28 16:59:22.000000000 +0200
++++ src/VBox/Additions/freebsd/Makefile 2017-07-13 00:04:11.766870597 +0200
+@@ -31,6 +31,14 @@
+ cp vboxvfs/vboxvfs.ko .; \
+ fi; \
+ fi
++ @echo
++ @if [ -d mount_vboxvfs ]; then \
++ echo "=== Building mount_vboxvfs binary ==="; \
++ $(MAKE) KBUILD_VERBOSE=$(KBUILD_VERBOSE) -C mount_vboxvfs clean obj depend all; \
++ if [ -f mount_vboxvfs ]; then \
++ cp mount_vboxvfs/mount_vboxvfs .; \
++ fi; \
++ fi
+
+
+ install:
+@@ -38,13 +46,19 @@
+ @if [ -d vboxvfs ]; then \
+ $(MAKE) KBUILD_VERBOSE=$(KBUILD_VERBOSE) -C vboxvfs install; \
+ fi
++ @if [ -d mount_vboxvfs ]; then \
++ $(MAKE) KBUILD_VERBOSE=$(KBUILD_VERBOSE) -C mount_vboxvfs install; \
++ fi
+
+ clean:
+ @$(MAKE) -C vboxguest clean
+ @if [ -d vboxvfs ]; then \
+ $(MAKE) -C vboxvfs clean; \
+ fi
+- rm -f vboxguest.*o vboxvfs.*o
++ @if [ -d mount_vboxvfs ]; then \
++ $(MAKE) -C mount_vboxvfs clean; \
++ fi
++ rm -f vboxguest.*o vboxvfs.*o mount_vboxvfs
+
+ load:
+ @/sbin/kldunload vboxvfs || true
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile.kmk b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile.kmk
index 79654201adfb..4f1b2028eddb 100644
--- a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile.kmk
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_Makefile.kmk
@@ -1,11 +1,13 @@
--- src/VBox/Additions/freebsd/Makefile.kmk.orig 2016-07-18 11:50:44 UTC
+++ src/VBox/Additions/freebsd/Makefile.kmk
-@@ -24,7 +24,7 @@ endif
+@@ -24,7 +24,8 @@ endif
# Include sub-makefiles.
- #include $(PATH_SUB_CURRENT)/vboxvfs/Makefile.kmk
+-#include $(PATH_SUB_CURRENT)/vboxvfs/Makefile.kmk
-include $(PATH_SUB_CURRENT)/drm/Makefile.kmk
++include $(PATH_SUB_CURRENT)/vboxvfs/Makefile.kmk
+#include $(PATH_SUB_CURRENT)/drm/Makefile.kmk
++include $(PATH_SUB_CURRENT)/mount_vboxvfs/Makefile.kmk
#
# Globals
@@ -17,13 +19,16 @@
VBoxOGLarrayspu.so \
VBoxOGLcrutil.so \
VBoxOGLerrorspu.so \
-@@ -76,8 +77,7 @@ VBOX_FBSD_ADD_STRIP_BIN = \
- VBoxOGL.so
+@@ -76,8 +76,9 @@ VBOX_FBSD_ADD_STRIP_BIN = \
+- VBoxOGL.so
++ VBoxOGL.so \
++ mount_vboxvfs
VBOX_FBSD_ADD_MODULES = \
- vboxguest \
- vboxvideo_drm
-+ vboxguest
++ vboxguest \
++ vboxvfs
#
# All the bin files that go into the archives.
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_Makefile.kmk b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_Makefile.kmk
new file mode 100644
index 000000000000..9d4a30723db0
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_Makefile.kmk
@@ -0,0 +1,27 @@
+--- src/VBox/Additions/freebsd/mount_vboxvfs/Makefile.kmk.orig 1970-01-01 01:00:00.000000000 +0100
++++ src/VBox/Additions/freebsd/mount_vboxvfs/Makefile.kmk 2017-07-13 00:17:45.342815229 +0200
+@@ -0,0 +1,24 @@
++# $Id: Makefile.kmk $
++## @file
++# Sub-Makefile for the FreeBSD Shared folder mount utility.
++#
++
++SUB_DEPTH = ../../../../..
++include $(KBUILD_PATH)/subheader.kmk
++
++ifneq ($(KBUILD_HOST),freebsd)
++$(error "The FreeBSD guest additions can only be built on FreeBSD!")
++endif
++
++FREEBSD_SRC= /usr/src
++MOUNT= ${FREEBSD_SRC}/sbin/mount
++
++CFLAGS+=-I${MOUNT}
++
++PROGRAMS += mount_vboxvfs
++mount_vboxvfs_TEMPLATE = NewVBoxGuestR3Exe
++mount_vboxvfs_SOURCES = ${MOUNT}/getmntopts.c \
++ mount_vboxvfs.c
++
++include $(FILE_KBUILD_SUB_FOOTER)
++
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.8 b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.8
new file mode 100644
index 000000000000..88460e155de3
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.8
@@ -0,0 +1,56 @@
+--- src/VBox/Additions/freebsd/mount_vboxvfs/mount_vboxvfs.8.orig 1970-01-01 01:00:00.000000000 +0100
++++ src/VBox/Additions/freebsd/mount_vboxvfs/mount_vboxvfs.8 2017-07-12 19:24:26.106029000 +0200
+@@ -0,0 +1,53 @@
++.\"
++.\" Copyright (c) 1992, 1993, 1994
++.\" The Regents of the University of California. All rights reserved.
++.\"
++.\" Redistribution and use in source and binary forms, with or without
++.\" modification, are permitted provided that the following conditions
++.\" are met:
++.\" 1. Redistributions of source code must retain the above copyright
++.\" notice, this list of conditions and the following disclaimer.
++.\" 2. Redistributions in binary form must reproduce the above copyright
++.\" notice, this list of conditions and the following disclaimer in the
++.\" documentation and/or other materials provided with the distribution.
++.\" 4. Neither the name of the University nor the names of its contributors
++.\" may be used to endorse or promote products derived from this software
++.\" without specific prior written permission.
++.\"
++.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
++.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
++.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++.\" SUCH DAMAGE.
++.\"
++.Dd August 5, 2013
++.Dt MOUNT_VBOXFS 8
++.Os
++.Sh NAME
++.Nm mount_vboxfs
++.Nd "Mount the VirtualBox shared folder NAME from the host system to MOUNTPOINT"
++.Sh SYNOPSIS
++.Nm
++.Op Fl o Ar options
++.Ar NAME
++.Ar MOUNTPOINT
++.Sh DESCRIPTION
++The
++.Nm
++Mount the VirtualBox shared folder NAME from the host system to MOUNTPOINT
++.Pp
++The options are as follows:
++.Bl -tag -width indent
++.Fl w
++mount the shared folder writably (the default)
++.Fl r
++mount the shared folder read-only
++.Fl o
++OPTION[,OPTION...] use the mount options specified
++.El
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.c b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.c
new file mode 100644
index 000000000000..7dd7e6271e03
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_mount__vboxvfs_mount__vboxvfs.c
@@ -0,0 +1,171 @@
+--- src/VBox/Additions/freebsd/mount_vboxvfs/mount_vboxvfs.c.orig 1970-01-01 01:00:00.000000000 +0100
++++ src/VBox/Additions/freebsd/mount_vboxvfs/mount_vboxvfs.c 2017-07-12 19:24:26.106029000 +0200
+@@ -0,0 +1,168 @@
++/*
++* Copyright (c) 1992, 1993, 1994
++* The Regents of the University of California. All rights reserved.
++*
++* Redistribution and use in source and binary forms, with or without
++* modification, are permitted provided that the following conditions
++* are met:
++* 1. Redistributions of source code must retain the above copyright
++* notice, this list of conditions and the following disclaimer.
++* 2. Redistributions in binary form must reproduce the above copyright
++* notice, this list of conditions and the following disclaimer in the
++* documentation and/or other materials provided with the distribution.
++* 4. Neither the name of the University nor the names of its contributors
++* may be used to endorse or promote products derived from this software
++* without specific prior written permission.
++*
++* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
++* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
++* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++* SUCH DAMAGE.
++*/
++
++#include <sys/cdio.h>
++#include <sys/stat.h>
++#include <sys/file.h>
++#include <sys/iconv.h>
++#include <sys/param.h>
++#include <sys/linker.h>
++#include <sys/module.h>
++#include <sys/mount.h>
++#include <sys/uio.h>
++
++#include <err.h>
++#include <errno.h>
++#include <stdlib.h>
++#include <stdio.h>
++#include <string.h>
++#include <sysexits.h>
++#include <unistd.h>
++
++#include "mntopts.h"
++
++#define MAX_HOST_NAME 256
++static char mount_point[MAXPATHLEN + 1];
++static char vboxfs_vfsname[] = "vboxvfs";
++static struct mntopt mopts[] = {
++ MOPT_STDOPTS,
++ MOPT_END
++};
++
++static void usage(void) __dead2;
++
++static void
++usage(void)
++{
++ fprintf(stderr,
++ "Usage: [OPTIONS] NAME MOUNTPOINT\n"
++ "Mount the VirtualBox shared folder NAME to MOUNTPOINT.\n"
++ "\nOptions:\n"
++ " -w mount the shared folder writable \n"
++ " -r mount the shared folder read-only (the default)\n");
++ exit(1);
++}
++
++int
++main(int argc, char *argv[])
++{
++ struct iovec *iov;
++ struct stat st;
++ char *host_name;
++ char errmsg[255];
++ uid_t uid;
++ gid_t gid;
++ mode_t dir_mode, file_mode;
++ int iovlen;
++ int ronly = 0;
++ int error, ch;
++ int mntflags = 0;
++
++ iov = NULL;
++ iovlen = 0;
++ errmsg[0] = '\0';
++ uid = (uid_t)-1;
++ gid = (gid_t)-1;
++ file_mode = 0;
++ dir_mode = 0;
++
++ while ((ch = getopt(argc, argv, "rwo:h")) != -1)
++ switch(ch) {
++ default:
++ fprintf(stderr, "unknown option `%c:%#x'\n", ch, ch);
++ return (1);
++
++ case '?':
++ case 'h':
++ usage();
++ case 'r':
++ ronly = 1;
++ break;
++ case 'w':
++ ronly = 0;
++ break;
++ case 'o':
++ getmntopts(optarg, mopts, &mntflags, 0);
++ break;
++ }
++
++ if (argc - optind < 2)
++ usage();
++
++ host_name = argv[optind];
++ realpath(argv[optind+1], mount_point);
++
++ if (stat(mount_point, &st) == -1)
++ err(EX_OSERR, "could not find mount point %s", mount_point);
++
++ if (!S_ISDIR(st.st_mode)) {
++ errno = ENOTDIR;
++ err(EX_OSERR, "can't mount on %s", mount_point);
++ }
++
++ if (strlen(host_name) > MAX_HOST_NAME - 1)
++ err(EX_OSERR, "host name is too big %s", host_name);
++
++ if (ronly == 0)
++ mntflags |= MNT_ASYNC;
++ if (uid == (uid_t)-1)
++ uid = st.st_uid;
++ if (gid == (gid_t)-1)
++ gid = st.st_gid;
++ if (file_mode == 0)
++ file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
++ if (dir_mode == 0) {
++ dir_mode = file_mode;
++ if (dir_mode & S_IRUSR)
++ dir_mode |= S_IXUSR;
++ if (dir_mode & S_IRGRP)
++ dir_mode |= S_IXGRP;
++ if (dir_mode & S_IROTH)
++ dir_mode |= S_IXOTH;
++ }
++
++ build_iovec(&iov, &iovlen, "fstype", vboxfs_vfsname, (size_t)-1);
++ build_iovec(&iov, &iovlen, "fspath", mount_point, (size_t)-1);
++ build_iovec(&iov, &iovlen, "from", host_name, (size_t)-1);
++ build_iovec_argf(&iov, &iovlen, "uid", "%d", uid);
++ build_iovec_argf(&iov, &iovlen, "gid", "%d", gid);
++ build_iovec_argf(&iov, &iovlen, "file_mode", "%o", file_mode);
++ build_iovec_argf(&iov, &iovlen, "dir_mode", "%o", dir_mode);
++ build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
++
++ error = nmount(iov, iovlen, mntflags);
++ if (error == -1) {
++ if (errmsg[0] != '\0')
++ err(1, "%s: error: %s", mount_point, errmsg);
++ else
++ err(1, "%s: error %d", mount_point, error);
++ }
++
++ return (0);
++}
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_Makefile.kmk b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_Makefile.kmk
new file mode 100644
index 000000000000..6792e77d58e0
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_Makefile.kmk
@@ -0,0 +1,49 @@
+--- src/VBox/Additions/freebsd/vboxvfs/Makefile.kmk.orig 2017-04-28 16:59:22.000000000 +0200
++++ src/VBox/Additions/freebsd/vboxvfs/Makefile.kmk 2017-07-13 00:52:49.020669620 +0200
+@@ -33,32 +33,36 @@
+ $(vboxvfs_0_OUTDIR)
+ vboxvfs_SOURCES = \
+ vboxvfs_vfsops.c \
+- vboxvfs_vnops.c
++ vboxvfs_vnops.c \
++ vboxvfs_prov.c
+ vboxvfs_LIBS = \
+ $(VBOX_LIB_VBGL_R0) \
+ $(VBOX_LIB_IPRT_GUEST_R0)
+ vboxvfs_DEPS = \
+- $$(vboxvfs_0_OUTDIR)/vnode_if.h \
+- $$(vboxvfs_0_OUTDIR)/vnode_if_newproto.h \
+- $$(vboxvfs_0_OUTDIR)/vnode_if_typedef.h
++ $(vboxvfs_0_OUTDIR)/vnode_if.h \
++ $(vboxvfs_0_OUTDIR)/vnode_if_newproto.h \
++ $(vboxvfs_0_OUTDIR)/vnode_if_typedef.h
+ vboxvfs_CLEAN += $(vboxvfs_DEPS)
+
+ VBOX_AWK := /usr/bin/awk
+
+-$$(vboxvfs_0_OUTDIR)/vnode_if.h: $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
++$$(vboxvfs_0_OUTDIR):
++ $(QUIET)$(MKDIR) -p $(vboxvfs_0_OUTDIR)
++
++$$(vboxvfs_0_OUTDIR)/vnode_if.h: $$(vboxvfs_0_OUTDIR) $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
+ $(call MSG_TOOL,awk,VBoxGuest,$<,$@)
+ $(QUIET)$(VBOX_AWK) -f $(VBOX_FREEBSD_SRC)/tools/vnode_if.awk $(VBOX_FREEBSD_SRC)/kern/vnode_if.src -h
+- $(QUIET)$(MV) $(vboxvfs_0_OUTDIR)/vnode_if.h $(vboxvfs_0_OUTDIR)/vnode_if.h
++ $(QUIET)$(MV) vnode_if.h $(vboxvfs_0_OUTDIR)/vnode_if.h
+
+-$$(vboxvfs_0_OUTDIR)/vnode_if_newproto.h: $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
++$$(vboxvfs_0_OUTDIR)/vnode_if_newproto.h: $$(vboxvfs_0_OUTDIR) $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
+ $(call MSG_TOOL,awk,VBoxGuest,$<,$@)
+ $(QUIET)$(VBOX_AWK) -f $(VBOX_FREEBSD_SRC)/tools/vnode_if.awk $(VBOX_FREEBSD_SRC)/kern/vnode_if.src -p
+- $(QUIET)$(MV) $(vboxvfs_0_OUTDIR)/vnode_if_newproto.h $(vboxvfs_0_OUTDIR)/vnode_if_newproto.h
++ $(QUIET)$(MV) vnode_if_newproto.h $(vboxvfs_0_OUTDIR)/vnode_if_newproto.h
+
+-$$(vboxvfs_0_OUTDIR)/vnode_if_typedef.h: $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
++$$(vboxvfs_0_OUTDIR)/vnode_if_typedef.h: $$(vboxvfs_0_OUTDIR) $(VBOX_FREEBSD_SRC)/kern/vnode_if.src
+ $(call MSG_TOOL,awk,VBoxGuest,$<,$@)
+ $(QUIET)$(VBOX_AWK) -f $(VBOX_FREEBSD_SRC)/tools/vnode_if.awk $(VBOX_FREEBSD_SRC)/kern/vnode_if.src -q
+- $(QUIET)$(MV) $(vboxvfs_0_OUTDIR)/vnode_if_typedef.h $(vboxvfs_0_OUTDIR)/vnode_if_typedef.h
++ $(QUIET)$(MV) vnode_if_typedef.h $(vboxvfs_0_OUTDIR)/vnode_if_typedef.h
+
+ include $(FILE_KBUILD_SUB_FOOTER)
+
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs.h b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs.h
new file mode 100644
index 000000000000..7f4816d2598f
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs.h
@@ -0,0 +1,470 @@
+--- src/VBox/Additions/freebsd/vboxvfs/vboxvfs.h.orig 2017-04-28 16:59:22.000000000 +0200
++++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs.h 2017-07-13 01:05:04.399618524 +0200
+@@ -1,10 +1,5 @@
+-/* $Id: vboxvfs.h $ */
+-/** @file
+- * Description.
+- */
+-
+ /*
+ * Copyright (C) 2010-2016 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+@@ -21,72 +16,418 @@
+ #define VBOXVFS_VFSNAME "vboxvfs"
+ #define VBOXVFS_VERSION 1
+
+-#define MAX_HOST_NAME 256
+-#define MAX_NLS_NAME 32
++#define MAX_HOST_NAME 256
++#define MAX_NLS_NAME 32
++//#define MODEMASK 07777 /* mode bits plus permission bits */
++/** Helper macros */
++#define VFSTOVBOXFS(mp) ((struct vboxfs_mnt *)((mp)->mnt_data))
++#define VP_TO_VBOXFS_NODE(vp) ((struct vboxfs_node *)(vp)->v_data)
++#define VBOXTOV(np) ((struct vnode *)(np)->n_vnode)
++
++#define ROOTDIR_INO 1
++#define THEFILE_INO 2
++#define THEFILE_NAME "thefile"
++
++#define VBOXFS_NODE_LOCK(node) mtx_lock(&(node)->sf_interlock)
++#define VBOXFS_NODE_UNLOCK(node) mtx_unlock(&(node)->sf_interlock)
++#define VBOXFS_NODE_MTX(node) (&(node)->sf_interlock)
++#define VBOXFS_NODE_ASSERT_LOCKED(node) mtx_assert(VBOXFS_NODE_MTX(node), \
++ MA_OWNED)
++
++#ifdef INVARIANTS
++#define VBOXFS_ASSERT_LOCKED(node) do { \
++ MPASS(node != NULL); \
++ MPASS(node->sf_vnode != NULL); \
++ if (!VOP_ISLOCKED(node->sf_vnode) && \
++ !mtx_owned(VBOXFS_NODE_MTX(node))) \
++ panic("vboxfs: node is not locked: %p", node); \
++ } while (0)
++#define VBOXFS_ASSERT_ELOCKED(node) do { \
++ MPASS((node) != NULL); \
++ MPASS((node)->sf_vnode != NULL); \
++ mtx_assert(VBOXFS_NODE_MTX(node), MA_OWNED); \
++ ASSERT_VOP_LOCKED((node)->sf_vnode, "vboxfs"); \
++ } while (0)
++#else
++#define VBOXFS_ASSERT_LOCKED(node) (void)0
++#define VBOXFS_ASSERT_ELOCKED(node) (void)0
++#endif
+
+-struct vboxvfs_mount_info {
+- char name[MAX_HOST_NAME];
+- char nls_name[MAX_NLS_NAME];
+- int uid;
+- int gid;
+- int ttl;
+-};
++#define VBOXFS_VNODE_ALLOCATING 1
++#define VBOXFS_VNODE_WANT 2
++#define VBOXFS_VNODE_DOOMED 4
++#define VBOXFS_VNODE_WRECLAIM 8
++
++MALLOC_DECLARE(M_VBOXVFS);
+
+ #ifdef _KERNEL
++#ifndef FREEBSD_STYLE
++#include "../../../../../include/iprt/nocrt/limits.h"
++#include "../../../../../include/iprt/alloc.h"
++#include "../../../../../include/iprt/asm.h"
++#include "../../../../../include/iprt/asm-amd64-x86.h"
++#include "../../../../../include/iprt/asm-math.h"
++#include "../../../../../include/iprt/assert.h"
++#include "../../../../../include/iprt/cdefs.h"
++#include "../../../../../include/iprt/err.h"
++#include "../../../../../include/iprt/fs.h"
++#include "../../../../../include/iprt/log.h"
++#include "../../../../../include/iprt/mangling.h"
++#include "../../../../../include/iprt/mem.h"
++#include "../../../../../include/iprt/param.h"
++#include "../../../../../include/iprt/path.h"
++#include "../../../../../include/iprt/semaphore.h"
++#include "../../../../../include/iprt/stdarg.h"
++#include "../../../../../include/iprt/stdint.h"
++#include "../../../../../include/iprt/string.h"
++#include "../../../../../include/iprt/time.h"
++#include "../../../../../include/iprt/types.h"
++#include "../../../../../include/iprt/uni.h"
++#include "../../../../../include/iprt/nocrt/limits.h"
++#include "../../../../../include/iprt/alloc.h"
++#include "../../../../../include/iprt/asm.h"
++#include "../../../../../include/iprt/asm-amd64-x86.h"
++#include "../../../../../include/iprt/asm-math.h"
++#include "../../../../../include/iprt/assert.h"
++#include "../../../../../include/iprt/cdefs.h"
++#include "../../../../../include/iprt/err.h"
++#include "../../../../../include/iprt/fs.h"
++#include "../../../../../include/iprt/log.h"
++#include "../../../../../include/iprt/mangling.h"
++#include "../../../../../include/iprt/mem.h"
++#include "../../../../../include/iprt/param.h"
++#include "../../../../../include/iprt/path.h"
++#include "../../../../../include/iprt/semaphore.h"
++#include "../../../../../include/iprt/stdarg.h"
++#include "../../../../../include/iprt/stdint.h"
++#include "../../../../../include/iprt/string.h"
++#include "../../../../../include/iprt/time.h"
++#include "../../../../../include/iprt/types.h"
++#include "../../../../../include/iprt/uni.h"
++
++#include "../../common/VBoxGuestLib/SysHlp.h"
++
++#else
++
++#include "iprt/nocrt/limits.h"
++#include "iprt/alloc.h"
++#include "iprt/asm.h"
++#include "iprt/asm-amd64-x86.h"
++#include "iprt/asm-math.h"
++#include "iprt/assert.h"
++#include "iprt/cdefs.h"
++#include "iprt/err.h"
++#include "iprt/fs.h"
++#include "iprt/log.h"
++#include "iprt/mem.h"
++#include "iprt/param.h"
++#include "iprt/path.h"
++#include "iprt/semaphore.h"
++#include "iprt/stdarg.h"
++#include "iprt/stdint.h"
++#include "iprt/string.h"
++#include "iprt/time.h"
++#include "iprt/types.h"
++#include "iprt/uni.h"
++#include "iprt/nocrt/limits.h"
++#include "iprt/alloc.h"
++#include "iprt/asm.h"
++#include "iprt/asm-amd64-x86.h"
++#include "iprt/asm-math.h"
++#include "iprt/assert.h"
++#include "iprt/cdefs.h"
++#include "iprt/err.h"
++#include "iprt/fs.h"
++#include "iprt/log.h"
++#include "iprt/mem.h"
++#include "iprt/param.h"
++#include "iprt/path.h"
++#include "iprt/semaphore.h"
++#include "iprt/stdarg.h"
++#include "iprt/stdint.h"
++#include "iprt/string.h"
++#include "iprt/time.h"
++#include "iprt/types.h"
++#include "iprt/uni.h"
++
++#include "common/VBoxGuestLib/SysHlp.h"
++
++#endif /* !FREEBSD_STYLE */
+
+-#include <VBox/VBoxGuestLibSharedFolders.h>
+ #include <sys/mount.h>
+ #include <sys/vnode.h>
++#include <sys/_timespec.h>
++
++#if defined(RT_OS_FREEBSD) && defined(_KERNEL)
++# undef PVM /** XXX: For not conflict with PVM in sys/priority.h */
++#endif
++#include <VBox/VBoxGuestLibSharedFolders.h>
++
++#define VBOXVFS_DEBUG(lvl, ...) do { \
++ if (vboxvfs_debug >= (lvl)) { \
++ printf("VBOXVFS[%u]: ", lvl); \
++ printf(__VA_ARGS__); \
++ printf("\n"); \
++ } \
++} while (0)
++
++/*
++ * representation of an active mount point
++ */
++struct sfp_mount {
++ VBGLSFMAP map;
++};
++
++/*
++ * Mount / Unmount a shared folder.
++ *
++ * sfprov_mount() takes as input the connection pointer and the name of
++ * the shared folder. On success, it returns zero and supplies an
++ * sfp_mount_t handle. On failure it returns any relevant errno value.
++ *
++ * sfprov_unmount() unmounts the mounted file system. It returns 0 on
++ * success and any relevant errno on failure.
++ */
++typedef struct sfp_mount sfp_mount_t;
++
++struct sfp_file {
++ SHFLHANDLE handle;
++ VBGLSFMAP map; /* need this again for the close operation */
++};
++
++typedef struct sfp_file sfp_file_t;
++
++/*
++ * File operations: open/close/read/write/etc.
++ *
++ * open/create can return any relevant errno, however ENOENT
++ * generally means that the host file didn't exist.
++ */
++typedef struct sffs_stat {
++ mode_t sf_mode;
++ off_t sf_size;
++ off_t sf_alloc;
++ struct timespec sf_atime;
++ struct timespec sf_mtime;
++ struct timespec sf_ctime;
++} sffs_stat_t;
++
++/*
++ * Read directory entries.
++ */
++/*
++ * a singly linked list of buffers, each containing an array of stat's+dirent's.
++ * sf_len is length of the sf_entries array, in bytes.
++ */
++typedef struct sffs_dirents {
++ struct sffs_dirents *sf_next;
++ long long sf_len;
++ struct sffs_dirent {
++ sffs_stat_t sf_stat;
++ struct dirent sf_entry; /* this is variable length */
++ off_t sf_off;
++ } sf_entries[1];
++} sffs_dirents_t;
++
++/*
++ * Shared Folders filesystem per-mount data structure.
++ */
++struct vboxfs_mnt {
++ struct mount *sf_vfsp; /* filesystem's vfs struct */
++ sfp_mount_t *sf_handle; /* guest-host communication handle */
++ uid_t sf_uid; /* owner of all shared folders */
++ gid_t sf_gid; /* group of all shared folders */
++ mode_t sf_dmode; /* mode of all directories */
++ mode_t sf_fmode; /* mode of all files */
++ mode_t sf_dmask; /* mask of all directories */
++ mode_t sf_fmask; /* mask of all files */
++ int sf_stat_ttl; /* ttl for stat caches (in ms) */
++ int sf_fsync; /* whether to honor fsync or not */
++ uint64_t sf_ino; /* per FS ino generator */
++ uma_zone_t sf_node_pool;
++ struct vboxfs_node *sf_root;
++};
++
++/*
++ * vboxfs_node is the file system dependent vnode data for vboxfs.
++ * vboxfs_node's also track all files ever accessed, both open and closed.
++ * It duplicates some of the information in vnode, since it holds
++ * information for files that may have been completely closed.
++ *
++ */
++struct vboxfs_node {
++ struct vboxfs_mnt *vboxfsmp; /* containing mounted file system */
++ char *sf_path; /* full pathname to file or dir */
++ uint64_t sf_ino; /* assigned unique ID number */
++ struct vnode *sf_vnode; /* vnode if active */
++ sfp_file_t *sf_file; /* non NULL if open */
++ struct vboxfs_node *sf_parent; /* parent sfnode of this one */
++ uint16_t sf_children; /* number of children sfnodes */
++ uint8_t sf_type; /* VDIR or VREG */
++ uint8_t sf_vpstate; /* XXX: ADD COMMENT */
++ uint8_t sf_is_stale; /* this is stale and should be purged */
++ sffs_stat_t sf_stat; /* cached file attrs for this node */
++ uint64_t sf_stat_time; /* last-modified time of sf_stat */
++ sffs_dirents_t *sf_dir_list; /* list of entries for this directory */
+
+-struct vboxvfsmount {
+- uid_t uid;
+- gid_t gid;
+- mode_t file_mode;
+- mode_t dir_mode;
+- struct mount *mp;
+- struct ucred *owner;
+- u_int flags;
+- long nextino;
+- int caseopt;
+- int didrele;
++ /* interlock to protect sf_vpstate */
++ struct mtx sf_interlock;
++};
++
++struct vboxfs_mount_info {
++ char name[MAX_HOST_NAME]; /* share name */
++ char nls_name[MAX_NLS_NAME];/* name of an I/O charset */
++ int uid; /* user ID for all entries, default 0=root */
++ int gid; /* group ID for all entries, default 0=root */
++ int ttl; /* time to live */
++ int dmode; /* mode for directories if != 0xffffffff */
++ int fmode; /* mode for regular files if != 0xffffffff */
++ int dmask; /* umask applied to directories */
++ int fmask; /* umask applied to regular files */
+ };
+
+-/* structs - stolen from the linux shared module code */
+ struct sf_glob_info {
+- VBGLSFMAP map;
+-/* struct nls_table *nls;*/
+- int ttl;
+- int uid;
+- int gid;
+- struct vnode *vnode_root;
++ VBGLSFMAP map;
++/* struct nls_table *nls;*/
++ int ttl;
++ int uid;
++ int gid;
++ struct vnode *vnode_root;
+ };
+
++/** Per-file system mount instance data. */
++typedef struct vboxfs_globinfo
++{
++ VBGLSFMAP Map;
++ int Ttl;
++ int Uid;
++ int Gid;
++ struct mount *pVFS;
++ struct vboxfs_node *pVNodeRoot;
++} vboxfs_globinfo_t;
++
+ struct sf_inode_info {
+- SHFLSTRING *path;
+- int force_restat;
++ SHFLSTRING *path;
++ int force_restat;
+ };
+
+ #if 0
+ struct sf_dir_info {
+- struct list_head info_list;
++ struct list_head info_list;
+ };
+ #endif
+
+ struct sf_dir_buf {
+- size_t nb_entries;
+- size_t free_bytes;
+- size_t used_bytes;
+- void *buf;
++ size_t nb_entries;
++ size_t free_bytes;
++ size_t used_bytes;
++ void *buf;
+ #if 0
+- struct list_head head;
++ struct list_head head;
+ #endif
+ };
+
+ struct sf_reg_info {
+- SHFLHANDLE handle;
++ SHFLHANDLE handle;
+ };
+
++int vboxfs_alloc_vp(struct mount *, struct vboxfs_node *, int,
++ struct vnode **);
++void vboxfs_free_vp(struct vnode *);
++
++int vboxfs_alloc_node(struct mount *, struct vboxfs_mnt *, const char*,
++ enum vtype, uid_t, gid_t, mode_t, struct vboxfs_node *,
++ struct vboxfs_node **);
++void vboxfs_free_node(struct vboxfs_mnt *, struct vboxfs_node *);
++
++/*
++ * These are the provider interfaces used by sffs to access the underlying
++ * shared file system.
++ */
++#define SFPROV_VERSION 1
++
++/*
++ * Initialization and termination.
++ * sfprov_connect() is called once before any other interfaces and returns
++ * a handle used in further calls. The argument should be SFPROV_VERSION
++ * from above. On failure it returns a NULL pointer.
++ *
++ * sfprov_disconnect() must only be called after all sf file systems have been
++ * unmounted.
++ */
++typedef struct sfp_connection sfp_connection_t;
++
++extern sfp_connection_t *sfprov_connect(int);
++extern void sfprov_disconnect(void);
++
++extern int sfprov_mount(char *, sfp_mount_t **);
++extern int sfprov_unmount(sfp_mount_t *);
++
++/*
++ * query information about a mounted file system
++ */
++typedef struct sffs_fsinfo {
++ uint64_t blksize;
++ uint64_t blksused;
++ uint64_t blksavail;
++ uint32_t maxnamesize;
++ uint32_t readonly;
++ uint32_t serial;
++} sffs_fsinfo_t;
++
++extern int sfprov_get_fsinfo(sfp_mount_t *, sffs_fsinfo_t *);
++
++extern int sfprov_create(sfp_mount_t *, char *path, mode_t mode,
++ sfp_file_t **fp, sffs_stat_t *stat);
++extern int sfprov_open(sfp_mount_t *, char *path, sfp_file_t **fp);
++extern int sfprov_close(sfp_file_t *fp);
++extern int sfprov_read(sfp_file_t *, char * buffer, uint64_t offset,
++ uint32_t *numbytes, int buflocked);
++extern int sfprov_write(sfp_file_t *, char * buffer, uint64_t offset,
++ uint32_t *numbytes, int buflocked);
++extern int sfprov_fsync(sfp_file_t *fp);
++
++
++/*
++ * get/set information about a file (or directory) using pathname
++ */
++extern int sfprov_get_mode(sfp_mount_t *, char *, mode_t *);
++extern int sfprov_get_size(sfp_mount_t *, char *, uint64_t *);
++extern int sfprov_get_atime(sfp_mount_t *, char *, struct timespec *);
++extern int sfprov_get_mtime(sfp_mount_t *, char *, struct timespec *);
++extern int sfprov_get_ctime(sfp_mount_t *, char *, struct timespec *);
++extern int sfprov_get_attr(sfp_mount_t *, char *, sffs_stat_t *);
++extern int sfprov_set_attr(sfp_mount_t *, char *, mode_t,
++ struct timespec, struct timespec, struct timespec);
++extern int sfprov_set_size(sfp_mount_t *, char *, uint64_t);
++
++
++/*
++ * File/Directory operations
++ */
++extern int sfprov_trunc(sfp_mount_t *, char *);
++extern int sfprov_remove(sfp_mount_t *, char *path, u_int is_link);
++extern int sfprov_mkdir(sfp_mount_t *, char *path, mode_t mode,
++ sfp_file_t **fp, sffs_stat_t *stat);
++extern int sfprov_rmdir(sfp_mount_t *, char *path);
++extern int sfprov_rename(sfp_mount_t *, char *from, char *to, u_int is_dir);
++
++
++/*
++ * Symbolic link operations
++ */
++extern int sfprov_set_show_symlinks(void);
++extern int sfprov_readlink(sfp_mount_t *, char *path, char *target,
++ size_t tgt_size);
++extern int sfprov_symlink(sfp_mount_t *, char *linkname, char *target,
++ sffs_stat_t *stat);
++
++#define SFFS_DIRENTS_SIZE 8192
++#define SFFS_DIRENTS_OFF (offsetof(sffs_dirents_t, sf_entries[0]))
++
++extern int sfprov_readdir(sfp_mount_t *mnt, char *path,
++ sffs_dirents_t **dirents);
++
+ #endif /* KERNEL */
+
+ #endif /* !___VBOXVFS_H___ */
+-
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__prov.c b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__prov.c
new file mode 100644
index 000000000000..02eb57353569
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__prov.c
@@ -0,0 +1,1024 @@
+--- src/VBox/Additions/freebsd/vboxvfs/vboxvfs_prov.c.orig 1970-01-01 01:00:00.000000000 +0100
++++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs_prov.c 2017-07-12 19:24:26.109029000 +0200
+@@ -0,0 +1,1021 @@
++/*
++ * Copyright (C) 2008-2016 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.
++ *
++ * The contents of this file may alternatively be used under the terms
++ * of the Common Development and Distribution License Version 1.0
++ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
++ * VirtualBox OSE distribution, in which case the provisions of the
++ * CDDL are applicable instead of those of the GPL.
++ *
++ * You may elect to license modified versions of this file under the
++ * terms and conditions of either the GPL or the CDDL or both.
++ */
++
++/*
++ * Provider interfaces for shared folder file system.
++ */
++
++#include <sys/types.h>
++#include <sys/stat.h>
++#include <sys/param.h>
++#include <sys/systm.h>
++#include <sys/kernel.h>
++#include <sys/malloc.h>
++#include <sys/mount.h>
++#include <sys/vnode.h>
++#include <sys/dirent.h>
++#include <sys/proc.h>
++#include <vm/vm.h>
++#include <vm/pmap.h>
++#include <vm/vm_kern.h>
++#include <vm/vm_map.h>
++#include <vm/vm_object.h>
++#include <vm/vm_extern.h>
++#include "vboxvfs.h"
++
++#define DIRENT_RECLEN(namelen) \
++ ((offsetof(struct dirent, d_name[0]) + 1 + (namelen) + 7) & ~ 7)
++#define DIRENT_NAMELEN(reclen) \
++ ((reclen) - (offsetof(struct dirent, d_name[0])))
++
++static VBGLSFCLIENT vbox_client;
++
++extern u_int vboxvfs_debug;
++
++static int
++sfprov_vbox2errno(int rc)
++{
++ if (rc == VERR_ACCESS_DENIED)
++ return (EACCES);
++ if (rc == VERR_INVALID_NAME)
++ return (ENOENT);
++ return (RTErrConvertToErrno(rc));
++}
++
++/*
++ * utility to create strings
++ */
++static SHFLSTRING *
++sfprov_string(char *path, int *sz)
++{
++ SHFLSTRING *str;
++ int len = strlen(path);
++
++ *sz = len + 1 + sizeof (*str) - sizeof (str->String);
++ str = malloc(*sz, M_VBOXVFS, M_WAITOK | M_ZERO);
++ str->u16Size = len + 1;
++ str->u16Length = len;
++ strcpy(str->String.utf8, path);
++ return (str);
++}
++
++sfp_connection_t *
++sfprov_connect(int version)
++{
++ /* only one version for now, so must match */
++ int error = -1;
++
++ if (version != SFPROV_VERSION) {
++ printf("%s: version mismatch (%d, expected %d)\n", __func__,
++ version, SFPROV_VERSION);
++ return (NULL);
++ }
++
++ if (RT_FAILURE(VbglR0SfInit()))
++ return (NULL);
++
++ if (RT_FAILURE(VbglR0SfConnect(&vbox_client))) {
++ VbglR0SfTerm();
++ return (NULL);
++ }
++
++ if (RT_FAILURE(VbglR0SfSetUtf8(&vbox_client))) {
++ VbglR0SfDisconnect(&vbox_client);
++ VbglR0SfTerm();
++ return (NULL);
++ }
++ return ((sfp_connection_t *)&vbox_client);
++}
++
++void
++sfprov_disconnect()
++{
++ VbglR0SfDisconnect(&vbox_client);
++ VbglR0SfTerm();
++}
++
++int
++sfprov_mount(char *path, sfp_mount_t **mnt)
++{
++ sfp_mount_t *m;
++ SHFLSTRING *str;
++ int size;
++ int rc;
++
++ VBOXVFS_DEBUG(1, "%s: Enter", __FUNCTION__);
++ VBOXVFS_DEBUG(1, "%s: path: [%s]", __FUNCTION__, path);
++
++ m = malloc(sizeof (*m), M_VBOXVFS, M_WAITOK | M_ZERO);
++ str = sfprov_string(path, &size);
++
++ int error;
++ rc = VbglR0SfMapFolder(&vbox_client, str, &m->map);
++ if (RT_FAILURE(rc)) {
++ free(m, M_VBOXVFS);
++ *mnt = NULL;
++ error = sfprov_vbox2errno(rc);
++ } else {
++ *mnt = m;
++ error = 0;
++ }
++ free(str, M_VBOXVFS);
++ printf("%s(%s): error=%d rc=%d\n", __func__, path, error, rc);
++ return (error);
++}
++
++int
++sfprov_unmount(sfp_mount_t *mnt)
++{
++ int rc;
++
++ rc = VbglR0SfUnmapFolder(&vbox_client, &mnt->map);
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_unmount: VbglR0SfUnmapFolder() failed rc=%d\n", rc);
++ rc = sfprov_vbox2errno(rc);
++ } else {
++ rc = 0;
++ }
++
++ free(mnt, M_VBOXVFS);
++ return (rc);
++}
++
++/*
++ * query information about a mounted file system
++ */
++int
++sfprov_get_fsinfo(sfp_mount_t *mnt, sffs_fsinfo_t *fsinfo)
++{
++ int rc;
++ SHFLVOLINFO info;
++ uint32_t bytes = sizeof(SHFLVOLINFO);
++ size_t bytesused;
++
++ rc = VbglR0SfFsInfo(&vbox_client, &mnt->map, 0,
++ (SHFL_INFO_GET | SHFL_INFO_VOLUME), &bytes, (SHFLDIRINFO *)&info);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++
++ fsinfo->blksize = info.ulBytesPerAllocationUnit;
++ bytesused =
++ info.ullTotalAllocationBytes - info.ullAvailableAllocationBytes;
++ fsinfo->blksused = bytesused / info.ulBytesPerAllocationUnit;
++ fsinfo->blksavail = info.ullAvailableAllocationBytes /
++ info.ulBytesPerAllocationUnit;
++ fsinfo->maxnamesize = info.fsProperties.cbMaxComponent;
++ fsinfo->readonly = info.fsProperties.fReadOnly;
++ fsinfo->serial = info.ulSerial;
++ return (0);
++}
++
++/*
++ * file/directory information conversions.
++ */
++static void
++sfprov_fmode_from_mode(RTFMODE *fMode, mode_t mode)
++{
++ RTFMODE m = 0;
++
++#define mode_set(r) ((mode & (S_##r)) ? RTFS_UNIX_##r : 0)
++ m = mode_set (ISUID);
++ m |= mode_set (ISGID);
++ m |= (mode & S_ISVTX) ? RTFS_UNIX_ISTXT : 0;
++ m |= mode_set (IRUSR);
++ m |= mode_set (IWUSR);
++ m |= mode_set (IXUSR);
++ m |= mode_set (IRGRP);
++ m |= mode_set (IWGRP);
++ m |= mode_set (IXGRP);
++ m |= mode_set (IROTH);
++ m |= mode_set (IWOTH);
++ m |= mode_set (IXOTH);
++
++ if (S_ISDIR(mode))
++ m |= RTFS_TYPE_DIRECTORY;
++ else if (S_ISREG(mode))
++ m |= RTFS_TYPE_FILE;
++ else if (S_ISFIFO(mode))
++ m |= RTFS_TYPE_FIFO;
++ else if (S_ISCHR(mode))
++ m |= RTFS_TYPE_DEV_CHAR;
++ else if (S_ISBLK(mode))
++ m |= RTFS_TYPE_DEV_BLOCK;
++ else if (S_ISLNK(mode))
++ m |= RTFS_TYPE_SYMLINK;
++ else if (S_ISSOCK(mode))
++ m |= RTFS_TYPE_SOCKET;
++ else
++ m |= RTFS_TYPE_FILE;
++
++ *fMode = m;
++}
++
++static void
++sfprov_mode_from_fmode(mode_t *mode, RTFMODE fMode)
++{
++ mode_t m = 0;
++
++ if (RTFS_IS_DIRECTORY(fMode))
++ m |= S_IFDIR;
++ else if (RTFS_IS_FILE(fMode))
++ m |= S_IFREG;
++ else if (RTFS_IS_FIFO(fMode))
++ m |= S_IFIFO;
++ else if (RTFS_IS_DEV_CHAR(fMode))
++ m |= S_IFCHR;
++ else if (RTFS_IS_DEV_BLOCK(fMode))
++ m |= S_IFBLK;
++ else if (RTFS_IS_SYMLINK(fMode))
++ m |= S_IFLNK;
++ else if (RTFS_IS_SOCKET(fMode))
++ m |= S_IFSOCK;
++
++ if (fMode & RTFS_UNIX_IRUSR)
++ m |= S_IRUSR;
++ if (fMode & RTFS_UNIX_IWUSR)
++ m |= S_IWUSR;
++ if (fMode & RTFS_UNIX_IXUSR)
++ m |= S_IXUSR;
++ if (fMode & RTFS_UNIX_IRGRP)
++ m |= S_IRGRP;
++ if (fMode & RTFS_UNIX_IWGRP)
++ m |= S_IWGRP;
++ if (fMode & RTFS_UNIX_IXGRP)
++ m |= S_IXGRP;
++ if (fMode & RTFS_UNIX_IROTH)
++ m |= S_IROTH;
++ if (fMode & RTFS_UNIX_IWOTH)
++ m |= S_IWOTH;
++ if (fMode & RTFS_UNIX_IXOTH)
++ m |= S_IXOTH;
++ if (fMode & RTFS_UNIX_ISUID)
++ m |= S_ISUID;
++ if (fMode & RTFS_UNIX_ISGID)
++ m |= S_ISGID;
++ if (fMode & RTFS_UNIX_ISTXT)
++ m |= S_ISVTX;
++ *mode = m;
++}
++
++static void
++sfprov_ftime_from_timespec(struct timespec *time, RTTIMESPEC *ts)
++{
++ uint64_t nanosec = RTTimeSpecGetNano(ts);
++ time->tv_sec = nanosec / UINT64_C(1000000000);
++ time->tv_nsec = nanosec % UINT64_C(1000000000);
++}
++
++static void
++sfprov_stat_from_info(sffs_stat_t *stat, SHFLFSOBJINFO *info)
++{
++ sfprov_mode_from_fmode(&stat->sf_mode, info->Attr.fMode);
++ stat->sf_size = info->cbObject;
++ stat->sf_alloc = info->cbAllocated;
++ sfprov_ftime_from_timespec(&stat->sf_atime, &info->AccessTime);
++ sfprov_ftime_from_timespec(&stat->sf_mtime, &info->ModificationTime);
++ sfprov_ftime_from_timespec(&stat->sf_ctime, &info->ChangeTime);
++}
++
++/*
++ * File operations: open/close/read/write/etc.
++ *
++ * open/create can return any relevant errno, however ENOENT
++ * generally means that the host file didn't exist.
++ */
++int
++sfprov_create(
++ sfp_mount_t *mnt,
++ char *path,
++ mode_t mode,
++ sfp_file_t **fp,
++ sffs_stat_t *stat)
++{
++
++ int rc;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ int size;
++ sfp_file_t *newfp;
++
++ str = sfprov_string(path, &size);
++ parms.Handle = SHFL_HANDLE_NIL;
++ parms.Info.cbObject = 0;
++ sfprov_fmode_from_mode(&parms.Info.Attr.fMode, mode);
++ parms.CreateFlags = SHFL_CF_ACT_CREATE_IF_NEW |
++ SHFL_CF_ACT_REPLACE_IF_EXISTS | SHFL_CF_ACCESS_READWRITE;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ free(str, M_VBOXVFS);
++
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ if (parms.Handle == SHFL_HANDLE_NIL) {
++ if (parms.Result == SHFL_FILE_EXISTS)
++ return (EEXIST);
++ return (ENOENT);
++ }
++ newfp = malloc(sizeof(sfp_file_t), M_VBOXVFS, M_WAITOK | M_ZERO);
++ newfp->handle = parms.Handle;
++ newfp->map = mnt->map;
++ *fp = newfp;
++ sfprov_stat_from_info(stat, &parms.Info);
++ return (0);
++}
++
++int
++sfprov_open(sfp_mount_t *mnt, char *path, sfp_file_t **fp)
++{
++ int rc;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ int size;
++ sfp_file_t *newfp;
++
++ /*
++ * First we attempt to open it read/write. If that fails we
++ * try read only.
++ */
++ bzero(&parms, sizeof(parms));
++ str = sfprov_string(path, &size);
++ parms.Handle = SHFL_HANDLE_NIL;
++ parms.Info.cbObject = 0;
++ parms.CreateFlags = SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_READWRITE;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ if (RT_FAILURE(rc) && rc != VERR_ACCESS_DENIED) {
++ free(str, M_VBOXVFS);
++ return (sfprov_vbox2errno(rc));
++ }
++ if (parms.Handle == SHFL_HANDLE_NIL) {
++ if (parms.Result == SHFL_PATH_NOT_FOUND ||
++ parms.Result == SHFL_FILE_NOT_FOUND) {
++ free(str, M_VBOXVFS);
++ return (ENOENT);
++ }
++ parms.CreateFlags =
++ SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_READ;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ if (RT_FAILURE(rc)) {
++ free(str, M_VBOXVFS);
++ return (sfprov_vbox2errno(rc));
++ }
++ if (parms.Handle == SHFL_HANDLE_NIL) {
++ free(str, M_VBOXVFS);
++ return (ENOENT);
++ }
++ }
++ else
++ free(str, M_VBOXVFS);
++ newfp = malloc(sizeof(sfp_file_t), M_VBOXVFS, M_WAITOK | M_ZERO);
++ newfp->handle = parms.Handle;
++ newfp->map = mnt->map;
++ *fp = newfp;
++ return (0);
++}
++
++int
++sfprov_trunc(sfp_mount_t *mnt, char *path)
++{
++ int rc;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ int size;
++
++ /*
++ * open it read/write.
++ */
++ str = sfprov_string(path, &size);
++ parms.Handle = 0;
++ parms.Info.cbObject = 0;
++ parms.CreateFlags = SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_READWRITE |
++ SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ free(str, M_VBOXVFS);
++
++ if (RT_FAILURE(rc)) {
++ return (sfprov_vbox2errno(rc));
++ }
++ (void)VbglR0SfClose(&vbox_client, &mnt->map, parms.Handle);
++ return (0);
++}
++
++int
++sfprov_close(sfp_file_t *fp)
++{
++ int rc;
++
++ rc = VbglR0SfClose(&vbox_client, &fp->map, fp->handle);
++ free(fp, M_VBOXVFS);
++ return (0);
++}
++
++int
++sfprov_read(sfp_file_t *fp, char *buffer, uint64_t offset, uint32_t *numbytes,
++ int buflocked)
++{
++ int rc;
++
++ rc = VbglR0SfRead(&vbox_client, &fp->map, fp->handle, offset,
++ numbytes, (uint8_t *)buffer, buflocked);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++int
++sfprov_write(sfp_file_t *fp, char *buffer, uint64_t offset, uint32_t *numbytes,
++ int buflocked)
++{
++ int rc;
++
++ rc = VbglR0SfWrite(&vbox_client, &fp->map, fp->handle, offset,
++ numbytes, (uint8_t *)buffer, buflocked);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++int
++sfprov_fsync(sfp_file_t *fp)
++{
++ int rc;
++
++ rc = VbglR0SfFlush(&vbox_client, &fp->map, fp->handle);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++
++static int
++sfprov_getinfo(sfp_mount_t *mnt, char *path, PSHFLFSOBJINFO info)
++{
++ int rc;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ int size;
++
++ str = sfprov_string(path, &size);
++ parms.Handle = 0;
++ parms.Info.cbObject = 0;
++ parms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ free(str, M_VBOXVFS);
++
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ if (parms.Result != SHFL_FILE_EXISTS)
++ return (ENOENT);
++ *info = parms.Info;
++ return (0);
++}
++
++/*
++ * get information about a file (or directory)
++ */
++int
++sfprov_get_mode(sfp_mount_t *mnt, char *path, mode_t *mode)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ sfprov_mode_from_fmode(mode, info.Attr.fMode);
++ return (0);
++}
++
++int
++sfprov_get_size(sfp_mount_t *mnt, char *path, uint64_t *size)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ *size = info.cbObject;
++ return (0);
++}
++
++
++int
++sfprov_get_atime(sfp_mount_t *mnt, char *path, struct timespec *time)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ sfprov_ftime_from_timespec(time, &info.AccessTime);
++ return (0);
++}
++
++int
++sfprov_get_mtime(sfp_mount_t *mnt, char *path, struct timespec *time)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ sfprov_ftime_from_timespec(time, &info.ModificationTime);
++ return (0);
++}
++
++int
++sfprov_get_ctime(sfp_mount_t *mnt, char *path, struct timespec *time)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ sfprov_ftime_from_timespec(time, &info.ChangeTime);
++ return (0);
++}
++
++int
++sfprov_get_attr(sfp_mount_t *mnt, char *path, sffs_stat_t *attr)
++{
++ int rc;
++ SHFLFSOBJINFO info;
++
++ rc = sfprov_getinfo(mnt, path, &info);
++ if (rc)
++ return (rc);
++ sfprov_stat_from_info(attr, &info);
++ return (0);
++}
++
++static void
++sfprov_timespec_from_ftime(RTTIMESPEC *ts, struct timespec time)
++{
++ uint64_t nanosec = UINT64_C(1000000000) * time.tv_sec + time.tv_nsec;
++ RTTimeSpecSetNano(ts, nanosec);
++}
++
++int
++sfprov_set_attr(
++ sfp_mount_t *mnt,
++ char *path,
++ mode_t mode,
++ struct timespec atime,
++ struct timespec mtime,
++ struct timespec ctime)
++{
++ int rc, err;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ SHFLFSOBJINFO info;
++ uint32_t bytes;
++ int str_size;
++
++ str = sfprov_string(path, &str_size);
++ parms.Handle = 0;
++ parms.Info.cbObject = 0;
++ parms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
++ | SHFL_CF_ACT_FAIL_IF_NEW
++ | SHFL_CF_ACCESS_ATTR_WRITE;
++
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_set_attr: VbglR0SfCreate(%s) failed rc=%d\n",
++ path, rc);
++ err = sfprov_vbox2errno(rc);
++ goto fail2;
++ }
++ if (parms.Result != SHFL_FILE_EXISTS) {
++ err = ENOENT;
++ goto fail1;
++ }
++
++ RT_ZERO(info);
++#if 0
++ if (mask & AT_MODE)
++ sfprov_fmode_from_mode(&info.Attr.fMode, mode);
++ if (mask & AT_ATIME)
++ sfprov_timespec_from_ftime(&info.AccessTime, atime);
++ if (mask & AT_MTIME)
++ sfprov_timespec_from_ftime(&info.ModificationTime, mtime);
++ if (mask & AT_CTIME)
++ sfprov_timespec_from_ftime(&info.ChangeTime, ctime);
++#endif
++ bytes = sizeof(info);
++ rc = VbglR0SfFsInfo(&vbox_client, &mnt->map, parms.Handle,
++ (SHFL_INFO_SET | SHFL_INFO_FILE), &bytes, (SHFLDIRINFO *)&info);
++ if (RT_FAILURE(rc)) {
++ if (rc != VERR_ACCESS_DENIED && rc != VERR_WRITE_PROTECT)
++ {
++ printf("sfprov_set_attr: VbglR0SfFsInfo(%s, FILE) failed rc=%d\n",
++ path, rc);
++ }
++ err = sfprov_vbox2errno(rc);
++ goto fail1;
++ }
++
++ err = 0;
++
++fail1:
++ rc = VbglR0SfClose(&vbox_client, &mnt->map, parms.Handle);
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_set_attr: VbglR0SfClose(%s) failed rc=%d\n",
++ path, rc);
++ }
++fail2:
++ free(str, M_VBOXVFS);
++ return err;
++}
++
++int
++sfprov_set_size(sfp_mount_t *mnt, char *path, uint64_t size)
++{
++ int rc, err;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ SHFLFSOBJINFO info;
++ uint32_t bytes;
++ int str_size;
++
++ str = sfprov_string(path, &str_size);
++ parms.Handle = 0;
++ parms.Info.cbObject = 0;
++ parms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
++ | SHFL_CF_ACT_FAIL_IF_NEW
++ | SHFL_CF_ACCESS_WRITE;
++
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_set_size: VbglR0SfCreate(%s) failed rc=%d\n",
++ path, rc);
++ err = sfprov_vbox2errno(rc);
++ goto fail2;
++ }
++ if (parms.Result != SHFL_FILE_EXISTS) {
++ err = ENOENT;
++ goto fail1;
++ }
++
++ RT_ZERO(info);
++ info.cbObject = size;
++ bytes = sizeof(info);
++ rc = VbglR0SfFsInfo(&vbox_client, &mnt->map, parms.Handle,
++ (SHFL_INFO_SET | SHFL_INFO_SIZE), &bytes, (SHFLDIRINFO *)&info);
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_set_size: VbglR0SfFsInfo(%s, SIZE) failed rc=%d\n",
++ path, rc);
++ err = sfprov_vbox2errno(rc);
++ goto fail1;
++ }
++
++ err = 0;
++
++fail1:
++ rc = VbglR0SfClose(&vbox_client, &mnt->map, parms.Handle);
++ if (RT_FAILURE(rc)) {
++ printf("sfprov_set_size: VbglR0SfClose(%s) failed rc=%d\n",
++ path, rc);
++ }
++fail2:
++ free(str, M_VBOXVFS);
++ return err;
++}
++
++/*
++ * Directory operations
++ */
++int
++sfprov_mkdir(
++ sfp_mount_t *mnt,
++ char *path,
++ mode_t mode,
++ sfp_file_t **fp,
++ sffs_stat_t *stat)
++{
++ int rc;
++ SHFLCREATEPARMS parms;
++ SHFLSTRING *str;
++ int size;
++ sfp_file_t *newfp;
++
++ str = sfprov_string(path, &size);
++ parms.Handle = SHFL_HANDLE_NIL;
++ parms.Info.cbObject = 0;
++ sfprov_fmode_from_mode(&parms.Info.Attr.fMode, mode);
++ parms.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_CREATE_IF_NEW |
++ SHFL_CF_ACT_FAIL_IF_EXISTS | SHFL_CF_ACCESS_READ;
++ rc = VbglR0SfCreate(&vbox_client, &mnt->map, str, &parms);
++ free(str, M_VBOXVFS);
++
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ if (parms.Handle == SHFL_HANDLE_NIL) {
++ if (parms.Result == SHFL_FILE_EXISTS)
++ return (EEXIST);
++ return (ENOENT);
++ }
++ newfp = malloc(sizeof(sfp_file_t), M_VBOXVFS, M_WAITOK | M_ZERO);
++ newfp->handle = parms.Handle;
++ newfp->map = mnt->map;
++ *fp = newfp;
++ sfprov_stat_from_info(stat, &parms.Info);
++ return (0);
++}
++
++int
++sfprov_set_show_symlinks(void)
++{
++ int rc;
++
++ rc = VbglR0SfSetSymlinks(&vbox_client);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++
++ return (0);
++}
++
++int
++sfprov_remove(sfp_mount_t *mnt, char *path, u_int is_link)
++{
++ int rc;
++ SHFLSTRING *str;
++ int size;
++
++ str = sfprov_string(path, &size);
++ rc = VbglR0SfRemove(&vbox_client, &mnt->map, str,
++ SHFL_REMOVE_FILE | (is_link ? SHFL_REMOVE_SYMLINK : 0));
++ free(str, M_VBOXVFS);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++int
++sfprov_readlink(
++ sfp_mount_t *mnt,
++ char *path,
++ char *target,
++ size_t tgt_size)
++{
++ int rc;
++ SHFLSTRING *str;
++ int size;
++
++ str = sfprov_string(path, &size);
++
++ rc = VbglR0SfReadLink(&vbox_client, &mnt->map, str, (uint32_t) tgt_size,
++ target);
++ if (RT_FAILURE(rc))
++ rc = sfprov_vbox2errno(rc);
++
++ free(str, M_VBOXVFS);
++ return (rc);
++}
++
++int
++sfprov_symlink(
++ sfp_mount_t *mnt,
++ char *linkname,
++ char *target,
++ sffs_stat_t *stat)
++{
++ int rc;
++ SHFLSTRING *lnk, *tgt;
++ int lnk_size, tgt_size;
++ SHFLFSOBJINFO info;
++
++ lnk = sfprov_string(linkname, &lnk_size);
++ tgt = sfprov_string(target, &tgt_size);
++
++ rc = VbglR0SfSymlink(&vbox_client, &mnt->map, lnk, tgt, &info);
++ if (RT_FAILURE(rc)) {
++ rc = sfprov_vbox2errno(rc);
++ goto done;
++ }
++
++ if (stat != NULL)
++ sfprov_stat_from_info(stat, &info);
++
++done:
++ free(lnk, M_VBOXVFS);
++ free(tgt, M_VBOXVFS);
++
++ return (rc);
++}
++
++int
++sfprov_rmdir(sfp_mount_t *mnt, char *path)
++{
++ int rc;
++ SHFLSTRING *str;
++ int size;
++
++ str = sfprov_string(path, &size);
++ rc = VbglR0SfRemove(&vbox_client, &mnt->map, str, SHFL_REMOVE_DIR);
++ free(str, M_VBOXVFS);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++int
++sfprov_rename(sfp_mount_t *mnt, char *from, char *to, u_int is_dir)
++{
++ int rc;
++ SHFLSTRING *old, *new;
++ int old_size, new_size;
++
++ old = sfprov_string(from, &old_size);
++ new = sfprov_string(to, &new_size);
++ rc = VbglR0SfRename(&vbox_client, &mnt->map, old, new,
++ (is_dir ? SHFL_RENAME_DIR : SHFL_RENAME_FILE) |
++ SHFL_RENAME_REPLACE_IF_EXISTS);
++ free(old, M_VBOXVFS);
++ free(new, M_VBOXVFS);
++ if (RT_FAILURE(rc))
++ return (sfprov_vbox2errno(rc));
++ return (0);
++}
++
++/*
++ * Read all filenames in a directory.
++ *
++ * - success - all entries read and returned
++ * - ENOENT - Couldn't open the directory for reading
++ * - EINVAL - Internal error of some kind
++ *
++ * On successful return, *dirents points to a list of sffs_dirents_t;
++ * for each dirent, all fields except the d_ino will be set appropriately.
++ * The caller is responsible for freeing the dirents buffer.
++ */
++int
++sfprov_readdir(
++ sfp_mount_t *mnt,
++ char *path,
++ sffs_dirents_t **dirents)
++{
++ int error;
++ char *cp;
++ int len;
++ SHFLSTRING *mask_str = NULL; /* must be path with "/" appended */
++ int mask_size;
++ sfp_file_t *fp;
++ uint32_t infobuff_alloc = 16384;
++ SHFLDIRINFO *infobuff = NULL, *info;
++ uint32_t numbytes;
++ uint32_t nents;
++ uint32_t size;
++ off_t offset;
++ sffs_dirents_t *cur_buf;
++ struct sffs_dirent *dirent;
++ unsigned short reclen;
++ unsigned short entlen;
++
++ *dirents = NULL;
++
++ error = sfprov_open(mnt, path, &fp);
++ if (error != 0)
++ return (ENOENT);
++
++ /*
++ * Allocate the first dirents buffers.
++ */
++ *dirents = malloc(SFFS_DIRENTS_SIZE, M_VBOXVFS, M_WAITOK | M_ZERO);
++ if (*dirents == NULL) {
++ error = (ENOSPC);
++ goto done;
++ }
++ cur_buf = *dirents;
++ cur_buf->sf_next = NULL;
++ cur_buf->sf_len = 0;
++
++ /*
++ * Create mask that VBox expects. This needs to be the directory path,
++ * plus a "*" wildcard to get all files.
++ */
++ len = strlen(path) + 3;
++ cp = malloc(len, M_VBOXVFS, M_WAITOK | M_ZERO);
++ if (cp == NULL) {
++ error = (ENOSPC);
++ goto done;
++ }
++ strcpy(cp, path);
++ strcat(cp, "/*");
++ mask_str = sfprov_string(cp, &mask_size);
++ free(cp, M_VBOXVFS);
++
++ /*
++ * Now loop using VbglR0SfDirInfo
++ */
++ infobuff = malloc(infobuff_alloc, M_VBOXVFS, M_WAITOK | M_ZERO);
++ if (infobuff == NULL) {
++ error = (ENOSPC);
++ goto done;
++ }
++
++ offset = 0;
++ for (;;) {
++ numbytes = infobuff_alloc;
++ error = VbglR0SfDirInfo(&vbox_client, &fp->map, fp->handle,
++ mask_str, 0, 0, &numbytes, infobuff, &nents);
++
++ switch (error) {
++ case VINF_SUCCESS:
++ /* fallthrough */
++ case VERR_NO_MORE_FILES:
++ break;
++ case VERR_NO_TRANSLATION:
++ /* XXX ??? */
++ break;
++ default:
++ error = sfprov_vbox2errno(error);
++ goto done;
++ }
++
++ /*
++ * Create the dirent_t's and save the stats for each name
++ */
++ for (info = infobuff; (char *) info < (char *) infobuff + numbytes; nents--) {
++ size_t buflen;
++
++ /* expand buffers if we need more space */
++ reclen = DIRENT_RECLEN(strlen(info->name.String.utf8));
++ entlen = sizeof(struct sffs_dirent) + reclen;
++ buflen = SFFS_DIRENTS_OFF + cur_buf->sf_len + entlen;
++ if (buflen > SFFS_DIRENTS_SIZE) {
++ cur_buf->sf_next = malloc(SFFS_DIRENTS_SIZE,
++ M_VBOXVFS, M_WAITOK | M_ZERO);
++ if (cur_buf->sf_next == NULL) {
++ error = ENOSPC;
++ goto done;
++ }
++ cur_buf = cur_buf->sf_next;
++ cur_buf->sf_next = NULL;
++ cur_buf->sf_len = 0;
++ }
++
++ /* create the dirent with the name, offset, and len */
++ dirent = (struct sffs_dirent *)
++ (((char *) &cur_buf->sf_entries[0]) + cur_buf->sf_len);
++ strncpy(&dirent->sf_entry.d_name[0],
++ info->name.String.utf8, DIRENT_NAMELEN(reclen));
++ dirent->sf_entry.d_reclen = reclen;
++ dirent->sf_entry.d_namlen = strlen(info->name.String.utf8);
++ dirent->sf_entry.d_name[dirent->sf_entry.d_namlen] = 0;
++ offset += entlen;
++ dirent->sf_off = offset;
++
++ /* save the stats */
++ sfprov_stat_from_info(&dirent->sf_stat, &info->Info);
++
++ /* next info */
++ cur_buf->sf_len += entlen;
++ size = offsetof (SHFLDIRINFO, name.String) + info->name.u16Size;
++ info = (SHFLDIRINFO *) ((uintptr_t) info + size);
++ }
++ KASSERT(nents == 0, ("nents != 0"));
++ KASSERT((char *) info == (char *) infobuff + numbytes, "(char *) info != (char *) infobuff + numbytes");
++
++ if (error == VERR_NO_MORE_FILES)
++ break;
++ }
++ error = 0;
++
++done:
++ if (error != 0) {
++ while (*dirents) {
++ cur_buf = (*dirents)->sf_next;
++ free(*dirents, M_VBOXVFS);
++ *dirents = cur_buf;
++ }
++ }
++ if (infobuff != NULL)
++ free(infobuff, M_VBOXVFS);
++ if (mask_str != NULL)
++ free(mask_str, M_VBOXVFS);
++ sfprov_close(fp);
++
++ return (error);
++}
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vfsops.c b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vfsops.c
new file mode 100644
index 000000000000..1b6eaf688ff4
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vfsops.c
@@ -0,0 +1,664 @@
+--- src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vfsops.c.orig 2017-04-28 16:59:22.000000000 +0200
++++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vfsops.c 2017-07-12 19:24:26.109029000 +0200
+@@ -1,10 +1,5 @@
+-/* $Id: vboxvfs_vfsops.c $ */
+-/** @file
+- * Description.
+- */
+-
+ /*
+ * Copyright (C) 2008-2016 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+@@ -14,245 +9,479 @@
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+-
+-#include "vboxvfs.h"
++#include <sys/types.h>
+ #include <sys/param.h>
+ #include <sys/systm.h>
+ #include <sys/proc.h>
+ #include <sys/bio.h>
+ #include <sys/buf.h>
++#include <sys/conf.h>
++#include <sys/dirent.h>
+ #include <sys/kernel.h>
+ #include <sys/sysctl.h>
+ #include <sys/vnode.h>
+ #include <sys/mount.h>
++#include <sys/namei.h>
++#include <sys/fcntl.h>
++#include <sys/priv.h>
+ #include <sys/stat.h>
+ #include <sys/malloc.h>
+ #include <sys/module.h>
++#include <sys/sbuf.h>
+
+-#include <iprt/mem.h>
+-
+-#define VFSMP2SFGLOBINFO(mp) ((struct sf_glob_info *)mp->mnt_data)
+-
+-static int vboxvfs_version = VBOXVFS_VERSION;
++#include <geom/geom.h>
++#include <geom/geom_vfs.h>
+
+-SYSCTL_NODE(_vfs, OID_AUTO, vboxvfs, CTLFLAG_RW, 0, "VirtualBox shared filesystem");
+-SYSCTL_INT(_vfs_vboxvfs, OID_AUTO, version, CTLFLAG_RD, &vboxvfs_version, 0, "");
++#include "vboxvfs.h"
+
+-/* global connection to the host service. */
+-static VBGLSFCLIENT g_vboxSFClient;
++#define VFSMP2SFGLOBINFO(mp) ((struct sf_glob_info *)mp->mnt_data)
+
+-static vfs_init_t vboxvfs_init;
+-static vfs_uninit_t vboxvfs_uninit;
+-static vfs_cmount_t vboxvfs_cmount;
+-static vfs_mount_t vboxvfs_mount;
+-static vfs_root_t vboxvfs_root;
+-static vfs_quotactl_t vboxvfs_quotactl;
+-static vfs_statfs_t vboxvfs_statfs;
+-static vfs_unmount_t vboxvfs_unmount;
+-
+-static struct vfsops vboxvfs_vfsops = {
+- .vfs_init = vboxvfs_init,
+- .vfs_cmount = vboxvfs_cmount,
+- .vfs_mount = vboxvfs_mount,
+- .vfs_quotactl = vboxvfs_quotactl,
+- .vfs_root = vboxvfs_root,
+- .vfs_statfs = vboxvfs_statfs,
+- .vfs_sync = vfs_stdsync,
+- .vfs_uninit = vboxvfs_uninit,
+- .vfs_unmount = vboxvfs_unmount,
++#ifdef MALLOC_DECLARE
++MALLOC_DEFINE(M_VBOXVFS, "vboxvfs", "VBOX VFS");
++#endif
++
++static sfp_connection_t *sfprov = NULL;
++
++static int vboxfs_version = VBOXVFS_VERSION;
++u_int vboxvfs_debug = 1;
++
++SYSCTL_NODE(_vfs, OID_AUTO, vboxfs, CTLFLAG_RW, 0, "VirtualBox shared filesystem");
++SYSCTL_INT(_vfs_vboxfs, OID_AUTO, version, CTLFLAG_RD, &vboxfs_version, 0, "");
++SYSCTL_UINT(_vfs_vboxfs, OID_AUTO, debug, CTLFLAG_RW, &vboxvfs_debug, 0, "Debug level");
++
++static vfs_init_t vboxfs_init;
++static vfs_uninit_t vboxfs_uninit;
++static vfs_cmount_t vboxfs_cmount;
++static vfs_mount_t vboxfs_mount;
++static vfs_root_t vboxfs_root;
++static vfs_quotactl_t vboxfs_quotactl;
++static vfs_statfs_t vboxfs_statfs;
++static vfs_unmount_t vboxfs_unmount;
++
++static struct vfsops vboxfs_vfsops = {
++ .vfs_init = vboxfs_init,
++ .vfs_cmount = vboxfs_cmount,
++ .vfs_mount = vboxfs_mount,
++ .vfs_quotactl = vboxfs_quotactl,
++ .vfs_root = vboxfs_root,
++ .vfs_statfs = vboxfs_statfs,
++ .vfs_sync = vfs_stdsync,
++ .vfs_uninit = vboxfs_uninit,
++ .vfs_unmount = vboxfs_unmount
+ };
+
+
+-VFS_SET(vboxvfs_vfsops, vboxvfs, VFCF_NETWORK);
++VFS_SET(vboxfs_vfsops, vboxvfs, VFCF_NETWORK);
+ MODULE_DEPEND(vboxvfs, vboxguest, 1, 1, 1);
+
+-static int vboxvfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
++/*
++ * Allocates a new node of type 'type' inside the 'tmp' mount point, with
++ * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
++ * using the credentials of the process 'p'.
++ *
++ * If the node type is set to 'VDIR', then the parent parameter must point
++ * to the parent directory of the node being created. It may only be NULL
++ * while allocating the root node.
++ *
++ * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
++ * specifies the device the node represents.
++ *
++ * If the node type is set to 'VLNK', then the parameter target specifies
++ * the file name of the target file for the symbolic link that is being
++ * created.
++ *
++ * Note that new nodes are retrieved from the available list if it has
++ * items or, if it is empty, from the node pool as long as there is enough
++ * space to create them.
++ *
++ * Returns zero on success or an appropriate error code on failure.
++ */
++int
++vboxfs_alloc_node(struct mount *mp, struct vboxfs_mnt *vsfmp, const char *fullpath,
++ enum vtype type, uid_t uid, gid_t gid, mode_t mode, struct vboxfs_node *parent,
++ struct vboxfs_node **node)
+ {
+- struct vboxvfs_mount_info args;
+- int rc = 0;
+-
+- printf("%s: Enter\n", __FUNCTION__);
+-
+- rc = copyin(data, &args, sizeof(struct vboxvfs_mount_info));
+- if (rc)
+- return rc;
++ struct vboxfs_node *nnode;
+
+- ma = mount_argf(ma, "uid", "%d", args.uid);
+- ma = mount_argf(ma, "gid", "%d", args.gid);
+- ma = mount_arg(ma, "from", args.name, -1);
++ if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
++ /*
++ * When a new tmpfs node is created for fully
++ * constructed mount point, there must be a parent
++ * node, which vnode is locked exclusively. As
++ * consequence, if the unmount is executing in
++ * parallel, vflush() cannot reclaim the parent vnode.
++ * Due to this, the check for MNTK_UNMOUNT flag is not
++ * racy: if we did not see MNTK_UNMOUNT flag, then tmp
++ * cannot be destroyed until node construction is
++ * finished and the parent vnode unlocked.
++ *
++ * Tmpfs does not need to instantiate new nodes during
++ * unmount.
++ */
++ return (EBUSY);
++ }
++
++ nnode = (struct vboxfs_node *)uma_zalloc_arg(
++ vsfmp->sf_node_pool, vsfmp, M_WAITOK);
++
++ /* Generic initialization. */
++ nnode->sf_type = type;
++ nnode->sf_ino = vsfmp->sf_ino++;
++ nnode->sf_path = strdup(fullpath, M_VBOXVFS);
++ nnode->sf_parent = parent;
++ nnode->vboxfsmp = vsfmp;
++
++ /* Type-specific initialization. */
++ switch (nnode->sf_type) {
++ case VBLK:
++ case VCHR:
++ case VDIR:
++ case VFIFO:
++ case VSOCK:
++ case VLNK:
++ case VREG:
++ break;
++
++ default:
++ panic("vboxfs_alloc_node: type %p %d", nnode, (int)nnode->sf_type);
++ }
+
+- rc = kernel_mount(ma, flags);
+-
+- printf("%s: Leave rc=%d\n", __FUNCTION__, rc);
+-
+- return rc;
++ *node = nnode;
++ return 0;
+ }
+
+-static const char *vboxvfs_opts[] = {
+- "uid", "gid", "from", "fstype", "fspath", "errmsg", NULL
+-};
+-
+-static int vboxvfs_mount(struct mount *mp, struct thread *td)
++void
++vboxfs_free_node(struct vboxfs_mnt *vboxfs, struct vboxfs_node *node)
+ {
+- int rc;
+- char *pszShare;
+- int cbShare, cbOption;
+- int uid = 0, gid = 0;
+- struct sf_glob_info *pShFlGlobalInfo;
+- SHFLSTRING *pShFlShareName = NULL;
+- int cbShFlShareName;
+-
+- printf("%s: Enter\n", __FUNCTION__);
+-
+- if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
+- return EOPNOTSUPP;
+-
+- if (vfs_filteropt(mp->mnt_optnew, vboxvfs_opts))
+- {
+- vfs_mount_error(mp, "%s", "Invalid option");
+- return EINVAL;
+- }
+-
+- rc = vfs_getopt(mp->mnt_optnew, "from", (void **)&pszShare, &cbShare);
+- if (rc || pszShare[cbShare-1] != '\0' || cbShare > 0xfffe)
+- return EINVAL;
+-
+- rc = vfs_getopt(mp->mnt_optnew, "gid", (void **)&gid, &cbOption);
+- if ((rc != ENOENT) && (rc || cbOption != sizeof(gid)))
+- return EINVAL;
+-
+- rc = vfs_getopt(mp->mnt_optnew, "uid", (void **)&uid, &cbOption);
+- if ((rc != ENOENT) && (rc || cbOption != sizeof(uid)))
+- return EINVAL;
+
+- pShFlGlobalInfo = RTMemAllocZ(sizeof(struct sf_glob_info));
+- if (!pShFlGlobalInfo)
+- return ENOMEM;
++#ifdef INVARIANTS
++ TMPFS_NODE_LOCK(node);
++ MPASS(node->sf_vnode == NULL);
++ MPASS((node->sf_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
++ TMPFS_NODE_UNLOCK(node);
++#endif
++ if (node->sf_path)
++ free(node->sf_path, M_VBOXVFS);
+
+- cbShFlShareName = offsetof (SHFLSTRING, String.utf8) + cbShare + 1;
+- pShFlShareName = RTMemAllocZ(cbShFlShareName);
+- if (!pShFlShareName)
+- return VERR_NO_MEMORY;
++ uma_zfree(vboxfs->sf_node_pool, node);
++}
+
+- pShFlShareName->u16Length = cbShare;
+- pShFlShareName->u16Size = cbShare + 1;
+- memcpy (pShFlShareName->String.utf8, pszShare, cbShare + 1);
++static int
++vboxfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
++{
++ struct vboxfs_mount_info args;
++ int error = 0;
+
+- rc = VbglR0SfMapFolder (&g_vboxSFClient, pShFlShareName, &pShFlGlobalInfo->map);
+- RTMemFree(pShFlShareName);
++ if (data == NULL)
++ return (EINVAL);
++ error = copyin(data, &args, sizeof(struct vboxfs_mount_info));
++ if (error)
++ return (error);
++
++ ma = mount_argf(ma, "uid", "%d", args.uid);
++ ma = mount_argf(ma, "gid", "%d", args.gid);
++ ma = mount_argf(ma, "file_mode", "%d", args.fmode);
++ ma = mount_argf(ma, "dir_mode", "%d", args.dmode);
++ ma = mount_arg(ma, "from", args.name, -1);
+
+- if (RT_FAILURE (rc))
+- {
+- RTMemFree(pShFlGlobalInfo);
+- printf("VbglR0SfMapFolder failed rc=%d\n", rc);
+- return EPROTO;
+- }
++ return (kernel_mount(ma, flags));
++}
+
+- pShFlGlobalInfo->uid = uid;
+- pShFlGlobalInfo->gid = gid;
++static const char *vboxfs_opts[] = {
++ "fstype",
++ "fspath",
++ "from",
++ "uid",
++ "gid",
++ "file_mode",
++ "dir_mode",
++ "errmsg",
++ NULL
++};
+
+- mp->mnt_data = pShFlGlobalInfo;
++#define VBOX_INTOPT(optname, val, base) do { \
++ char *ep, *optarg = NULL; \
++ if (vfs_getopt(opts, optname, (void **)&optarg, NULL) == 0) { \
++ if (optarg != NULL && *optarg == '\0') \
++ optarg = NULL; \
++ if (optarg != NULL) \
++ val = strtoul(optarg, &ep, base); \
++ if (optarg == NULL || *ep != '\0') { \
++ struct sbuf *sb = sbuf_new_auto(); \
++ sbuf_printf(sb, "Invalid %s: \"%s\"", optname, \
++ optarg); \
++ sbuf_finish(sb); \
++ vfs_mount_error(mp, sbuf_data(sb)); \
++ sbuf_delete(sb); \
++ return (EINVAL); \
++ } \
++ } \
++} while (0)
+
+- /** @todo root vnode. */
++static int
++vboxfs_node_ctor(void *mem, int size, void *arg, int flags)
++{
++ struct vboxfs_node *node = (struct vboxfs_node *)mem;
+
+- vfs_getnewfsid(mp);
+- vfs_mountedfrom(mp, pszShare);
++ node->sf_vnode = NULL;
++ node->sf_vpstate = 0;
+
+- printf("%s: Leave rc=0\n", __FUNCTION__);
++ return (0);
++}
+
+- return 0;
++static void
++vboxfs_node_dtor(void *mem, int size, void *arg)
++{
++ struct vboxfs_node *node = (struct vboxfs_node *)mem;
++ node->sf_type = VNON;
+ }
+
+-static int vboxvfs_unmount(struct mount *mp, int mntflags, struct thread *td)
++static int
++vboxfs_node_init(void *mem, int size, int flags)
+ {
+- struct sf_glob_info *pShFlGlobalInfo = VFSMP2SFGLOBINFO(mp);
+- int rc;
+- int flags = 0;
++ struct vboxfs_node *node = (struct vboxfs_node *)mem;
++ node->sf_ino = 0;
+
+- rc = VbglR0SfUnmapFolder(&g_vboxSFClient, &pShFlGlobalInfo->map);
+- if (RT_FAILURE(rc))
+- printf("Failed to unmap shared folder\n");
++ mtx_init(&node->sf_interlock, "tmpfs node interlock", NULL, MTX_DEF);
+
+- if (mntflags & MNT_FORCE)
+- flags |= FORCECLOSE;
++ return (0);
++}
+
+- /* There is 1 extra root vnode reference (vnode_root). */
+- rc = vflush(mp, 1, flags, td);
+- if (rc)
+- return rc;
++static void
++vboxfs_node_fini(void *mem, int size)
++{
++ struct vboxfs_node *node = (struct vboxfs_node *)mem;
+
++ mtx_destroy(&node->sf_interlock);
++}
+
+- RTMemFree(pShFlGlobalInfo);
+- mp->mnt_data = NULL;
++static int
++vboxfs_mount(struct mount *mp)
++{
++ struct vboxfs_mnt *vboxfsmp = NULL;
++ struct vfsoptlist *opts = mp->mnt_optnew;
++ sfp_mount_t *handle = NULL;
++ int readonly = 0;
++ sffs_fsinfo_t fsinfo;
++ int error, share_len;
++ char *share_name;
++ mode_t file_mode = 0, dir_mode = 0;
++ uid_t uid = 0;
++ gid_t gid = 0;
++ struct vboxfs_node *root;
++
++ if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
++ return (EOPNOTSUPP);
++
++ if (vfs_filteropt(opts, vboxfs_opts)) {
++ vfs_mount_error(mp, "%s", "Invalid option");
++ return (EINVAL);
++ }
++
++ VBOX_INTOPT("uid", uid, 10);
++ VBOX_INTOPT("gid", gid, 10);
++ VBOX_INTOPT("file_mode", file_mode, 8);
++ VBOX_INTOPT("dir_mode", dir_mode, 8);
++ VBOX_INTOPT("ro", readonly, 10);
++
++ error = vfs_getopt(opts, "from", (void **)&share_name, &share_len);
++ if (error != 0 || share_len == 0) {
++ vfs_mount_error(mp, "Invalid from");
++ return (EINVAL);
++ }
++
++ vboxfsmp = malloc(sizeof(struct vboxfs_mnt), M_VBOXVFS, M_WAITOK | M_ZERO);
++ vboxfsmp->sf_uid = uid;
++ vboxfsmp->sf_gid = gid;
++ vboxfsmp->sf_fmode = file_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
++ vboxfsmp->sf_dmode = dir_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
++ vboxfsmp->sf_ino = 3;
++ vboxfsmp->sf_stat_ttl = 200;
++
++ /* Invoke Hypervisor mount interface before proceeding */
++ error = sfprov_mount(share_name, &handle);
++ if (error)
++ return (error);
++
++ /* Determine whether the filesystem must be read-only. */
++ error = sfprov_get_fsinfo(handle, &fsinfo);
++ if (error != 0) {
++ sfprov_unmount(handle);
++ return (error);
++ }
++ if (readonly == 0)
++ readonly = (fsinfo.readonly != 0);
++
++ vboxfsmp->sf_handle = handle;
++ vboxfsmp->sf_vfsp = mp;
++
++ vboxfsmp->sf_node_pool = uma_zcreate("VBOXFS node",
++ sizeof(struct vboxfs_node),
++ vboxfs_node_ctor, vboxfs_node_dtor,
++ vboxfs_node_init, vboxfs_node_fini,
++ UMA_ALIGN_PTR, 0);
++
++ /* Allocate the root node. */
++ error = vboxfs_alloc_node(mp, vboxfsmp, "", VDIR, 0,
++ 0, 0755, NULL, &root);
++
++ if (error != 0 || root == NULL) {
++ uma_zdestroy(vboxfsmp->sf_node_pool);
++ free(vboxfsmp, M_VBOXVFS);
++ return error;
++ }
++
++ root->sf_parent = root;
++ vboxfsmp->sf_root = root;
++
++ MNT_ILOCK(mp);
++ mp->mnt_data = vboxfsmp;
++ bzero(&mp->mnt_stat.f_fsid, sizeof(&mp->mnt_stat.f_fsid));
++ /* f_fsid is int32_t but serial is uint32_t, convert */
++ memcpy(&mp->mnt_stat.f_fsid, &fsinfo.serial, sizeof(mp->mnt_stat.f_fsid));
++ mp->mnt_flag |= MNT_LOCAL;
++ if (readonly != 0)
++ mp->mnt_flag |= MNT_RDONLY;
++#if __FreeBSD_version >= 1000021
++ mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
++#else
++ mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
++ MNTK_EXTENDED_SHARED;
++#endif
++ MNT_IUNLOCK(mp);
++ vfs_mountedfrom(mp, share_name);
+
+- return 0;
++ return (0);
+ }
+
+-static int vboxvfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
++/*
++ * Unmount a shared folder.
++ *
++ * vboxfs_unmount umounts the mounted file system. It return 0
++ * on sucess and any relevant errno on failure.
++ */
++static int
++vboxfs_unmount(struct mount *mp, int mntflags)
+ {
+- int rc = 0;
+- struct sf_glob_info *pShFlGlobalInfo = VFSMP2SFGLOBINFO(mp);
+- struct vnode *vp;
++ struct vboxfs_mnt *vboxfsmp;
++ struct thread *td;
++ int error;
++ int flags;
++
++ vboxfsmp = VFSTOVBOXFS(mp);
++ td = curthread;
++ flags = 0;
++ if (mntflags & MNT_FORCE)
++ flags |= FORCECLOSE;
++
++ error = vflush(mp, 0, flags, td);
++ if (error)
++ return (error);
++
++ /* Invoke Hypervisor unmount interface before proceeding */
++ error = sfprov_unmount(vboxfsmp->sf_handle);
++ if (error != 0) {
++ /* TBD anything here? */
++ }
++
++ uma_zdestroy(vboxfsmp->sf_node_pool);
++
++ free(vboxfsmp, M_VBOXVFS);
++ MNT_ILOCK(mp);
++ mp->mnt_data = NULL;
++ mp->mnt_flag &= ~MNT_LOCAL;
++ MNT_IUNLOCK(mp);
+
+- printf("%s: Enter\n", __FUNCTION__);
+-
+- vp = pShFlGlobalInfo->vnode_root;
+- VREF(vp);
++ return (0);
++}
+
+- vn_lock(vp, flags | LK_RETRY, td);
+- *vpp = vp;
++static int
++vboxfs_root(struct mount *mp, int flags, struct vnode **vpp)
++{
++ int error;
++ error = vboxfs_alloc_vp(mp, VFSTOVBOXFS(mp)->sf_root, flags, vpp);
+
+- printf("%s: Leave\n", __FUNCTION__);
++ if (!error)
++ (*vpp)->v_vflag |= VV_ROOT;
+
+- return rc;
++ return error;
+ }
+
+-static int vboxvfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct thread *td)
++/*
++ * Do operation associated with quotas, not supported
++ */
++static int
++vboxfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
+ {
+- return EOPNOTSUPP;
++ return (EOPNOTSUPP);
+ }
+
+-int vboxvfs_init(struct vfsconf *vfsp)
++/*
++ * Initialize the filesystem globals.
++ */
++static int
++vboxfs_init(struct vfsconf *vfsp)
+ {
+- int rc;
++ int error;
+
+- /* Initialize the R0 guest library. */
+- rc = VbglR0SfInit();
+- if (RT_FAILURE(rc))
+- return ENXIO;
++ DROP_GIANT();
+
+- /* Connect to the host service. */
+- rc = VbglR0SfConnect(&g_vboxSFClient);
+- if (RT_FAILURE(rc))
+- {
+- printf("Failed to get connection to host! rc=%d\n", rc);
+- VbglR0SfTerm();
+- return ENXIO;
+- }
++ sfprov = sfprov_connect(SFPROV_VERSION);
++ if (sfprov == NULL) {
++ printf("%s: couldn't connect to sf provider", __func__);
++ return (ENODEV);
++ }
++
++ error = sfprov_set_show_symlinks();
++ if (error != 0)
++ printf("%s: host unable to show symlinks, error=%d\n",
++ __func__, error);
+
+- rc = VbglR0SfSetUtf8(&g_vboxSFClient);
+- if (RT_FAILURE (rc))
+- {
+- printf("VbglR0SfSetUtf8 failed, rc=%d\n", rc);
+- VbglR0SfDisconnect(&g_vboxSFClient);
+- VbglR0SfTerm();
+- return EPROTO;
+- }
+-
+- printf("Successfully loaded shared folder module\n");
+-
+- return 0;
++ PICKUP_GIANT();
++ return (0);
+ }
+
+-int vboxvfs_uninit(struct vfsconf *vfsp)
++/*
++ * Undo the work of vboxfs_init().
++ */
++static int
++vboxfs_uninit(struct vfsconf *vfsp)
+ {
+- VbglR0SfDisconnect(&g_vboxSFClient);
+- VbglR0SfTerm();
+
+- return 0;
++ DROP_GIANT();
++ sfprov_disconnect();
++ PICKUP_GIANT();
++ return (0);
+ }
+
+-int vboxvfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
++/*
++ * Get filesystem statistics.
++ */
++static int
++vboxfs_statfs(struct mount *mp, struct statfs *sbp)
+ {
+- return 0;
++ struct vboxfs_mnt *vboxfsmp;
++ sffs_fsinfo_t fsinfo;
++ int error;
++
++ vboxfsmp = VFSTOVBOXFS(mp);
++
++ error = sfprov_get_fsinfo(vboxfsmp->sf_handle, &fsinfo);
++ if (error != 0)
++ return (error);
++
++ sbp->f_iosize = fsinfo.blksize;
++ sbp->f_bsize = fsinfo.blksize;
++
++ sbp->f_bfree = fsinfo.blksavail;
++ sbp->f_bavail = fsinfo.blksavail;
++ sbp->f_files = fsinfo.blksavail / 4; /* some kind of reasonable value */
++ sbp->f_ffree = fsinfo.blksavail / 4;
++
++ sbp->f_blocks = fsinfo.blksused + sbp->f_bavail;
++ sbp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
++ sbp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
++ sbp->f_namemax = fsinfo.maxnamesize;
++
++ return (0);
+ }
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vnops.c b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vnops.c
new file mode 100644
index 000000000000..d159ced798be
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Additions_freebsd_vboxvfs_vboxvfs__vnops.c
@@ -0,0 +1,1510 @@
+--- src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vnops.c.orig 2017-04-28 16:59:22.000000000 +0200
++++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vnops.c 2017-07-14 14:22:09.045026515 +0200
+@@ -1,10 +1,6 @@
+-/* $Id: vboxvfs_vnops.c $ */
+-/** @file
+- * Description.
+- */
+-
+ /*
+ * Copyright (C) 2008-2016 Oracle Corporation
++ * Copyright (C) 2017 Mahdi Mokhtari
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+@@ -14,228 +9,1336 @@
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+-
+-#include "vboxvfs.h"
+ #include <sys/param.h>
+ #include <sys/systm.h>
+ #include <sys/namei.h>
+ #include <sys/kernel.h>
+-#include <sys/proc.h>
++#include <sys/types.h>
++#include <sys/malloc.h>
++#include <sys/stat.h>
+ #include <sys/bio.h>
++#include <sys/conf.h>
+ #include <sys/buf.h>
+-#include <sys/fcntl.h>
++#include <sys/iconv.h>
+ #include <sys/mount.h>
+-#include <sys/unistd.h>
+ #include <sys/vnode.h>
+-#include <sys/limits.h>
+-#include <sys/lockf.h>
+-#include <sys/stat.h>
++#include <sys/dirent.h>
++#include <sys/queue.h>
++#include <sys/unistd.h>
++#include <sys/endian.h>
+
+-#include <vm/vm.h>
+-#include <vm/vm_extern.h>
++#include <vm/uma.h>
++
++#include "vboxvfs.h"
+
+ /*
+ * Prototypes for VBOXVFS vnode operations
+ */
+-static vop_create_t vboxvfs_create;
+-static vop_mknod_t vboxvfs_mknod;
+-static vop_open_t vboxvfs_open;
+-static vop_close_t vboxvfs_close;
+-static vop_access_t vboxvfs_access;
+-static vop_getattr_t vboxvfs_getattr;
+-static vop_setattr_t vboxvfs_setattr;
+-static vop_read_t vboxvfs_read;
+-static vop_write_t vboxvfs_write;
+-static vop_fsync_t vboxvfs_fsync;
+-static vop_remove_t vboxvfs_remove;
+-static vop_link_t vboxvfs_link;
+-static vop_lookup_t vboxvfs_lookup;
+-static vop_rename_t vboxvfs_rename;
+-static vop_mkdir_t vboxvfs_mkdir;
+-static vop_rmdir_t vboxvfs_rmdir;
+-static vop_symlink_t vboxvfs_symlink;
+-static vop_readdir_t vboxvfs_readdir;
+-static vop_strategy_t vboxvfs_strategy;
+-static vop_print_t vboxvfs_print;
+-static vop_pathconf_t vboxvfs_pathconf;
+-static vop_advlock_t vboxvfs_advlock;
+-static vop_getextattr_t vboxvfs_getextattr;
+-static vop_ioctl_t vboxvfs_ioctl;
+-static vop_getpages_t vboxvfs_getpages;
+-static vop_inactive_t vboxvfs_inactive;
+-static vop_putpages_t vboxvfs_putpages;
+-static vop_reclaim_t vboxvfs_reclaim;
+-
+-struct vop_vector vboxvfs_vnodeops = {
+- .vop_default = &default_vnodeops,
+-
+- .vop_access = vboxvfs_access,
+- .vop_advlock = vboxvfs_advlock,
+- .vop_close = vboxvfs_close,
+- .vop_create = vboxvfs_create,
+- .vop_fsync = vboxvfs_fsync,
+- .vop_getattr = vboxvfs_getattr,
+- .vop_getextattr = vboxvfs_getextattr,
+- .vop_getpages = vboxvfs_getpages,
+- .vop_inactive = vboxvfs_inactive,
+- .vop_ioctl = vboxvfs_ioctl,
+- .vop_link = vboxvfs_link,
+- .vop_lookup = vboxvfs_lookup,
+- .vop_mkdir = vboxvfs_mkdir,
+- .vop_mknod = vboxvfs_mknod,
+- .vop_open = vboxvfs_open,
+- .vop_pathconf = vboxvfs_pathconf,
+- .vop_print = vboxvfs_print,
+- .vop_putpages = vboxvfs_putpages,
+- .vop_read = vboxvfs_read,
+- .vop_readdir = vboxvfs_readdir,
+- .vop_reclaim = vboxvfs_reclaim,
+- .vop_remove = vboxvfs_remove,
+- .vop_rename = vboxvfs_rename,
+- .vop_rmdir = vboxvfs_rmdir,
+- .vop_setattr = vboxvfs_setattr,
+- .vop_strategy = vboxvfs_strategy,
+- .vop_symlink = vboxvfs_symlink,
+- .vop_write = vboxvfs_write,
++static vop_create_t vboxfs_create;
++static vop_open_t vboxfs_open;
++static vop_close_t vboxfs_close;
++static vop_access_t vboxfs_access;
++static vop_getattr_t vboxfs_getattr;
++static vop_setattr_t vboxfs_setattr;
++static vop_read_t vboxfs_read;
++static vop_readlink_t vboxfs_readlink;
++static vop_write_t vboxfs_write;
++static vop_fsync_t vboxfs_fsync;
++static vop_remove_t vboxfs_remove;
++static vop_link_t vboxfs_link;
++static vop_cachedlookup_t vboxfs_lookup;
++static vop_rename_t vboxfs_rename;
++static vop_mkdir_t vboxfs_mkdir;
++static vop_rmdir_t vboxfs_rmdir;
++static vop_symlink_t vboxfs_symlink;
++static vop_readdir_t vboxfs_readdir;
++static vop_print_t vboxfs_print;
++static vop_pathconf_t vboxfs_pathconf;
++static vop_advlock_t vboxfs_advlock;
++static vop_ioctl_t vboxfs_ioctl;
++static vop_inactive_t vboxfs_inactive;
++static vop_reclaim_t vboxfs_reclaim;
++static vop_vptofh_t vboxfs_vptofh;
++
++struct vop_vector vboxfs_vnodeops = {
++ .vop_default = &default_vnodeops,
++
++ .vop_access = vboxfs_access,
++ .vop_advlock = VOP_EOPNOTSUPP,
++ .vop_close = vboxfs_close,
++ .vop_create = vboxfs_create,
++ .vop_fsync = vboxfs_fsync,
++ .vop_getattr = vboxfs_getattr,
++ .vop_getextattr = VOP_EOPNOTSUPP,
++ .vop_inactive = vboxfs_inactive,
++ .vop_ioctl = vboxfs_ioctl,
++ .vop_link = vboxfs_link,
++ .vop_lookup = vfs_cache_lookup,
++ .vop_cachedlookup = vboxfs_lookup,
++ .vop_mkdir = vboxfs_mkdir,
++ .vop_mknod = VOP_EOPNOTSUPP,
++ .vop_open = vboxfs_open,
++ .vop_pathconf = vboxfs_pathconf,
++ .vop_print = vboxfs_print,
++ .vop_read = vboxfs_read,
++ .vop_readdir = vboxfs_readdir,
++ .vop_readlink = vboxfs_readlink,
++ .vop_reclaim = vboxfs_reclaim,
++ .vop_remove = vboxfs_remove,
++ .vop_rename = vboxfs_rename,
++ .vop_rmdir = vboxfs_rmdir,
++ .vop_setattr = vboxfs_setattr,
++ .vop_vptofh = vboxfs_vptofh,
++ .vop_symlink = vboxfs_symlink,
++ .vop_write = vboxfs_write,
++ .vop_bmap = VOP_EOPNOTSUPP
+ };
+
+-static int vboxvfs_access(struct vop_access_args *ap)
++static uint64_t
++vsfnode_cur_time_usec(void)
+ {
+- return 0;
+-}
++ struct timeval now;
+
+-static int vboxvfs_open(struct vop_open_args *ap)
+-{
+- return 0;
+-}
++ getmicrotime(&now);
+
+-static int vboxvfs_close(struct vop_close_args *ap)
+-{
+- return 0;
++ return (now.tv_sec*1000 + now.tv_usec);
+ }
+
+-static int vboxvfs_getattr(struct vop_getattr_args *ap)
++static int
++vsfnode_stat_cached(struct vboxfs_node *np)
+ {
+- return 0;
++ return (vsfnode_cur_time_usec() - np->sf_stat_time) <
++ np->vboxfsmp->sf_stat_ttl * 1000UL;
+ }
+
+-static int vboxvfs_setattr(struct vop_setattr_args *ap)
++static int
++vsfnode_update_stat_cache(struct vboxfs_node *np)
+ {
+- return 0;
+-}
++ int error;
+
+-static int vboxvfs_read(struct vop_read_args *ap)
+-{
+- return 0;
+-}
++ error = sfprov_get_attr(np->vboxfsmp->sf_handle, np->sf_path,
++ &np->sf_stat);
++#if 0
++ if (error == ENOENT)
++ sfnode_make_stale(node);
++#endif
++ if (error == 0)
++ np->sf_stat_time = vsfnode_cur_time_usec();
+
+-static int vboxvfs_write(struct vop_write_args *ap)
+-{
+- return 0;
++ return (error);
+ }
+
+-static int vboxvfs_create(struct vop_create_args *ap)
++/*
++ * Need to clear v_object for insmntque failure.
++ */
++static void
++vboxfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
+ {
+- return 0;
+-}
+
+-static int vboxvfs_remove(struct vop_remove_args *ap)
+-{
+- return 0;
++ // XXX: vboxfs_destroy_vobject(vp, vp->v_object);
++ vp->v_object = NULL;
++ vp->v_data = NULL;
++ vp->v_op = &dead_vnodeops;
++ vgone(vp);
++ vput(vp);
+ }
+
+-static int vboxvfs_rename(struct vop_rename_args *ap)
+-{
+- return 0;
+-}
++/*
++ * Allocates a new vnode for the node node or returns a new reference to
++ * an existing one if the node had already a vnode referencing it. The
++ * resulting locked vnode is returned in *vpp.
++ *
++ * Returns zero on success or an appropriate error code on failure.
++ */
++int
++vboxfs_alloc_vp(struct mount *mp, struct vboxfs_node *node, int lkflag,
++ struct vnode **vpp)
++{
++ struct vnode *vp;
++ int error;
++
++ error = 0;
++loop:
++ VBOXFS_NODE_LOCK(node);
++loop1:
++ if ((vp = node->sf_vnode) != NULL) {
++ MPASS((node->sf_vpstate & VBOXFS_VNODE_DOOMED) == 0);
++ VI_LOCK(vp);
++ if ((node->sf_type == VDIR && node->sf_parent == NULL) ||
++ ((vp->v_iflag & VI_DOOMED) != 0 &&
++ (lkflag & LK_NOWAIT) != 0)) {
++ VI_UNLOCK(vp);
++ VBOXFS_NODE_UNLOCK(node);
++ error = ENOENT;
++ vp = NULL;
++ goto out;
++ }
++ if ((vp->v_iflag & VI_DOOMED) != 0) {
++ VI_UNLOCK(vp);
++ node->sf_vpstate |= VBOXFS_VNODE_WRECLAIM;
++ while ((node->sf_vpstate & VBOXFS_VNODE_WRECLAIM) != 0) {
++ msleep(&node->sf_vnode, VBOXFS_NODE_MTX(node),
++ 0, "vsfE", 0);
++ }
++ goto loop1;
++ }
++ VBOXFS_NODE_UNLOCK(node);
++ error = vget(vp, lkflag | LK_INTERLOCK, curthread);
++ if (error == ENOENT)
++ goto loop;
++ if (error != 0) {
++ vp = NULL;
++ goto out;
++ }
++
++ /*
++ * Make sure the vnode is still there after
++ * getting the interlock to avoid racing a free.
++ */
++ if (node->sf_vnode == NULL || node->sf_vnode != vp) {
++ vput(vp);
++ goto loop;
++ }
++
++ goto out;
++ }
++
++ if ((node->sf_vpstate & VBOXFS_VNODE_DOOMED) ||
++ (node->sf_type == VDIR && node->sf_parent == NULL)) {
++ VBOXFS_NODE_UNLOCK(node);
++ error = ENOENT;
++ vp = NULL;
++ goto out;
++ }
++
++ /*
++ * otherwise lock the vp list while we call getnewvnode
++ * since that can block.
++ */
++ if (node->sf_vpstate & VBOXFS_VNODE_ALLOCATING) {
++ node->sf_vpstate |= VBOXFS_VNODE_WANT;
++ error = msleep((caddr_t) &node->sf_vpstate,
++ VBOXFS_NODE_MTX(node), PDROP | PCATCH,
++ "vboxfs_alloc_vp", 0);
++ if (error)
++ return error;
++
++ goto loop;
++ } else
++ node->sf_vpstate |= VBOXFS_VNODE_ALLOCATING;
++
++ VBOXFS_NODE_UNLOCK(node);
++
++ /* Get a new vnode and associate it with our node. */
++ error = getnewvnode("vboxfs", mp, &vboxfs_vnodeops, &vp);
++ if (error != 0)
++ goto unlock;
++ MPASS(vp != NULL);
++
++ /* lkflag is ignored, the lock is exclusive */
++ (void) vn_lock(vp, lkflag | LK_RETRY);
++
++ vp->v_data = node;
++ vp->v_type = node->sf_type;
++
++ /* Type-specific initialization. */
++ switch (node->sf_type) {
++ case VBLK:
++ /* FALLTHROUGH */
++ case VCHR:
++ /* FALLTHROUGH */
++ case VLNK:
++ /* FALLTHROUGH */
++ case VSOCK:
++ /* FALLTHROUGH */
++ case VFIFO:
++ /* FALLTHROUGH */
++ case VREG:
++ break;
++ case VDIR:
++ MPASS(node->sf_parent != NULL);
++ if (node->sf_parent == node)
++ vp->v_vflag |= VV_ROOT;
++ break;
++
++ default:
++ panic("vboxfs_alloc_vp: type %p %d", node, (int)node->sf_type);
++ }
++
++ if (vp->v_type != VFIFO)
++ VN_LOCK_ASHARE(vp);
++
++ error = insmntque1(vp, mp, vboxfs_insmntque_dtr, NULL);
++ if (error)
++ vp = NULL;
++
++unlock:
++ VBOXFS_NODE_LOCK(node);
++
++ MPASS(node->sf_vpstate & VBOXFS_VNODE_ALLOCATING);
++ node->sf_vpstate &= ~VBOXFS_VNODE_ALLOCATING;
++ node->sf_vnode = vp;
++
++ if (node->sf_vpstate & VBOXFS_VNODE_WANT) {
++ node->sf_vpstate &= ~VBOXFS_VNODE_WANT;
++ VBOXFS_NODE_UNLOCK(node);
++ wakeup((caddr_t) &node->sf_vpstate);
++ } else
++ VBOXFS_NODE_UNLOCK(node);
++
++out:
++ *vpp = vp;
++
++#ifdef INVARIANTS
++ if (error == 0) {
++ MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
++ VBOXFS_NODE_LOCK(node);
++ MPASS(*vpp == node->sf_vnode);
++ VBOXFS_NODE_UNLOCK(node);
++ }
++#endif
+
+-static int vboxvfs_link(struct vop_link_args *ap)
+-{
+- return EOPNOTSUPP;
++ return error;
+ }
+
+-static int vboxvfs_symlink(struct vop_symlink_args *ap)
++/*
++ * Destroys the association between the vnode vp and the node it
++ * references.
++ */
++void
++vboxfs_free_vp(struct vnode *vp)
+ {
+- return EOPNOTSUPP;
+-}
++ struct vboxfs_node *node;
+
+-static int vboxvfs_mknod(struct vop_mknod_args *ap)
+-{
+- return EOPNOTSUPP;
+-}
++ node = VP_TO_VBOXFS_NODE(vp);
+
+-static int vboxvfs_mkdir(struct vop_mkdir_args *ap)
+-{
+- return 0;
++ VBOXFS_NODE_ASSERT_LOCKED(node);
++ node->sf_vnode = NULL;
++ if ((node->sf_vpstate & VBOXFS_VNODE_WRECLAIM) != 0)
++ wakeup(&node->sf_vnode);
++ node->sf_vpstate &= ~VBOXFS_VNODE_WRECLAIM;
++ vp->v_data = NULL;
+ }
+
+-static int vboxvfs_rmdir(struct vop_rmdir_args *ap)
++/*
++ * Allocate new vboxfs_node and vnode for given file
++ */
++static int
++vboxfs_alloc_file(struct vboxfs_mnt *vboxfsmp, const char *fullpath,
++ enum vtype type, mode_t mode, struct vboxfs_node *parent,
++ int lkflag, struct vnode **vpp)
+ {
+- return 0;
+-}
++ int error;
++ struct vboxfs_node *unode;
+
+-static int vboxvfs_readdir(struct vop_readdir_args *ap)
+-{
+- return 0;
+-}
++ error = vboxfs_alloc_node(vboxfsmp->sf_vfsp, vboxfsmp, fullpath, type,
++ vboxfsmp->sf_uid, vboxfsmp->sf_gid, mode, parent, &unode);
+
+-static int vboxvfs_fsync(struct vop_fsync_args *ap)
+-{
+- return 0;
+-}
++ if (error)
++ goto out;
+
+-static int vboxvfs_print (struct vop_print_args *ap)
+-{
+- return 0;
+-}
++ error = vboxfs_alloc_vp(vboxfsmp->sf_vfsp, unode, lkflag, vpp);
++ if (error)
++ vboxfs_free_node(vboxfsmp, unode);
+
+-static int vboxvfs_pathconf (struct vop_pathconf_args *ap)
+-{
+- return 0;
++out:
++ return (error);
+ }
+
+-static int vboxvfs_strategy (struct vop_strategy_args *ap)
++static int
++vboxfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
++ struct vnode **rvp)
+ {
+- return 0;
+-}
+
+-static int vboxvfs_ioctl(struct vop_ioctl_args *ap)
+-{
+- return ENOTTY;
++ return (vboxfs_alloc_vp(mp, arg, lkflags, rvp));
+ }
+
+-static int vboxvfs_getextattr(struct vop_getextattr_args *ap)
++/*
++ * Construct a new pathname given an sfnode plus an optional tail
++ * component of length len
++ * This handles ".." and "."
++ */
++static char *
++sfnode_construct_path(struct vboxfs_node *node, char *tail, int len)
+ {
+- return 0;
+-}
++ char *p;
+
+-static int vboxvfs_advlock(struct vop_advlock_args *ap)
+-{
+- return 0;
+-}
++ if (strncmp(tail, ".", len) == 0 || strncmp(tail, "..", len) == 0)
++ panic("construct path for %s", tail);
++ p = malloc(strlen(node->sf_path) + 1 + len + 1, M_VBOXVFS, M_WAITOK);
++ strcpy(p, node->sf_path);
++ strcat(p, "/");
++ strncat(p, tail, len);
++ return (p);
++}
++
++static int
++vboxfs_access(struct vop_access_args *ap)
++{
++ struct vnode *vp = ap->a_vp;
++ accmode_t accmode = ap->a_accmode;
++ struct vboxfs_node *node;
++ int error;
++ mode_t m;
++
++ MPASS(VOP_ISLOCKED(vp));
++
++ node = VP_TO_VBOXFS_NODE(vp);
++
++ if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
++ switch (vp->v_type) {
++ case VDIR:
++ case VLNK:
++ case VREG:
++ return (EROFS);
++ /* NOT REACHED */
++ default:
++ break;
++ }
++ }
++
++ if (vsfnode_stat_cached(node))
++ error = 0;
++ else
++ error = vsfnode_update_stat_cache(node);
++ m = (error == 0) ? node->sf_stat.sf_mode : 0;
+
+-static int vboxvfs_lookup(struct vop_lookup_args *ap)
+-{
+- return 0;
++ return (vaccess(vp->v_type, m, node->vboxfsmp->sf_uid,
++ node->vboxfsmp->sf_gid, accmode, ap->a_cred, NULL));
+ }
+
+-static int vboxvfs_inactive(struct vop_inactive_args *ap)
++/*
++ * Clears the (cached) directory listing for the node.
++ */
++static void
++vfsnode_clear_dir_list(struct vboxfs_node *np)
+ {
+- return 0;
++ while (np->sf_dir_list != NULL) {
++ sffs_dirents_t *next = np->sf_dir_list->sf_next;
++ free(np->sf_dir_list, M_VBOXVFS);
++ np->sf_dir_list = next;
++ }
++}
++
++static int
++vboxfs_open(struct vop_open_args *ap)
++{
++ struct vboxfs_node *np;
++ sfp_file_t *fp;
++ int error;
++
++ MPASS(VOP_ISLOCKED(vp));
++
++ np = VP_TO_VBOXFS_NODE(ap->a_vp);
++ error = sfprov_open(np->vboxfsmp->sf_handle, np->sf_path, &fp);
++ if (error != 0)
++ goto out;
++
++ np->sf_file = fp;
++ vnode_create_vobject(ap->a_vp, 0, ap->a_td);
++
++out:
++ MPASS(VOP_ISLOCKED(vp));
++
++ return (error);
++}
++
++static void
++vfsnode_invalidate_stat_cache(struct vboxfs_node *np)
++{
++ np->sf_stat_time = 0;
++}
++
++static int
++vboxfs_close(struct vop_close_args *ap)
++{
++
++ struct vnode *vp = ap->a_vp;
++ struct vboxfs_node *np;
++
++ np = VP_TO_VBOXFS_NODE(vp);
++
++ /*
++ * Free the directory entries for the node. We do this on this call
++ * here because the directory node may not become inactive for a long
++ * time after the readdir is over. Case in point, if somebody cd's into
++ * the directory then it won't become inactive until they cd away again.
++ * In such a case we would end up with the directory listing not getting
++ * updated (i.e. the result of 'ls' always being the same) until they
++ * change the working directory.
++ */
++ vfsnode_clear_dir_list(np);
++
++ vfsnode_invalidate_stat_cache(np);
++
++ if (np->sf_file != NULL && vp->v_usecount <= 1) {
++ (void) sfprov_close(np->sf_file);
++ np->sf_file = NULL;
++ }
++
++ return (0);
++}
++
++static int
++vboxfs_getattr(struct vop_getattr_args *ap)
++{
++ struct vnode *vp = ap->a_vp;
++ struct vattr *vap = ap->a_vap;
++ struct vboxfs_node *np = VP_TO_VBOXFS_NODE(vp);
++ struct vboxfs_mnt *mp = np->vboxfsmp;
++ mode_t mode;
++ int error = 0;
++
++ mode = 0;
++ vap->va_type = vp->v_type;
++
++ vap->va_nlink = 1; /* number of references to file */
++ vap->va_uid = mp->sf_uid; /* owner user id */
++ vap->va_gid = mp->sf_gid; /* owner group id */
++ vap->va_rdev = NODEV; /* device the special file represents */
++ vap->va_gen = VNOVAL; /* generation number of file */
++ vap->va_flags = 0; /* flags defined for file */
++ vap->va_filerev = 0; /* file modification number */
++ vap->va_vaflags = 0; /* operations flags */
++ vap->va_fileid = np->sf_ino; /* file id */
++ vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
++ if (vap->va_fileid == 0)
++ vap->va_fileid = 2;
++
++ vap->va_atime.tv_sec = VNOVAL;
++ vap->va_atime.tv_nsec = VNOVAL;
++ vap->va_mtime.tv_sec = VNOVAL;
++ vap->va_mtime.tv_nsec = VNOVAL;
++ vap->va_ctime.tv_sec = VNOVAL;
++ vap->va_ctime.tv_nsec = VNOVAL;
++
++ if (!vsfnode_stat_cached(np)) {
++ error = vsfnode_update_stat_cache(np);
++ if (error != 0)
++ goto done;
++ }
++
++ vap->va_atime = np->sf_stat.sf_atime;
++ vap->va_mtime = np->sf_stat.sf_mtime;
++ vap->va_ctime = np->sf_stat.sf_ctime;
++
++ mode = np->sf_stat.sf_mode;
++
++ vap->va_mode = mode;
++ if (S_ISDIR(mode)) {
++ vap->va_type = VDIR; /* vnode type (for create) */
++ vap->va_mode = mp->sf_dmode != 0 ? (mp->sf_dmode & 0777) : vap->va_mode;
++ vap->va_mode &= ~mp->sf_dmask;
++ vap->va_mode |= S_IFDIR;
++ } else if (S_ISREG(mode)) {
++ vap->va_type = VREG;
++ vap->va_mode = mp->sf_fmode != 0 ? (mp->sf_fmode & 0777) : vap->va_mode;
++ vap->va_mode &= ~mp->sf_fmask;
++ vap->va_mode |= S_IFREG;
++ } else if (S_ISFIFO(mode))
++ vap->va_type = VFIFO;
++ else if (S_ISCHR(mode))
++ vap->va_type = VCHR;
++ else if (S_ISBLK(mode))
++ vap->va_type = VBLK;
++ else if (S_ISLNK(mode)) {
++ vap->va_type = VLNK;
++ vap->va_mode = mp->sf_fmode != 0 ? (mp->sf_fmode & 0777) : vap->va_mode;
++ vap->va_mode &= ~mp->sf_fmask;
++ vap->va_mode |= S_IFLNK;
++ } else if (S_ISSOCK(mode))
++ vap->va_type = VSOCK;
++
++ vap->va_size = np->sf_stat.sf_size;
++ vap->va_blocksize = 512;
++ /* bytes of disk space held by file */
++ vap->va_bytes = (np->sf_stat.sf_alloc + 511) / 512;
++
++done:
++ return (error);
++}
++
++static int
++vboxfs_setattr(struct vop_setattr_args *ap)
++{
++
++ struct vnode *vp = ap->a_vp;
++ struct vattr *vap = ap->a_vap;
++ struct vboxfs_node *np = VP_TO_VBOXFS_NODE(vp);
++ int error;
++ mode_t mode;
++
++ mode = vap->va_mode;
++ if (vp->v_type == VREG)
++ mode |= S_IFREG;
++ else if (vp->v_type == VDIR)
++ mode |= S_IFDIR;
++ else if (vp->v_type == VBLK)
++ mode |= S_IFBLK;
++ else if (vp->v_type == VCHR)
++ mode |= S_IFCHR;
++ else if (vp->v_type == VLNK)
++ mode |= S_IFLNK;
++ else if (vp->v_type == VFIFO)
++ mode |= S_IFIFO;
++ else if (vp->v_type == VSOCK)
++ mode |= S_IFSOCK;
++
++ vfsnode_invalidate_stat_cache(np);
++
++ error = sfprov_set_attr(np->vboxfsmp->sf_handle, np->sf_path,
++ mode, vap->va_atime, vap->va_mtime, vap->va_ctime);
++#if 0
++ if (error == ENOENT)
++ sfnode_make_stale(np);
++#endif
++ if (vap->va_size != (u_quad_t)VNOVAL) {
++ switch (vp->v_type) {
++ case VDIR:
++ return (EISDIR);
++ case VLNK:
++ /* FALLTHROUGH */
++ case VREG:
++ error = sfprov_set_size(np->vboxfsmp->sf_handle, np->sf_path, vap->va_size);
++ break;
++ case VCHR:
++ /* FALLTHROUGH */
++ case VBLK:
++ /* FALLTHROUGH */
++ case VSOCK:
++ /* FALLTHROUGH */
++ case VFIFO:
++ /* FALLTHROUGH */
++ case VNON:
++ /* FALLTHROUGH */
++ case VBAD:
++ /* FALLTHROUGH */
++ case VMARKER:
++ return (0);
++ }
++ }
++
++ return (error);
++}
++
++#define blkoff(vboxfsmp, loc) ((loc) & (vboxfsmp)->bmask)
++
++static int
++vboxfs_read(struct vop_read_args *ap)
++{
++ struct vnode *vp = ap->a_vp;
++ struct uio *uio = ap->a_uio;
++ struct vboxfs_node *np = VP_TO_VBOXFS_NODE(vp);
++ int error = 0;
++ uint32_t bytes;
++ uint32_t done;
++ unsigned long offset;
++ ssize_t total;
++ void *tmpbuf;
++
++ if (vp->v_type == VDIR)
++ return (EISDIR);
++
++ if (vp->v_type != VREG)
++ return (EINVAL);
++
++ if (uio->uio_offset < 0)
++ return (EINVAL);
++
++ total = uio->uio_resid;
++ if (total == 0)
++ return (0);
++
++ /*
++ * XXXGONZO: this is just to get things working
++ * should be optimized
++ */
++ tmpbuf = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK, 0, ~0, PAGE_SIZE, 0);
++ if (tmpbuf == 0)
++ return (ENOMEM);
++
++ do {
++ offset = uio->uio_offset;
++ done = bytes = min(PAGE_SIZE, uio->uio_resid);
++ error = sfprov_read(np->sf_file, tmpbuf,
++ offset, &done, 0);
++ if (error == 0 && done > 0)
++ error = uiomove(tmpbuf, done, uio);
++ } while (error == 0 && uio->uio_resid > 0 && done > 0);
++
++ contigfree(tmpbuf, PAGE_SIZE, M_DEVBUF);
++
++ /* a partial read is never an error */
++ if (total != uio->uio_resid)
++ error = 0;
++
++ return (error);
++}
++
++static int
++vboxfs_write(struct vop_write_args *ap)
++{
++ struct vnode *vp = ap->a_vp;
++ struct uio *uio = ap->a_uio;
++ struct vboxfs_node *np = VP_TO_VBOXFS_NODE(vp);
++ int error = 0;
++ uint32_t bytes;
++ uint32_t done;
++ unsigned long offset;
++ ssize_t total;
++ void *tmpbuf;
++
++ if (vp->v_type == VDIR)
++ return (EISDIR);
++
++ if (vp->v_type != VREG)
++ return (EINVAL);
++
++ if (uio->uio_offset < 0)
++ return (EINVAL);
++
++ total = uio->uio_resid;
++ if (total == 0)
++ return (0);
++
++ /*
++ * XXXGONZO: this is just to get things working
++ * should be optimized
++ */
++ tmpbuf = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK, 0, ~0, PAGE_SIZE, 0);
++ if (tmpbuf == 0)
++ return (ENOMEM);
++
++ do {
++ offset = uio->uio_offset;
++ bytes = min(PAGE_SIZE, uio->uio_resid);
++ error = uiomove(tmpbuf, bytes, uio);
++ if (error != 0)
++ break;
++ done = bytes;
++ error = sfprov_write(np->sf_file, tmpbuf,
++ offset, &done, 0);
++ if (error != 0)
++ break;
++ total -= done;
++ if (done != bytes)
++ uio->uio_resid += bytes - done;
++ } while (error == 0 && uio->uio_resid > 0 && done > 0);
++
++ contigfree(tmpbuf, PAGE_SIZE, M_DEVBUF);
++
++ /* a partial write is never an error */
++ if (total != uio->uio_resid)
++ error = 0;
++
++ return (error);
++}
++
++static int
++vboxfs_create(struct vop_create_args *ap)
++{
++ struct vnode *dvp = ap->a_dvp;
++ struct vnode **vpp = ap->a_vpp;
++ struct componentname *cnp = ap->a_cnp;
++ struct vattr *vap = ap->a_vap;
++ sffs_stat_t stat;
++ char *fullpath = NULL;
++ struct vboxfs_node *dir = VP_TO_VBOXFS_NODE(dvp);
++ sfp_file_t *fp;
++ int error;
++ struct vboxfs_mnt *vboxfsmp = dir->vboxfsmp;
++
++ MPASS(vap->va_type == VREG);
++
++ fullpath = sfnode_construct_path(dir, cnp->cn_nameptr, cnp->cn_namelen);
++ error = sfprov_create(dir->vboxfsmp->sf_handle, fullpath, vap->va_mode,
++ &fp, &stat);
++
++ if (error)
++ goto out;
++
++ error = vboxfs_alloc_file(vboxfsmp, fullpath, VREG, vap->va_mode, dir, cnp->cn_lkflags, vpp);
++
++out:
++ if (fullpath)
++ free(fullpath, M_VBOXVFS);
++
++ if (error == 0) {
++ vfsnode_clear_dir_list(dir);
++ if ((cnp->cn_flags & MAKEENTRY) != 0)
++ cache_enter(dvp, *vpp, cnp);
++ }
++
++ return (error);
++}
++
++static int
++vboxfs_remove(struct vop_remove_args *ap)
++{
++ struct vnode *dvp = ap->a_dvp;
++ struct vnode *vp = ap->a_vp;
++ struct vboxfs_node *np, *dir;
++
++ int error;
++
++ MPASS(VOP_ISLOCKED(dvp));
++ MPASS(VOP_ISLOCKED(vp));
++
++ error = 0;
++
++ np = VP_TO_VBOXFS_NODE(vp);
++ dir = VP_TO_VBOXFS_NODE(vp);
++
++ /*
++ * If anything else is using this vnode, then fail the remove.
++ * Why? Windows hosts can't sfprov_remove() a file that is open,
++ * so we have to sfprov_close() it first.
++ * There is no errno for this - since it's not a problem on UNIX,
++ * but ETXTBSY is the closest.
++ */
++ if (np->sf_file != NULL) {
++ if (vp->v_usecount > 1) {
++ error = ETXTBSY;
++ goto out;
++ }
++ sfprov_close(np->sf_file);
++ np->sf_file = NULL;
++ }
++
++ error = sfprov_remove(np->vboxfsmp->sf_handle, np->sf_path,
++ np->sf_type == VLNK);
++
++#if 0
++ if (error == ENOENT || error == 0)
++ sfnode_make_stale(np);
++#endif
++
++ if (error == 0)
++ vfsnode_clear_dir_list(dir);
++
++out:
++ return (error);
++}
++
++static int
++vboxfs_rename(struct vop_rename_args *ap)
++{
++ struct vnode *fvp;
++ struct vnode *fdvp;
++ struct vnode *tvp;
++ struct vnode *tdvp;
++ struct componentname *fcnp;
++ struct componentname *tcnp;
++ struct vboxfs_node *np;
++ int ret;
++
++ fvp = ap->a_fvp;
++ fdvp = ap->a_fdvp;
++ tvp = ap->a_tvp;
++ tdvp = ap->a_tdvp;
++ fcnp = ap->a_fcnp;
++ tcnp = ap->a_tcnp;
++
++ /* Check for cross-device rename */
++ if ((fvp->v_mount != tdvp->v_mount) ||
++ (tvp && (fvp->v_mount != tvp->v_mount))) {
++ ret = EXDEV;
++ goto out;
++ }
++ np = VP_TO_VBOXFS_NODE(fvp);
++ if (np == NULL)
++ return (0);
++ ret = sfprov_rename(np->vboxfsmp->sf_handle,
++ fcnp->cn_nameptr, tcnp->cn_nameptr, fvp->v_type == VDIR);
++out:
++ if (tdvp == tvp)
++ vrele(tdvp);
++ else
++ vput(tdvp);
++ if (tvp)
++ vput(tvp);
++ vrele(fdvp);
++ vrele(fvp);
++ return (ret);
++}
++
++static int
++vboxfs_link(struct vop_link_args *ap)
++{
++ return (EOPNOTSUPP);
++}
++
++static int
++vboxfs_symlink(struct vop_symlink_args *ap)
++{
++ struct vnode *dvp = ap->a_dvp;
++ struct vnode **vpp = ap->a_vpp;
++ struct componentname *cnp = ap->a_cnp;
++ struct vattr *vap = ap->a_vap;
++ sffs_stat_t stat;
++ char *fullpath = NULL;
++ struct vboxfs_node *dir = VP_TO_VBOXFS_NODE(dvp);
++ int error;
++ struct vboxfs_mnt *vboxfsmp = dir->vboxfsmp;
++
++ MPASS(vap->va_type == VLNK);
++
++ fullpath = sfnode_construct_path(dir, cnp->cn_nameptr, cnp->cn_namelen);
++ error = sfprov_symlink(dir->vboxfsmp->sf_handle, fullpath, ap->a_target, &stat);
++
++ if (error)
++ goto out;
++
++ error = vboxfs_alloc_file(vboxfsmp, fullpath, VLNK, vap->va_mode, dir, cnp->cn_lkflags, vpp);
++
++out:
++ if (fullpath)
++ free(fullpath, M_VBOXVFS);
++
++ if (error == 0)
++ vfsnode_clear_dir_list(dir);
++
++ return (error);
++}
++
++static int
++vboxfs_mkdir(struct vop_mkdir_args *ap)
++{
++ struct vnode *dvp = ap->a_dvp;
++ struct vnode **vpp = ap->a_vpp;
++ struct componentname *cnp = ap->a_cnp;
++ struct vattr *vap = ap->a_vap;
++ sffs_stat_t stat;
++ char *fullpath = NULL;
++ struct vboxfs_node *dir = VP_TO_VBOXFS_NODE(dvp);
++ sfp_file_t *fp;
++ int error;
++ struct vboxfs_mnt *vboxfsmp = dir->vboxfsmp;
++
++ MPASS(vap->va_type == VDIR);
++
++ fullpath = sfnode_construct_path(dir, cnp->cn_nameptr, cnp->cn_namelen);
++ error = sfprov_mkdir(dir->vboxfsmp->sf_handle, fullpath, vap->va_mode,
++ &fp, &stat);
++
++ if (error)
++ goto out;
++
++ error = vboxfs_alloc_file(vboxfsmp, fullpath, VDIR, vap->va_mode, dir, cnp->cn_lkflags, vpp);
++
++out:
++ if (fullpath)
++ free(fullpath, M_VBOXVFS);
++
++ if (error == 0)
++ vfsnode_clear_dir_list(dir);
++
++ return (error);
++}
++
++static int
++vboxfs_rmdir(struct vop_rmdir_args *ap)
++{
++ struct vnode *dvp = ap->a_dvp;
++ struct vnode *vp = ap->a_vp;
++ struct vboxfs_node *np, *dir;
++
++ int error;
++
++ MPASS(VOP_ISLOCKED(dvp));
++ MPASS(VOP_ISLOCKED(vp));
++
++ error = 0;
++
++ np = VP_TO_VBOXFS_NODE(vp);
++ dir = VP_TO_VBOXFS_NODE(vp);
++
++ /*
++ * If anything else is using this vnode, then fail the remove.
++ * Why? Windows hosts can't sfprov_remove() a file that is open,
++ * so we have to sfprov_close() it first.
++ * There is no errno for this - since it's not a problem on UNIX,
++ * but ETXTBSY is the closest.
++ */
++ if (np->sf_file != NULL) {
++ if (vp->v_usecount > 1) {
++ error = ETXTBSY;
++ goto out;
++ }
++ sfprov_close(np->sf_file);
++ np->sf_file = NULL;
++ }
++
++ error = sfprov_rmdir(np->vboxfsmp->sf_handle, np->sf_path);
++
++#if 0
++ if (error == ENOENT || error == 0)
++ sfnode_make_stale(np);
++#endif
++
++ if (error == 0)
++ vfsnode_clear_dir_list(dir);
++
++out:
++ return (error);
++}
++
++static int
++vboxfs_readdir(struct vop_readdir_args *ap)
++{
++ int *eofp = ap->a_eofflag;
++ struct vnode *vp = ap->a_vp;
++ struct uio *uio = ap->a_uio;
++ struct vboxfs_node *dir = VP_TO_VBOXFS_NODE(vp);
++ struct vboxfs_node *node;
++ struct sffs_dirent *dirent = NULL;
++ sffs_dirents_t *cur_buf;
++ off_t offset = 0;
++ off_t orig_off = uio->uio_offset;
++ int error = 0;
++ int dummy_eof;
++
++ if (vp->v_type != VDIR)
++ return (ENOTDIR);
++
++ if (eofp == NULL)
++ eofp = &dummy_eof;
++ *eofp = 0;
++
++ /*
++ * Get the directory entry names from the host. This gets all
++ * entries. These are stored in a linked list of sffs_dirents_t
++ * buffers, each of which contains a list of dirent64_t's.
++ */
++ if (dir->sf_dir_list == NULL) {
++ error = sfprov_readdir(dir->vboxfsmp->sf_handle, dir->sf_path,
++ &dir->sf_dir_list);
++ if (error != 0)
++ goto done;
++ }
++
++ /*
++ * Validate and skip to the desired offset.
++ */
++ cur_buf = dir->sf_dir_list;
++ offset = 0;
++
++ while (cur_buf != NULL && offset + cur_buf->sf_len <= uio->uio_offset) {
++ offset += cur_buf->sf_len;
++ cur_buf = cur_buf->sf_next;
++ }
++
++ if (cur_buf == NULL && offset != uio->uio_offset) {
++ error = EINVAL;
++ goto done;
++ }
++
++ if (cur_buf != NULL && offset != uio->uio_offset) {
++ off_t off = offset;
++ int step;
++ dirent = &cur_buf->sf_entries[0];
++
++ while (off < uio->uio_offset) {
++ if (dirent->sf_off == uio->uio_offset)
++ break;
++ step = sizeof(struct sffs_dirent) + dirent->sf_entry.d_reclen;
++ dirent = (struct sffs_dirent *) (((char *) dirent) + step);
++ off += step;
++ }
++
++ if (off >= uio->uio_offset) {
++ error = EINVAL;
++ goto done;
++ }
++ }
++
++ offset = uio->uio_offset - offset;
++
++ /*
++ * Lookup each of the names, so that we have ino's, and copy to
++ * result buffer.
++ */
++ while (cur_buf != NULL) {
++ if (offset >= cur_buf->sf_len) {
++ cur_buf = cur_buf->sf_next;
++ offset = 0;
++ continue;
++ }
++
++ dirent = (struct sffs_dirent *)
++ (((char *) &cur_buf->sf_entries[0]) + offset);
++ if (dirent->sf_entry.d_reclen > uio->uio_resid)
++ break;
++
++ if (strcmp(dirent->sf_entry.d_name, ".") == 0) {
++ node = dir;
++ } else if (strcmp(dirent->sf_entry.d_name, "..") == 0) {
++ node = dir->sf_parent;
++ if (node == NULL)
++ node = dir;
++ } else {
++#if 0
++ node = vsfnode_lookup(dir, dirent->sf_entry.d_name, VNON,
++ 0, &dirent->sf_stat, vsfnode_cur_time_usec(), NULL);
++ if (node == NULL)
++ panic("sffs_readdir() lookup failed");
++#endif
++ }
++
++ if (node)
++ dirent->sf_entry.d_fileno = node->sf_ino;
++ else
++ dirent->sf_entry.d_fileno = 0xdeadbeef;
++
++ error = uiomove(&dirent->sf_entry, dirent->sf_entry.d_reclen, uio);
++ if (error != 0)
++ break;
++
++ uio->uio_offset = dirent->sf_off;
++ offset += sizeof(struct sffs_dirent) + dirent->sf_entry.d_reclen;
++ }
++
++ if (error == 0 && cur_buf == NULL)
++ *eofp = 1;
++done:
++ if (error != 0)
++ uio->uio_offset = orig_off;
++ return (error);
++}
++
++static int
++vboxfs_readlink(struct vop_readlink_args *v)
++{
++ struct vnode *vp = v->a_vp;
++ struct uio *uio = v->a_uio;
++
++ int error;
++ struct vboxfs_node *np;
++ void *tmpbuf;
++
++ MPASS(uio->uio_offset == 0);
++ MPASS(vp->v_type == VLNK);
++
++ np = VP_TO_VBOXFS_NODE(vp);
++
++ tmpbuf = contigmalloc(MAXPATHLEN, M_DEVBUF, M_WAITOK, 0, ~0, 1, 0);
++ if (tmpbuf == NULL)
++ return (ENOMEM);
++
++ error = sfprov_readlink(np->vboxfsmp->sf_handle, np->sf_path, tmpbuf,
++ MAXPATHLEN);
++ if (error)
++ goto done;
++
++ error = uiomove(tmpbuf, strlen(tmpbuf), uio);
++
++done:
++ if (tmpbuf)
++ contigfree(tmpbuf, MAXPATHLEN, M_DEVBUF);
++ return (error);
++}
++
++static int
++vboxfs_fsync(struct vop_fsync_args *ap)
++{
++ struct vnode *vp;
++ struct vboxfs_node *np;
++ int ret;
++
++ vp = ap->a_vp;
++ np = VP_TO_VBOXFS_NODE(vp);
++ if (np == NULL)
++ return (0);
++ ret = sfprov_fsync(np->sf_file);
++ return (ret);
++}
++
++static int
++vboxfs_print(struct vop_print_args *ap)
++{
++ struct vnode *vp = ap->a_vp;
++ struct vboxfs_node *np;
++
++ np = VP_TO_VBOXFS_NODE(vp);
++
++ if (np == NULL) {
++ printf("No vboxfs_node data\n");
++ return (0);
++ }
++
++ printf("\tpath = %s, parent = %p", np->sf_path,
++ np->sf_parent ? np->sf_parent : NULL);
++ printf("\n");
++ return (0);
++}
++
++static int
++vboxfs_pathconf(struct vop_pathconf_args *ap)
++{
++ register_t *retval = ap->a_retval;
++ int error = 0;
++
++ switch (ap->a_name) {
++ case _PC_LINK_MAX:
++ *retval = 65535;
++ break;
++ case _PC_NAME_MAX:
++ *retval = NAME_MAX;
++ break;
++ case _PC_PATH_MAX:
++ *retval = PATH_MAX;
++ break;
++ default:
++ error = EINVAL;
++ break;
++ }
++ return (error);
+ }
+
+-static int vboxvfs_reclaim(struct vop_reclaim_args *ap)
++/*
++ * File specific ioctls.
++ */
++static int
++vboxfs_ioctl(struct vop_ioctl_args *ap)
+ {
+- return 0;
++ return (ENOTTY);
+ }
+
+-static int vboxvfs_getpages(struct vop_getpages_args *ap)
+-{
+- return 0;
++/*
++ * Lookup an entry in a directory and create a new vnode if found.
++ */
++static int
++vboxfs_lookup(struct vop_cachedlookup_args /* {
++ struct vnodeop_desc *a_desc;
++ struct vnode *a_dvp;
++ struct vnode **a_vpp;
++ struct componentname *a_cnp;
++ } */ *ap)
++{
++ struct componentname *cnp = ap->a_cnp;
++ struct vnode *dvp = ap->a_dvp; /* the directory vnode */
++ char *nameptr = cnp->cn_nameptr; /* the name of the file or directory */
++ struct vnode **vpp = ap->a_vpp; /* the vnode we found or NULL */
++ struct vnode *tdp = NULL;
++ struct vboxfs_node *node = VP_TO_VBOXFS_NODE(dvp);
++ struct vboxfs_mnt *vboxfsmp = node->vboxfsmp;
++ u_long nameiop = cnp->cn_nameiop;
++ u_long flags = cnp->cn_flags;
++ sffs_stat_t stat;
++ //long namelen;
++ ino_t id = 0;
++ int ltype, type, error = 0;
++ int lkflags = cnp->cn_lkflags;
++ char *fullpath = NULL;
++
++ error = ENOENT;
++ if (cnp->cn_flags & ISDOTDOT) {
++ error = vn_vget_ino_gen(dvp, vboxfs_vn_get_ino_alloc,
++ node->sf_parent, cnp->cn_lkflags, vpp);
++ error = ENOENT;
++ if (error != 0)
++ goto out;
++
++ } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
++ VREF(dvp);
++ *vpp = dvp;
++ error = 0;
++ } else {
++ mode_t m;
++ type = VNON;
++ fullpath = sfnode_construct_path(node, cnp->cn_nameptr, cnp->cn_namelen);
++ error = sfprov_get_attr(node->vboxfsmp->sf_handle,
++ fullpath, &stat);
++ // stat_time = vsfnode_cur_time_usec();
++
++ m = stat.sf_mode;
++ if (error != 0) {
++ /* The entry was not found in the directory.
++ * This is OK if we are creating or renaming an
++ * entry and are working on the last component of
++ * the path name. */
++ if ((cnp->cn_flags & ISLASTCN) &&
++ (cnp->cn_nameiop == CREATE || \
++ cnp->cn_nameiop == RENAME ||
++ (cnp->cn_nameiop == DELETE &&
++ cnp->cn_flags & DOWHITEOUT &&
++ cnp->cn_flags & ISWHITEOUT))) {
++ error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
++ cnp->cn_thread);
++ if (error != 0)
++ goto out;
++
++ /* Keep the component name in the buffer for
++ * future uses. */
++ cnp->cn_flags |= SAVENAME;
++
++ error = EJUSTRETURN;
++ } else
++ error = ENOENT;
++ }
++ else {
++ if (S_ISDIR(m))
++ type = VDIR;
++ else if (S_ISREG(m))
++ type = VREG;
++ else if (S_ISLNK(m))
++ type = VLNK;
++ error = vboxfs_alloc_file(vboxfsmp, fullpath, type, 0755, node, cnp->cn_lkflags, vpp);
++ }
++ }
++
++ if ((cnp->cn_flags & MAKEENTRY) != 0)
++ cache_enter(dvp, *vpp, cnp);
++out:
++ if (fullpath)
++ free(fullpath, M_VBOXVFS);
++
++ return (error);
++}
++
++static int
++vboxfs_inactive(struct vop_inactive_args *ap)
++{
++ return (0);
++}
++
++static int
++vboxfs_reclaim(struct vop_reclaim_args *ap)
++{
++ struct vnode *vp;
++ struct vboxfs_node *node;
++ struct vboxfs_mnt *vboxfsmp;
++
++ vp = ap->a_vp;
++ node = VP_TO_VBOXFS_NODE(vp);
++ vboxfsmp = node->vboxfsmp;
++
++ vnode_destroy_vobject(vp);
++ vp->v_object = NULL;
++ cache_purge(vp);
++
++ VBOXFS_NODE_LOCK(node);
++ VBOXFS_ASSERT_ELOCKED(node);
++ vboxfs_free_vp(vp);
++
++ /* If the node referenced by this vnode was deleted by the user,
++ * we must free its associated data structures (now that the vnode
++ * is being reclaimed). */
++ if ((node->sf_vpstate & VBOXFS_VNODE_ALLOCATING) == 0) {
++ node->sf_vpstate = VBOXFS_VNODE_DOOMED;
++ VBOXFS_NODE_UNLOCK(node);
++ vboxfs_free_node(vboxfsmp, node);
++ } else
++ VBOXFS_NODE_UNLOCK(node);
++
++ MPASS(vp->v_data == NULL);
++
++ return (0);
+ }
+
+-static int vboxvfs_putpages(struct vop_putpages_args *ap)
++static int
++vboxfs_vptofh(struct vop_vptofh_args *ap)
+ {
+- return 0;
+-}
+
++ return (EOPNOTSUPP);
++}
diff --git a/emulators/virtualbox-ose/files/patch-src_VBox_Runtime_r0drv_freebsd_memobj-r0drv-freebsd.c b/emulators/virtualbox-ose/files/patch-src_VBox_Runtime_r0drv_freebsd_memobj-r0drv-freebsd.c
new file mode 100644
index 000000000000..3e40689685d3
--- /dev/null
+++ b/emulators/virtualbox-ose/files/patch-src_VBox_Runtime_r0drv_freebsd_memobj-r0drv-freebsd.c
@@ -0,0 +1,162 @@
+--- src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c.orig 2017-04-28 15:04:48 UTC
++++ src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c
+@@ -121,16 +121,15 @@ DECLHIDDEN(int) rtR0MemObjNativeFree(RTR
+
+ case RTR0MEMOBJTYPE_LOCK:
+ {
+- vm_map_t pMap = kernel_map;
+-
+- if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
+- pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
++ if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS) {
++ vm_map_t pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
+
+- rc = vm_map_unwire(pMap,
++ rc = vm_map_unwire(pMap,
+ (vm_offset_t)pMemFreeBSD->Core.pv,
+ (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb,
+ VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
+- AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
++ AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
++ }
+ break;
+ }
+
+@@ -224,18 +223,23 @@ static vm_page_t rtR0MemObjFreeBSDContig
+ #else
+ VM_OBJECT_UNLOCK(pObject);
+ #endif
+- if (pPages)
++ if (pPages || cTries >= 1)
+ break;
++#if __FreeBSD_version >= 1100092
++ if (!vm_page_reclaim_contig(fFlags, cPages, 0, VmPhysAddrHigh, uAlignment, 0))
++ break;
++#elif __FreeBSD_version >= 1000015
+ vm_pageout_grow_cache(cTries, 0, VmPhysAddrHigh);
++#else
++ vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
++#endif
+ cTries++;
+ }
+-
+- return pPages;
+ #else
+- while (cTries <= 1)
++ while (1)
+ {
+ pPages = vm_phys_alloc_contig(cPages, 0, VmPhysAddrHigh, uAlignment, 0);
+- if (pPages)
++ if (pPages || cTries >= 1)
+ break;
+ vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
+ cTries++;
+@@ -243,11 +247,8 @@ static vm_page_t rtR0MemObjFreeBSDContig
+
+ if (!pPages)
+ return pPages;
+-#if __FreeBSD_version >= 1000030
+- VM_OBJECT_WLOCK(pObject);
+-#else
++
+ VM_OBJECT_LOCK(pObject);
+-#endif
+ for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
+ {
+ vm_page_t pPage = pPages + iPage;
+@@ -259,13 +260,9 @@ static vm_page_t rtR0MemObjFreeBSDContig
+ atomic_add_int(&cnt.v_wire_count, 1);
+ }
+ }
+-#if __FreeBSD_version >= 1000030
+- VM_OBJECT_WUNLOCK(pObject);
+-#else
+ VM_OBJECT_UNLOCK(pObject);
+ #endif
+ return pPages;
+-#endif
+ }
+
+ static int rtR0MemObjFreeBSDPhysAllocHelper(vm_object_t pObject, u_long cPages,
+@@ -292,16 +289,17 @@ static int rtR0MemObjFreeBSDPhysAllocHel
+ #else
+ VM_OBJECT_LOCK(pObject);
+ #endif
++
+ while (iPage-- > 0)
+ {
+ pPage = vm_page_lookup(pObject, iPage);
+-#if __FreeBSD_version < 1000000
++#if __FreeBSD_version < 900000
+ vm_page_lock_queues();
+ #endif
+ if (fWire)
+ vm_page_unwire(pPage, 0);
+ vm_page_free(pPage);
+-#if __FreeBSD_version < 1000000
++#if __FreeBSD_version < 900000
+ vm_page_unlock_queues();
+ #endif
+ }
+@@ -519,14 +517,19 @@ static int rtR0MemObjNativeLockInMap(PPR
+ if (!pMemFreeBSD)
+ return VERR_NO_MEMORY;
+
+- /*
+- * We could've used vslock here, but we don't wish to be subject to
+- * resource usage restrictions, so we'll call vm_map_wire directly.
+- */
+- rc = vm_map_wire(pVmMap, /* the map */
+- AddrStart, /* start */
+- AddrStart + cb, /* end */
+- fFlags); /* flags */
++ if (pVmMap != kernel_map) {
++ /*
++ * We could've used vslock here, but we don't wish to be subject to
++ * resource usage restrictions, so we'll call vm_map_wire directly.
++ */
++ rc = vm_map_wire(pVmMap, /* the map */
++ AddrStart, /* start */
++ AddrStart + cb, /* end */
++ fFlags); /* flags */
++ }
++ else
++ rc = KERN_SUCCESS;
++
+ if (rc == KERN_SUCCESS)
+ {
+ pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
+@@ -751,7 +754,12 @@ DECLHIDDEN(int) rtR0MemObjNativeMapUser(
+ {
+ /** @todo is this needed?. */
+ PROC_LOCK(pProc);
+- AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
++ AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr +
++#if __FreeBSD_version >= 1100077
++ lim_max_proc(pProc, RLIMIT_DATA));
++#else
++ lim_max(pProc, RLIMIT_DATA));
++#endif
+ PROC_UNLOCK(pProc);
+ }
+ else
+@@ -850,11 +858,15 @@ DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGet
+
+ vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
+
+- struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
+- struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
+- pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
++ if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
++ {
++ struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
++ struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
++ pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
+
+- return pmap_extract(pPhysicalMap, pb);
++ return pmap_extract(pPhysicalMap, pb);
++ }
++ return vtophys(pb);
+ }
+
+ case RTR0MEMOBJTYPE_MAPPING: