diff --git a/emulators/virtualbox-ose-additions/Makefile b/emulators/virtualbox-ose-additions/Makefile index 609337173458..6a2e8cbd2916 100644 --- a/emulators/virtualbox-ose-additions/Makefile +++ b/emulators/virtualbox-ose-additions/Makefile @@ -1,164 +1,166 @@ PORTNAME= virtualbox-ose PORTVERSION= 6.1.46 PORTREVISION?= 0 CATEGORIES= emulators MASTER_SITES= https://download.virtualbox.org/virtualbox/${PORTVERSION}/ PKGNAMESUFFIX?= -additions DISTNAME= VirtualBox-${PORTVERSION} MAINTAINER= vbox@FreeBSD.org COMMENT= VirtualBox additions for FreeBSD guests WWW= https://www.virtualbox.org/ LICENSE= GPLv2 LICENSE_FILE= ${WRKSRC}/COPYING ONLY_FOR_ARCHS= amd64 USES= compiler:c++11-lang cpe iconv kmod tar:bzip2 USE_RC_SUBR= vboxguest vboxservice BUILD_DEPENDS= yasm:devel/yasm \ kmk:devel/kBuild \ xsltproc:textproc/libxslt CPE_VENDOR= oracle CPE_PRODUCT= vm_virtualbox PATCHDIR= ${.CURDIR}/../${PORTNAME}/files WRKSRC= ${WRKDIR}/VirtualBox-${PORTVERSION} MAKE_JOBS_UNSAFE= yes HAS_CONFIGURE= yes CONFIGURE_ARGS+= --disable-alsa \ --disable-docs \ --disable-libvpx \ --disable-opengl \ --disable-pulse \ --disable-python \ --disable-sdl-ttf \ --disable-xpcom CONFIGURE_ARGS+= --nofatal --with-gcc="${CC}" --with-g++="${CXX}" CONFLICTS_INSTALL= virtualbox-ose \ virtualbox-ose-additions-devel \ virtualbox-ose-additions-legacy \ virtualbox-ose-devel \ virtualbox-ose-legacy \ virtualbox-ose-lite \ virtualbox-ose-nox11 OPTIONS_DEFINE= DBUS DEBUG X11 OPTIONS_DEFAULT= DBUS X11 OPTIONS_SUB= yes DBUS_CONFIGURE_OFF= --disable-dbus DBUS_LIB_DEPENDS= libdbus-1.so:devel/dbus DEBUG_CONFIGURE_ON= --build-debug DEBUG_VARS= KMK_BUILDTYPE=debug DEBUG_VARS_OFF= KMK_BUILDTYPE=release X11_BUILD_DEPENDS= ${LOCALBASE}/include/xorg/compiler.h:x11-servers/xorg-server X11_RUN_DEPENDS= xrandr:x11/xrandr X11_USE= XORG=x11,xcursor,xext,xorgproto,xmu,xrandr,xt X11_USES= pkgconfig xorg X11_VARS= VBOX_WITH_X11=1 .include .if ${SLAVE_PORT} == no CONFLICTS_INSTALL+= virtualbox-ose-additions-nox11 .else CONFLICTS_INSTALL+= virtualbox-ose-additions .endif VIDEODIR= ${PREFIX}/lib/xorg/modules/drivers INPUTDIR= ${PREFIX}/lib/xorg/modules/input VBOX_BIN= ${WRKSRC}/out/${KMK_ARCH}/${KMK_BUILDTYPE}/bin/additions VBOX_SBINS= VBoxControl \ VBoxService \ mount_vboxvfs BUILD_WRKSRC= ${VBOX_BIN}/src KMK_ARCH= freebsd.${ARCH:S/i386/x86/} KMK_CONFIG+= VBOX_LIBPATH_X11=${LOCALBASE} VBOX_FREEBSD_SRC=${SRC_BASE}/sys .if ${PORT_OPTIONS:MX11} .else VBOX_WITH_X11= .endif .include post-patch: @${ECHO_CMD} 'VBOX_ONLY_ADDITIONS = 1' > ${WRKSRC}/LocalConfig.kmk @${ECHO_CMD} 'VBOX_WITH_DRAG_AND_DROP = ${VBOX_WITH_X11}' >> \ ${WRKSRC}/LocalConfig.kmk @${ECHO_CMD} 'VBOX_WITH_DRAG_AND_DROP_GH = ${VBOX_WITH_X11}' >> \ ${WRKSRC}/LocalConfig.kmk @${ECHO_CMD} 'VBOX_WITH_X11_ADDITIONS = ${VBOX_WITH_X11}' >> \ ${WRKSRC}/LocalConfig.kmk @${ECHO_CMD} 'VBOX_GCC_std = -std=c++11' >> ${WRKSRC}/LocalConfig.kmk + @${ECHO_CMD} 'CXXDEFS += _LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR' >> \ + ${WRKSRC}/LocalConfig.kmk .if ${COMPILER_TYPE} == clang @${REINPLACE_CMD} -e 's| -finline-limit=8000||' \ -e 's| -fno-merge-constants||' \ -e 's| -mpreferred-stack-boundary=2||' ${WRKSRC}/Config.kmk .endif @${REINPLACE_CMD} -e 's|/usr/local|${LOCALBASE}|g' \ ${WRKSRC}/Config.kmk ${WRKSRC}/configure \ ${WRKSRC}/kBuild/header.kmk ${WRKSRC}/kBuild/units/qt4.kmk \ ${WRKSRC}/kBuild/units/qt5.kmk ${WRKSRC}/kBuild/sdks/LIBSDL.kmk \ ${WRKSRC}/src/VBox/Additions/x11/Installer/98vboxadd-xclient \ ${WRKSRC}/src/VBox/Additions/x11/Installer/vboxclient.desktop \ ${WRKSRC}/src/VBox/Additions/x11/vboxmouse/Makefile.kmk \ ${WRKSRC}/src/VBox/Additions/x11/vboxvideo/Makefile.kmk @${REINPLACE_CMD} \ -e 's|\$$KBUILDDIR_BIN/kmk_sed|${LOCALBASE}/bin/kmk_sed|g' \ ${WRKSRC}/configure .if empty(ICONV_LIB) @${REINPLACE_CMD} -e 's|iconv||' ${WRKSRC}/Config.kmk \ ${WRKSRC}/src/VBox/Runtime/Makefile.kmk \ ${WRKSRC}/src/VBox/Additions/x11/VBoxClient/Makefile.kmk @${ECHO_CMD} 'VBOX_ICONV_DEFS = LIBICONV_PLUG' >> ${WRKSRC}/LocalConfig.kmk .endif post-patch-X11-on: @${ECHO_CMD} 'VBOX_USE_SYSTEM_XORG_HEADERS = 1' >> \ ${WRKSRC}/LocalConfig.kmk pre-build: cd ${WRKSRC} && ${SH} -c \ ". env.sh && ${KMK_CONFIG} ${LOCALBASE}/bin/kmk" do-install: ${MKDIR} ${STAGEDIR}${KMODDIR} ${INSTALL_KLD} ${VBOX_BIN}/src/vboxguest/vboxguest.ko \ ${STAGEDIR}${KMODDIR} ${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/ 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_LIB} ${VBOX_BIN}/vboxvideo_drv_system.so \ ${STAGEDIR}${VIDEODIR}/vboxvideo_drv.so ${MKDIR} ${STAGEDIR}${INPUTDIR} ${INSTALL_LIB} ${VBOX_BIN}/vboxmouse_drv_system.so \ ${STAGEDIR}${INPUTDIR}/vboxmouse_drv.so .include 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 index a4f94f92e148..3551533f2bcd 100644 --- 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 @@ -1,421 +1,427 @@ --- src/VBox/Additions/freebsd/vboxvfs/vboxvfs.h.orig 2021-01-07 15:34:22 UTC +++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs.h @@ -1,8 +1,3 @@ -/* $Id: vboxvfs.h $ */ -/** @file - * Description. - */ - /* * Copyright (C) 2010-2020 Oracle Corporation * -@@ -24,72 +19,371 @@ +@@ -24,72 +19,377 @@ #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) -struct vboxvfs_mount_info { - char name[MAX_HOST_NAME]; - char nls_name[MAX_NLS_NAME]; - int uid; - int gid; - int ttl; -}; +#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 + +#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/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 +#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 "common/VBoxGuestLib/SysHlp.h" + +#endif /* !FREEBSD_STYLE */ + #include #include +#include -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; +#include + ++#if __FreeBSD_version >= 1400093 ++typedef __enum_uint8(vtype) enum_vtype_t; ++#else ++typedef enum vtype enum_vtype_t; ++#endif ++ +#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; }; -/* structs - stolen from the linux shared module code */ +/* + * 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 */ + + /* 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 */ +}; + 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 *, ++ enum_vtype_t, 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 /* !GA_INCLUDED_SRC_freebsd_vboxvfs_vboxvfs_h */ - 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 index c37baa13efc4..be9d416aa3bf 100644 --- 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 @@ -1,644 +1,644 @@ --- src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vfsops.c.orig 2021-04-28 16:24:46 UTC +++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vfsops.c @@ -1,8 +1,3 @@ -/* $Id: vboxvfs_vfsops.c $ */ -/** @file - * Description. - */ - /* * Copyright (C) 2008-2020 Oracle Corporation * @@ -14,245 +9,478 @@ * 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 #include #include #include #include #include +#include +#include #include #include #include #include +#include +#include +#include #include #include #include +#include -#include +#include +#include +#include "vboxvfs.h" + #define VFSMP2SFGLOBINFO(mp) ((struct sf_glob_info *)mp->mnt_data) -static int vboxvfs_version = VBOXVFS_VERSION; +#ifdef MALLOC_DECLARE +MALLOC_DEFINE(M_VBOXVFS, "vboxvfs", "VBOX VFS"); +#endif -SYSCTL_NODE(_vfs, OID_AUTO, vboxvfs, CTLFLAG_RW, 0, "VirtualBox shared filesystem"); -SYSCTL_INT(_vfs_vboxvfs, OID_AUTO, version, CTLFLAG_RD, &vboxvfs_version, 0, ""); +static sfp_connection_t *sfprov = NULL; -/* global connection to the host service. */ -static VBGLSFCLIENT g_vboxSFClient; +static int vboxfs_version = VBOXVFS_VERSION; +u_int vboxvfs_debug = 1; -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; +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 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, +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, ++ enum_vtype_t 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; + struct vboxfs_node *nnode; - printf("%s: Enter\n", __FUNCTION__); + 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); + } - rc = copyin(data, &args, sizeof(struct vboxvfs_mount_info)); - if (rc) - return rc; + nnode = (struct vboxfs_node *)uma_zalloc_arg( + vsfmp->sf_node_pool, vsfmp, M_WAITOK); - ma = mount_argf(ma, "uid", "%d", args.uid); - ma = mount_argf(ma, "gid", "%d", args.gid); - ma = mount_arg(ma, "from", args.name, -1); + /* 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; - rc = kernel_mount(ma, flags); + /* Type-specific initialization. */ + switch (nnode->sf_type) { + case VBLK: + case VCHR: + case VDIR: + case VFIFO: + case VSOCK: + case VLNK: + case VREG: + break; - printf("%s: Leave rc=%d\n", __FUNCTION__, rc); + default: + panic("vboxfs_alloc_node: type %p %d", nnode, (int)nnode->sf_type); + } - 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__); +#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); - if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) - return EOPNOTSUPP; + uma_zfree(vboxfs->sf_node_pool, node); +} - if (vfs_filteropt(mp->mnt_optnew, vboxvfs_opts)) - { - vfs_mount_error(mp, "%s", "Invalid option"); - return EINVAL; - } +static int +vboxfs_cmount(struct mntarg *ma, void *data, uint64_t flags) +{ + struct vboxfs_mount_info args; + int error = 0; - rc = vfs_getopt(mp->mnt_optnew, "from", (void **)&pszShare, &cbShare); - if (rc || pszShare[cbShare-1] != '\0' || cbShare > 0xfffe) - return EINVAL; + if (data == NULL) + return (EINVAL); + error = copyin(data, &args, sizeof(struct vboxfs_mount_info)); + if (error) + return (error); - rc = vfs_getopt(mp->mnt_optnew, "gid", (void **)&gid, &cbOption); - if ((rc != ENOENT) && (rc || cbOption != sizeof(gid))) - return EINVAL; + 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); - rc = vfs_getopt(mp->mnt_optnew, "uid", (void **)&uid, &cbOption); - if ((rc != ENOENT) && (rc || cbOption != sizeof(uid))) - return EINVAL; + return (kernel_mount(ma, flags)); +} - pShFlGlobalInfo = RTMemAllocZ(sizeof(struct sf_glob_info)); - if (!pShFlGlobalInfo) - return ENOMEM; +static const char *vboxfs_opts[] = { + "fstype", + "fspath", + "from", + "uid", + "gid", + "file_mode", + "dir_mode", + "errmsg", + NULL +}; - cbShFlShareName = offsetof (SHFLSTRING, String.utf8) + cbShare + 1; - pShFlShareName = RTMemAllocZ(cbShFlShareName); - if (!pShFlShareName) - return VERR_NO_MEMORY; +#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) - pShFlShareName->u16Length = cbShare; - pShFlShareName->u16Size = cbShare + 1; - memcpy (pShFlShareName->String.utf8, pszShare, cbShare + 1); +static int +vboxfs_node_ctor(void *mem, int size, void *arg, int flags) +{ + struct vboxfs_node *node = (struct vboxfs_node *)mem; - rc = VbglR0SfMapFolder (&g_vboxSFClient, pShFlShareName, &pShFlGlobalInfo->map); - RTMemFree(pShFlShareName); + node->sf_vnode = NULL; + node->sf_vpstate = 0; - if (RT_FAILURE (rc)) - { - RTMemFree(pShFlGlobalInfo); - printf("VbglR0SfMapFolder failed rc=%d\n", rc); - return EPROTO; - } + return (0); +} - pShFlGlobalInfo->uid = uid; - pShFlGlobalInfo->gid = gid; +static void +vboxfs_node_dtor(void *mem, int size, void *arg) +{ + struct vboxfs_node *node = (struct vboxfs_node *)mem; + node->sf_type = VNON; +} - mp->mnt_data = pShFlGlobalInfo; +static int +vboxfs_node_init(void *mem, int size, int flags) +{ + struct vboxfs_node *node = (struct vboxfs_node *)mem; + node->sf_ino = 0; - /** @todo root vnode. */ + mtx_init(&node->sf_interlock, "tmpfs node interlock", NULL, MTX_DEF); - vfs_getnewfsid(mp); - vfs_mountedfrom(mp, pszShare); + return (0); +} - printf("%s: Leave rc=0\n", __FUNCTION__); +static void +vboxfs_node_fini(void *mem, int size) +{ + struct vboxfs_node *node = (struct vboxfs_node *)mem; - return 0; + mtx_destroy(&node->sf_interlock); } -static int vboxvfs_unmount(struct mount *mp, int mntflags, struct thread *td) +static int +vboxfs_mount(struct mount *mp) { - struct sf_glob_info *pShFlGlobalInfo = VFSMP2SFGLOBINFO(mp); - int rc; - int flags = 0; + 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; - rc = VbglR0SfUnmapFolder(&g_vboxSFClient, &pShFlGlobalInfo->map); - if (RT_FAILURE(rc)) - printf("Failed to unmap shared folder\n"); + if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) + return (EOPNOTSUPP); - if (mntflags & MNT_FORCE) - flags |= FORCECLOSE; + if (vfs_filteropt(opts, vboxfs_opts)) { + vfs_mount_error(mp, "%s", "Invalid option"); + return (EINVAL); + } - /* There is 1 extra root vnode reference (vnode_root). */ - rc = vflush(mp, 1, flags, td); - if (rc) - return rc; + 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); + } - RTMemFree(pShFlGlobalInfo); - mp->mnt_data = NULL; + 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; - return 0; + /* 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; + mp->mnt_stat.f_fsid.val[0] = fsinfo.serial; + mp->mnt_stat.f_fsid.val[1] = 0; + mp->mnt_flag |= MNT_LOCAL; + if (readonly != 0) + mp->mnt_flag |= MNT_RDONLY; + + mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED; + MNT_IUNLOCK(mp); + vfs_mountedfrom(mp, share_name); + + 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; - printf("%s: Enter\n", __FUNCTION__); + vboxfsmp = VFSTOVBOXFS(mp); + td = curthread; + flags = 0; + if (mntflags & MNT_FORCE) + flags |= FORCECLOSE; - vp = pShFlGlobalInfo->vnode_root; - VREF(vp); + error = vflush(mp, 0, flags, td); + if (error) + return (error); - vn_lock(vp, flags | LK_RETRY, td); - *vpp = vp; + /* Invoke Hypervisor unmount interface before proceeding */ + error = sfprov_unmount(vboxfsmp->sf_handle); + if (error != 0) { + /* TBD anything here? */ + } - printf("%s: Leave\n", __FUNCTION__); + uma_zdestroy(vboxfsmp->sf_node_pool); - return rc; + free(vboxfsmp, M_VBOXVFS); + MNT_ILOCK(mp); + mp->mnt_data = NULL; + mp->mnt_flag &= ~MNT_LOCAL; + MNT_IUNLOCK(mp); + + return (0); } -static int vboxvfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct thread *td) +static int +vboxfs_root(struct mount *mp, int flags, struct vnode **vpp) { - return EOPNOTSUPP; + int error; + error = vboxfs_alloc_vp(mp, VFSTOVBOXFS(mp)->sf_root, flags, vpp); + + if (!error) + (*vpp)->v_vflag |= VV_ROOT; + + return error; } -int vboxvfs_init(struct vfsconf *vfsp) +/* + * Do operation associated with quotas, not supported + */ +static int +vboxfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg +#if __FreeBSD_version >= 1400018 + , bool *mp_busy +#endif + ) { - int rc; + return (EOPNOTSUPP); +} - /* Initialize the R0 guest library. */ - rc = VbglR0SfInit(); - if (RT_FAILURE(rc)) - return ENXIO; +/* + * Initialize the filesystem globals. + */ +static int +vboxfs_init(struct vfsconf *vfsp) +{ + int error; - /* 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; - } + DROP_GIANT(); - rc = VbglR0SfSetUtf8(&g_vboxSFClient); - if (RT_FAILURE (rc)) - { - printf("VbglR0SfSetUtf8 failed, rc=%d\n", rc); - VbglR0SfDisconnect(&g_vboxSFClient); - VbglR0SfTerm(); - return EPROTO; - } + sfprov = sfprov_connect(SFPROV_VERSION); + if (sfprov == NULL) { + printf("%s: couldn't connect to sf provider", __func__); + return (ENODEV); + } - printf("Successfully loaded shared folder module\n"); + error = sfprov_set_show_symlinks(); + if (error != 0) + printf("%s: host unable to show symlinks, error=%d\n", + __func__, error); - 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 index b2f56a2bbd28..d0da1fbf3f45 100644 --- 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 @@ -1,1491 +1,1491 @@ --- src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vnops.c.orig 2022-07-19 20:51:58 UTC +++ src/VBox/Additions/freebsd/vboxvfs/vboxvfs_vnops.c @@ -14,228 +14,1364 @@ * 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 #include #include #include -#include +#include +#include +#include #include +#include #include -#include +#include #include -#include #include -#include -#include -#include +#include +#include +#include +#include -#include -#include +#include +#include "vboxvfs.h" + +#if __FreeBSD_version < 1300063 +#define VN_IS_DOOMED(vp) (((vp)->v_iflag & VI_DOOMED) != 0) +#endif + /* * 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; +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 vboxvfs_vnodeops = { - .vop_default = &default_vnodeops, +struct vop_vector vboxfs_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, + .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 }; +#if __FreeBSD_version > 1300068 +VFS_VOP_VECTOR_REGISTER(vboxfs_vnodeops); +#endif -static int vboxvfs_access(struct vop_access_args *ap) +static uint64_t +vsfnode_cur_time_usec(void) { - return 0; + struct timeval now; + + getmicrotime(&now); + + return (now.tv_sec*1000 + now.tv_usec); } -static int vboxvfs_open(struct vop_open_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_close(struct vop_close_args *ap) +static int +vsfnode_update_stat_cache(struct vboxfs_node *np) { - return 0; + int error; + + 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(); + + return (error); } -static int vboxvfs_getattr(struct vop_getattr_args *ap) +/* + * Need to clear v_object for insmntque failure. + */ +static void +vboxfs_insmntque_dtr(struct vnode *vp, void *dtr_arg) { - 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_setattr(struct vop_setattr_args *ap) +/* + * 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) { - return 0; + 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) || + (VN_IS_DOOMED(vp) && + (lkflag & LK_NOWAIT) != 0)) { + VI_UNLOCK(vp); + VBOXFS_NODE_UNLOCK(node); + error = ENOENT; + vp = NULL; + goto out; + } + if (VN_IS_DOOMED(vp)) { + 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); +#if __FreeBSD_version < 1300109 + error = vget(vp, lkflag | LK_INTERLOCK, curthread); +#else + error = vget(vp, lkflag | LK_INTERLOCK); +#endif + 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); + +#if __FreeBSD_version < 1400051 + error = insmntque1(vp, mp, vboxfs_insmntque_dtr, NULL); +#else + error = insmntque(vp, mp); +#endif + if (error) { +#if __FreeBSD_version >= 1400051 + vboxfs_insmntque_dtr(vp, NULL); +#endif + 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 + + return error; } -static int vboxvfs_read(struct vop_read_args *ap) +/* + * Destroys the association between the vnode vp and the node it + * references. + */ +void +vboxfs_free_vp(struct vnode *vp) { - return 0; + struct vboxfs_node *node; + + node = VP_TO_VBOXFS_NODE(vp); + + 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_write(struct vop_write_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, ++ enum_vtype_t type, mode_t mode, struct vboxfs_node *parent, + int lkflag, struct vnode **vpp) { - return 0; + int error; + struct vboxfs_node *unode; + + error = vboxfs_alloc_node(vboxfsmp->sf_vfsp, vboxfsmp, fullpath, type, + vboxfsmp->sf_uid, vboxfsmp->sf_gid, mode, parent, &unode); + + if (error) + goto out; + + error = vboxfs_alloc_vp(vboxfsmp->sf_vfsp, unode, lkflag, vpp); + if (error) + vboxfs_free_node(vboxfsmp, unode); + +out: + return (error); } -static int vboxvfs_create(struct vop_create_args *ap) +static int +vboxfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags, + struct vnode **rvp) { - return 0; + + return (vboxfs_alloc_vp(mp, arg, lkflags, rvp)); } -static int vboxvfs_remove(struct vop_remove_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; + + if (len <= 2 && tail[0] == '.' && (len == 1 || tail[1] == '.')) + 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, "/"); + strcat(p, tail); + return (p); } -static int vboxvfs_rename(struct vop_rename_args *ap) +static int +vboxfs_access(struct vop_access_args *ap) { - return 0; + 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; + +#if __FreeBSD_version < 1300105 + return (vaccess(vp->v_type, m, node->vboxfsmp->sf_uid, + node->vboxfsmp->sf_gid, accmode, ap->a_cred, NULL)); +#else + return (vaccess(vp->v_type, m, node->vboxfsmp->sf_uid, + node->vboxfsmp->sf_gid, accmode, ap->a_cred)); +#endif } -static int vboxvfs_link(struct vop_link_args *ap) +/* + * Clears the (cached) directory listing for the node. + */ +static void +vfsnode_clear_dir_list(struct vboxfs_node *np) { - return EOPNOTSUPP; + 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 vboxvfs_symlink(struct vop_symlink_args *ap) +static int +vboxfs_open(struct vop_open_args *ap) { - return EOPNOTSUPP; + 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 int vboxvfs_mknod(struct vop_mknod_args *ap) +static void +vfsnode_invalidate_stat_cache(struct vboxfs_node *np) { - return EOPNOTSUPP; + np->sf_stat_time = 0; } -static int vboxvfs_mkdir(struct vop_mkdir_args *ap) +static int +vboxfs_close(struct vop_close_args *ap) { - return 0; + 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 vboxvfs_rmdir(struct vop_rmdir_args *ap) +static int +vboxfs_getattr(struct vop_getattr_args *ap) { - return 0; + 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 vboxvfs_readdir(struct vop_readdir_args *ap) +static int +vboxfs_setattr(struct vop_setattr_args *ap) { - return 0; + 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); } -static int vboxvfs_fsync(struct vop_fsync_args *ap) +#define blkoff(vboxfsmp, loc) ((loc) & (vboxfsmp)->bmask) + +static int +vboxfs_read(struct vop_read_args *ap) { - return 0; + 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 vboxvfs_print (struct vop_print_args *ap) +static int +vboxfs_write(struct vop_write_args *ap) { - return 0; + 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 vboxvfs_pathconf (struct vop_pathconf_args *ap) +static int +vboxfs_create(struct vop_create_args *ap) { - return 0; + 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 vboxvfs_strategy (struct vop_strategy_args *ap) +static int +vboxfs_remove(struct vop_remove_args *ap) { - return 0; + 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 vboxvfs_ioctl(struct vop_ioctl_args *ap) +static int +vboxfs_rename(struct vop_rename_args *ap) { - return ENOTTY; + 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 vboxvfs_getextattr(struct vop_getextattr_args *ap) +static int +vboxfs_link(struct vop_link_args *ap) { - return 0; + return (EOPNOTSUPP); } -static int vboxvfs_advlock(struct vop_advlock_args *ap) +static int +vboxfs_symlink(struct vop_symlink_args *ap) { - return 0; + 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 vboxvfs_lookup(struct vop_lookup_args *ap) +static int +vboxfs_mkdir(struct vop_mkdir_args *ap) { - return 0; + 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 vboxvfs_inactive(struct vop_inactive_args *ap) +static int +vboxfs_rmdir(struct vop_rmdir_args *ap) { - return 0; + 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 vboxvfs_reclaim(struct vop_reclaim_args *ap) +static int +vboxfs_readdir(struct vop_readdir_args *ap) { - return 0; + 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 = NULL; + 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 vboxvfs_getpages(struct vop_getpages_args *ap) +static int +vboxfs_readlink(struct vop_readlink_args *v) { - return 0; + 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 vboxvfs_putpages(struct vop_putpages_args *ap) +static int +vboxfs_fsync(struct vop_fsync_args *ap) { - return 0; + 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); +} + +/* + * File specific ioctls. + */ +static int +vboxfs_ioctl(struct vop_ioctl_args *ap) +{ + return (ENOTTY); +} + +/* + * 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, +#if __FreeBSD_version < 1400037 + cnp->cn_thread); +#else + curthread); +#endif + if (error != 0) + goto out; + +#if __FreeBSD_version < 1400068 + /* Keep the component name in the buffer for + * future uses. */ + cnp->cn_flags |= SAVENAME; +#endif + + 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 +vboxfs_vptofh(struct vop_vptofh_args *ap) +{ + + return (EOPNOTSUPP); +}