fs: Add VirtualBox guest shared folder (vboxsf) support
Subsystems (2)
v10 cover fs: Add VirtualBox guest shared folder (vboxsf) support
Hello Everyone,
Here is the 10th version of my cleaned-up / refactored version of the VirtualBox shared-folder VFS driver.
This version hopefully addresses all issues pointed out in Al's detailed review of v9 (thank you for the review Al):
Changes in v10:
-Code-style fixes and remove some unneeded checks as suggested by Al Viro -Stop handle reuse between sf_create_aux and sf_reg_open, the code for this
was racy and the re-use meant the O_APPEND was not passed to the host for newly created files with O_APPEND set
-Use idr to generate unique inode number, modelled after the kernfs code -Only read and write the contents of the passed in offset pointer once in
sf_reg_write
-Keep a list of refcounted open handles in the inode, so that writepage can
get a writeable handle this way. This replaces the old very racy code which was just storing a pointer to the last opened struct file inside the inode. This is modelled after how the cifs and fuse code do this
For changes in older versions see the change log in the patch.
This version has been used by several distributions (arch, Fedora) for a while now, so hopefully we can get this upstream soonish, please review.
Regards,
Hans
PATCH v10 1/1 fs: Add VirtualBox guest shared folder (vboxsf) support
VirtualBox hosts can share folders with guests, this commit adds a VFS driver implementing the Linux-guest side of this, allowing folders exported by the host to be mounted under Linux.
This driver depends on the guest <-> host IPC functions exported by the vboxguest driver.
---
Changes in v10:
-Code-style fixes and remove some unneeded checks as suggested by Al Viro -Stop handle reuse between sf_create_aux and sf_reg_open, the code for this
was racy and the re-use meant the O_APPEND was not passed to the host for newly created files with O_APPEND set
-Use idr to generate unique inode number, modelled after the kernfs code -Only read and write the contents of the passed in offset pointer once in
sf_reg_write
-Keep a list of refcounted open handles in the inode, so that writepage can
get a writeable handle this way. This replaces the old very racy code which was just storing a pointer to the last opened struct file inside the inode. This is modelled after how the cifs and fuse code do this
Changes in v9:
-Change license from GPL-2.0 or CDDL-1.0 to MIT, following upstream's
license change from: https://www.virtualbox.org/changeset/72627/vbox
I've gotten permission by email from VirtualBox upstream to retro-actively apply the license-change to my "fork" of the vboxsf code
-Fix not being able to mount any shared-folders when built with gcc9 -Adjust for recent vboxguest changes
-Fix potential buffer overrun in vboxsf_nlscpy
-Fix build errors in some configs, caught by buildbot
-Fix 3 sparse warnings
-Some changes from upstream VirtualBox svn:
-Use 0x786f4256 /* 'VBox' little endian */ as super-magic matching upstream -Implement AT_STATX_SYNC_TYPE support
-Properly return -EPERM when symlink creation is not supported
Changes in v8:
-Fix broken error-handling / oops when the vboxsf_map_folder() call fails -Fix umount using umount.nfs to umount vboxsf mounts
-Prefixed the modules init and exit function names with vboxsf_ -Delay connecting to the vbox hypervisor until the first mount, this fixes
vboxsf not working when it is builtin (in which case it may be initialized before the vboxguest driver has bound to the guest communication PCI device)
-Fix sf_write_end return value, return the number of bytes written or 0 on error:
https://github.com/jwrdegoede/vboxsf/issues/2
-Use an ida id in the name passed to super_setup_bdi_name so that the same
shared-folder can be mounted twice without causing a
"sysfs: cannot create duplicate filename" error
https://github.com/jwrdegoede/vboxsf/issues/3
Changes in v7:
-Do not propagate sgid / suid bits between guest-host, note hosts with
VirtualBox version 5.2.6 or newer will filter these out regardless of what we do
-Better error messages when we cannot connect to the VirtualBox guest PCI
device, which may e.g. happen when trying to use vboxsf outside a vbox vm
Changes in v6:
-Address: https://www.virtualbox.org/ticket/819 which really is multiple bugs:
1) Fix MAP_SHARED not being supported
2) Fix changes done through regular read/write on the guest side not being seen by guest apps using mmap() access
3) Fix any changes done on the host side not being seen by guest apps using mmap() access
Changes in v5:
-Honor CONFIG_NLS_DEFAULT (reported-by [email protected])
Changes in v4:
-Drop "name=..." mount option, instead use the dev_name argument to the
mount syscall, to keep compatibility with existing fstab entries
-Fix "nls=%" match_table_t entry to "nls=%s"
Changes in v3:
-Use text only mount options, instead of a custom data struct -Stop caching full path in inode data, if parents gets renamed it will change -Fixed negative dentries handling
-Dropped the force_reread flag for dirs, not sure what it was actually for
but it is no good, doing a re-read on unlink of a file will lead to another file being skipped if the caller has already iterated over the entry for the unlinked file.
-Use file_inode(), file_dentry() and d_inode() helpers
-Prefix any non object-private symbols with vboxsf_ so as to not pollute
the global namespace when builtin
-Add MAINTAINERS entry
-Misc. cleanups
Changes in v2:
-Removed various unused wrapper functions
-Don't use i_private, instead defined alloc_inode and destroy_inode
methods and use container_of.
-Drop obsolete comment referencing people to
http://www.atnf.csiro.au/people/rgooch/linux/vfs.txt
-move the single symlink op of from lnkops.c to file.c
-Use SPDX license headers
-Replace SHFLROOT / SHFLHANDLE defines with normal types -Removed unnecessary S_ISREG checks
-Got rid of bounce_buffer in regops, instead add a "user" flag to
vboxsf_read / vboxsf_write, re-using the existing __user address support in the vboxguest module
-Make vboxsf_wrappers return regular linux errno values
-Use i_size_write to update size on writing
-Convert doxygen style comments to kerneldoc style comments
--- MAINTAINERS | 6 + fs/Kconfig | 1 + fs/Makefile | 1 + fs/vboxsf/Kconfig | 10 + fs/vboxsf/Makefile | 3 + fs/vboxsf/dir.c | 538 +++++++++++++++++++++ fs/vboxsf/file.c | 452 ++++++++++++++++++ fs/vboxsf/shfl_hostintf.h | 919 ++++++++++++++++++++++++++++++++++++ fs/vboxsf/super.c | 491 +++++++++++++++++++ fs/vboxsf/utils.c | 563 ++++++++++++++++++++++ fs/vboxsf/vboxsf_wrappers.c | 375 +++++++++++++++ fs/vboxsf/vfsmod.h | 132 ++++++ 12 files changed, 3491 insertions(+) create mode 100644 fs/vboxsf/Kconfig create mode 100644 fs/vboxsf/Makefile create mode 100644 fs/vboxsf/dir.c create mode 100644 fs/vboxsf/file.c create mode 100644 fs/vboxsf/shfl_hostintf.h create mode 100644 fs/vboxsf/super.c create mode 100644 fs/vboxsf/utils.c create mode 100644 fs/vboxsf/vboxsf_wrappers.c create mode 100644 fs/vboxsf/vfsmod.h
MAINTAINERS +6 -0
@@ -16598,6 +16598,12 @@ F: include/linux/vbox_utils.h F: include/uapi/linux/vbox*.h F: drivers/virt/vboxguest/ +VIRTUAL BOX SHARED FOLDER VFS DRIVER: +M: Hans de Goede <[email protected]> +L: [email protected] +S: Maintained +F: fs/vboxsf/* + VIRTUAL SERIO DEVICE DRIVER M: Stephen Chandler Paul <[email protected]> S: Maintained
fs/Kconfig +1 -0
@@ -261,6 +261,7 @@ source "fs/romfs/Kconfig" source "fs/pstore/Kconfig" source "fs/sysv/Kconfig" source "fs/ufs/Kconfig" +source "fs/vboxsf/Kconfig" endif # MISC_FILESYSTEMS
fs/Makefile +1 -0
@@ -129,3 +129,4 @@ obj-$(CONFIG_F2FS_FS) += f2fs/ obj-$(CONFIG_CEPH_FS) += ceph/ obj-$(CONFIG_PSTORE) += pstore/ obj-$(CONFIG_EFIVAR_FS) += efivarfs/ +obj-$(CONFIG_VBOXSF_FS) += vboxsf/
fs/vboxsf/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +config VBOXSF_FS + tristate "VirtualBox guest shared folder (vboxsf) support" + depends on X86 && VBOXGUEST + select NLS + help + VirtualBox hosts can share folders with guests, this driver + implements the Linux-guest side of this allowing folders exported + by the host to be mounted under Linux. + + If you want to use shared folders in VirtualBox guests, answer Y or M.
fs/vboxsf/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_VBOXSF_FS) += vboxsf.o + +vboxsf-objs := dir.o file.o utils.o vboxsf_wrappers.o super.o
fs/vboxsf/dir.c new file +538 -0
538 lines not on this page · raw message
fs/vboxsf/file.c new file +452 -0
452 lines not on this page · raw message
fs/vboxsf/shfl_hostintf.h new file +919 -0
919 lines not on this page · raw message
fs/vboxsf/super.c new file +491 -0
491 lines not on this page · raw message
fs/vboxsf/utils.c new file +563 -0
563 lines not on this page · raw message
fs/vboxsf/vboxsf_wrappers.c new file +375 -0
1 line not on this page · raw message
fs/vboxsf/vfsmod.h new file +132 -0
132 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.21.0
Part of the diff is not shown here. View the raw message.
fs/vboxsf/dir.c line 33
Quoted 4 lines · Hans de Goede
fs/vboxsf/dir.c new file +4
-0+ params.create_flags =0 + |SHFL_CF_DIRECTORY + |SHFL_CF_ACT_OPEN_IF_EXISTS + |SHFL_CF_ACT_FAIL_IF_NEW |SHFL_CF_ACCESS_READ ;
The 0 here would seem to be superfluous. Also, most common practice in the kernel would put the binary operator at the end of the preceding line.
fs/vboxsf/dir.c line 61
Quoted 9 lines
fs/vboxsf/dir.c new file +9
-0+/** +* This is called when reference count of [file] goes to zero. Notify +* the host that it can free whatever is associated with this directory +* and deallocate our own internal buffers +* Return: 0 or negative errno value. +* @inode inode +* @file file +*/ +static int sf_dir_release (struct inode *inode,struct file *file)
I'm pretty certain most of your kdoc comments are invalid, but I could be wrong. Shouldn't this be:
/**
* name_of_function - Brief description
* @arg1: Summary arg 1
* @arg2: Summary arg 2
* @arg3: Summary arg 3
*
* Description...
*/
type name_of_function(type arg1, type arg2, type arg3)
{
...
}Quoted 1 line
+static int
sf_get_d_type(u32 mode)
unsigned int would be preferable, I think.
fs/vboxsf/dir.c line 153
Quoted 6 lines
+ * Return: 0 or negative errno value.
...fs/vboxsf/dir.c new file +3
-0+static int sf_getdent(struct file *dir, loff_t pos, + char d_name[NAME_MAX], int *d_type) + return 1;
The return value is not concordant with the function description.
Quoted 2 lines
+ * This is called when vfs wants to populate internal buffers with
+ * directory [dir]s contents.
I would say "the directory" rather than "directory [dir]s". It's fairly obvious what the definite article refers to in this case.
Quoted 4 lines
[opaque] is an argument to the
+ * [filldir]. [filldir] magically modifies it's argument - [opaque]
+ * and takes following additional arguments (which i in turn get from
+ * the host viasf_getdent):
opaque and filldir no longer exist.
Quoted 4 lines
+ * name : name of the entry (i must also supply it's length huh?)
+ * type : type of the entry (FILE | DIR | etc) (i ellect to useDT_UNKNOWN)
+ * pos : position/index of the entry
+ * ino : inode number of the entry (i fake those)
I would indent these more (use a tab after the '*' rather than a space).
fs/vboxsf/dir.c line 207
Quoted 8 lines
fs/vboxsf/dir.c new file +8
-0+/* d_name now contains a valid entry name */ + sanity = ctx->pos +0xbeef ; + fake_ino = sanity; +/* +* On 32 bit systems pos is 64 signed, while ino is 32 bit +* unsigned so fake_ino may overflow, check for this. +*/ +if (sanity - fake_ino) {
Ugh. Why '0xbeef'? Why not '1'? I wonder if:
if ((ino_t)(ctx->pos + 1) != (unsigned long long)(ctx->pos + 1))
would work.
fs/vboxsf/shfl_hostintf.h line 30
Quoted 5 lines
fs/vboxsf/shfl_hostintf.h new file +4
-0+/* Query mappings changes. */ +#define SHFL_FN_QUERY_MAPPINGS (1) +/* Query mappings changes. */ +#define SHFL_FN_QUERY_MAP_NAME (2)
Enumify?
Quoted 1 line
+#define
SHFL_ROOT_NIL((u32)~0)
UINT_MAX?
Quoted 1 line
+#define
SHFL_HANDLE_NIL((u64)~0LL)
ULONGLONG_MAX?
fs/vboxsf/shfl_hostintf.h line 492
Quoted 5 lines
fs/vboxsf/shfl_hostintf.h new file +4
-0+/** Shared folder filesystem properties. */ +struct shfl_fsproperties { +}; +VMMDEV_ASSERT_SIZE (shfl_fsproperties,12 );
Should this be __packed given the size assertion?
fs/vboxsf/super.c line 60
Quoted 11 lines
fs/vboxsf/super.c new file +11
-0+static const match_table_t vboxsf_tokens = { + { opt_nls,"nls=%s" }, + { opt_uid,"uid=%u" }, + { opt_gid,"gid=%u" }, + { opt_ttl,"ttl=%u" }, + { opt_dmode,"dmode=%o" }, + { opt_fmode,"fmode=%o" }, + { opt_dmask,"dmask=%o" }, + { opt_fmask,"fmask=%o" }, + { opt_error,NULL }, +};
This needs converting to the new mount API. See:
Documentation/filesystems/mount_api.txt
fs/vboxsf/super.c line 77
Quoted 7 lines
fs/vboxsf/super.c new file +7
-0+if (options[0 ] ==VBSF_MOUNT_SIGNATURE_BYTE_0 && + options[1 ] ==VBSF_MOUNT_SIGNATURE_BYTE_1 && + options[2 ] ==VBSF_MOUNT_SIGNATURE_BYTE_2 && + options[3 ] ==VBSF_MOUNT_SIGNATURE_BYTE_3 ) { +vbg_err ("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n" ); +return -EINVAL ; + }
This bit should go in your ->parse_monolithic() method.
David
Hi David,
Thank you for the review.
Quoted 9 lines · David Howells
fs/vboxsf/dir.c line 33
Quoted 4 lines · Hans de Goede
fs/vboxsf/dir.c new file +4
-0+ params.create_flags =0 + |SHFL_CF_DIRECTORY + |SHFL_CF_ACT_OPEN_IF_EXISTS + |SHFL_CF_ACT_FAIL_IF_NEW |SHFL_CF_ACCESS_READ ;The 0 here would seem to be superfluous. Also, most common practice in the kernel would put the binary operator at the end of the preceding line.
Ack, will fix for the next version.
Quoted 13 lines
fs/vboxsf/dir.c line 61
Quoted 9 lines
fs/vboxsf/dir.c new file +9
-0+/** +* This is called when reference count of [file] goes to zero. Notify +* the host that it can free whatever is associated with this directory +* and deallocate our own internal buffers +* Return: 0 or negative errno value. +* @inode inode +* @file file +*/ +static int sf_dir_release (struct inode *inode,struct file *file)I'm pretty certain most of your kdoc comments are invalid, but I could be wrong. Shouldn't this be:
Quoted 12 lines
/**
* name_of_function - Brief description
* @arg1: Summary arg 1
* @arg2: Summary arg 2
* @arg3: Summary arg 3
*
* Description...
*/
type name_of_function(type arg1, type arg2, type arg3)
{
...
}Right, this code is derived from the out of tree vboxsf code from VirtualBox upstream, which uses doxygen comments. I did not want to just rip the comments out so I've tried to convert them to kerneldoc style (note no docs are built from them). But you are right, my conversion is incomplete. I will fix this for the next version.
Quoted 3 lines
Quoted 1 line
+static int
sf_get_d_type(u32 mode)unsigned int would be preferable, I think.
Ack, will fix for the next version.
Quoted 8 lines
fs/vboxsf/dir.c line 153
Quoted 6 lines
+ * Return: 0 or negative errno value.
...fs/vboxsf/dir.c new file +3
-0+static int sf_getdent(struct file *dir, loff_t pos, + char d_name[NAME_MAX], int *d_type) + return 1;The return value is not concordant with the function description.
Ack, will fix for the next version.
Quoted 11 lines
Quoted 2 lines
+ * This is called when vfs wants to populate internal buffers with
+ * directory [dir]s contents.I would say "the directory" rather than "directory [dir]s". It's fairly obvious what the definite article refers to in this case.
Quoted 4 lines
[opaque] is an argument to the
+ * [filldir]. [filldir] magically modifies it's argument - [opaque]
+ * and takes following additional arguments (which i in turn get from
+ * the host viasf_getdent):
Quoted 8 lines
opaque and filldir no longer exist.
Quoted 4 lines
+ * name : name of the entry (i must also supply it's length huh?)
+ * type : type of the entry (FILE | DIR | etc) (i ellect to useDT_UNKNOWN)
+ * pos : position/index of the entry
+ * ino : inode number of the entry (i fake those)I would indent these more (use a tab after the '*' rather than a space).
Right this comment has become a bit stale, will fix.
Quoted 11 lines
fs/vboxsf/dir.c line 207
Quoted 8 lines
fs/vboxsf/dir.c new file +8
-0+/* d_name now contains a valid entry name */ + sanity = ctx->pos +0xbeef ; + fake_ino = sanity; +/* +* On 32 bit systems pos is 64 signed, while ino is 32 bit +* unsigned so fake_ino may overflow, check for this. +*/ +if (sanity - fake_ino) {Ugh. Why '
0xbeef'? Why not '1'? I wonder if:
Quoted 3 lines
if ((ino_t)(ctx->pos + 1) != (unsigned long long)(ctx->pos + 1))would work.
Yes I believe that that should work fine, will fix.
Quoted 7 lines
fs/vboxsf/shfl_hostintf.h line 30
Quoted 5 lines
fs/vboxsf/shfl_hostintf.h new file +4
-0+/* Query mappings changes. */ +#define SHFL_FN_QUERY_MAPPINGS (1) +/* Query mappings changes. */ +#define SHFL_FN_QUERY_MAP_NAME (2)Enumify?
Ack, will fix.
Quoted 7 lines
Quoted 1 line
+#define
SHFL_ROOT_NIL((u32)~0)
UINT_MAX?Quoted 1 line
+#define
SHFL_HANDLE_NIL((u64)~0LL)
ULONGLONG_MAX?
ULLONG_MAX, otherwise ack, will fix both.
Quoted 7 lines
fs/vboxsf/shfl_hostintf.h line 492
Quoted 5 lines
fs/vboxsf/shfl_hostintf.h new file +4
-0+/** Shared folder filesystem properties. */ +struct shfl_fsproperties { +}; +VMMDEV_ASSERT_SIZE (shfl_fsproperties,12 );Should this be
__packedgiven the size assertion?
AFAICT packing it would give it a size of 10, 4 for the u32 + 6 bytes for the bools. So I'm keeping this as is.
Quoted 16 lines
fs/vboxsf/super.c line 60
Quoted 11 lines
fs/vboxsf/super.c new file +11
-0+static const match_table_t vboxsf_tokens = { + { opt_nls,"nls=%s" }, + { opt_uid,"uid=%u" }, + { opt_gid,"gid=%u" }, + { opt_ttl,"ttl=%u" }, + { opt_dmode,"dmode=%o" }, + { opt_fmode,"fmode=%o" }, + { opt_dmask,"dmask=%o" }, + { opt_fmask,"fmask=%o" }, + { opt_error,NULL }, +};This needs converting to the new mount API. See:
Documentation/filesystems/mount_api.txt
Quoted 9 lines
fs/vboxsf/super.c line 77
Quoted 7 lines
fs/vboxsf/super.c new file +7
-0+if (options[0 ] ==VBSF_MOUNT_SIGNATURE_BYTE_0 && + options[1 ] ==VBSF_MOUNT_SIGNATURE_BYTE_1 && + options[2 ] ==VBSF_MOUNT_SIGNATURE_BYTE_2 && + options[3 ] ==VBSF_MOUNT_SIGNATURE_BYTE_3 ) { +vbg_err ("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n" ); +return -EINVAL ; + }This bit should go in your ->parse_monolithic() method.
Ok, I will start working on converting it to the new mount API and once that is done and tested I will post a new version.
Regards,
Hans