Skip to contentSkip to conversation

fs: Add VirtualBox guest shared folder (vboxsf) support

PATCH Superseded Hans de Goede 2 participants 5 messages Jun 6, 2019

Subsystems (2)

v11 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v11 cover fs: Add VirtualBox guest shared folder (vboxsf) support

Hello Everyone,

Here is the 11th version of my cleaned-up / refactored version of the VirtualBox shared-folder VFS driver.

This version hopefully addresses all issues pointed out in David Howell's review of v10 (thank you for the review David):

Changes in v11:
-Convert to the new Documentation/filesystems/mount_api.txt mount API -Fixed all the function kerneldoc comments to have things in the proper order -Change type of d_type variable passed as type to dir_emit from int to
unsigned int
-Replaced the fake-ino overflow test with the one suggested by David Howells -Fixed various coding style issues

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

#1 lore raw

PATCH v11 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 v11:
-Convert to the new Documentation/filesystems/mount_api.txt mount API -Fixed all the function kerneldoc comments to have things in the proper order -Change type of d_type variable passed as type to dir_emit from int to
unsigned int
-Replaced the fake-ino overflow test with the one suggested by David Howells -Fixed various coding style issues

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             | 562 ++++++++++++++++++++++
 fs/vboxsf/file.c            | 452 ++++++++++++++++++
 fs/vboxsf/shfl_hostintf.h   | 901 ++++++++++++++++++++++++++++++++++++
 fs/vboxsf/super.c           | 482 +++++++++++++++++++
 fs/vboxsf/utils.c           | 568 +++++++++++++++++++++++
 fs/vboxsf/vboxsf_wrappers.c | 379 +++++++++++++++
 fs/vboxsf/vfsmod.h          | 138 ++++++
 12 files changed, 3503 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
index bd186c616b8b..1262a06d2358 100644
@@ -16823,6 +16823,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
index f1046cf6ad85..b05dafd4db0f 100644
@@ -262,6 +262,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
index c9aea23aba56..76bcde11a689 100644
@@ -130,3 +130,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
new file mode 100644
index 000000000000..b84586ae08b3
@@ -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
new file mode 100644
index 000000000000..fcbd488f9eec
@@ -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 +562 -0

562 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 +901 -0

901 lines not on this page · raw message

fs/vboxsf/super.c new file +482 -0

482 lines not on this page · raw message

fs/vboxsf/utils.c new file +568 -0

567 lines not on this page · raw message

fs/vboxsf/vboxsf_wrappers.c new file +379 -0

379 lines not on this page · raw message

fs/vboxsf/vfsmod.h new file +138 -0

138 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.

#2 lore raw
Quoted 12 lines · Hans de Goede
fs/vboxsf/shfl_hostintf.h new file +12 -0
+enum {
+	SHFL_FN_QUERY_MAPPINGS = 1,	/* Query mappings changes. */
+	SHFL_FN_QUERY_MAP_NAME,		/* Query map name. */
+	SHFL_FN_CREATE,			/* Open/create object. */
+	SHFL_FN_CLOSE,			/* Close object handle. */
+	SHFL_FN_READ,			/* Read object content. */
+	SHFL_FN_WRITE,			/* Write new object content. */
+	SHFL_FN_LOCK,			/* Lock/unlock a range in the object. */
+	SHFL_FN_LIST,			/* List object content. */
+	SHFL_FN_INFORMATION,		/* Query/set object information. */
+	/* Note function number 10 is not used! */
+	SHFL_FN_REMOVE = 11,		/* Remove object */

fs/vboxsf/shfl_hostintf.h line 48

Quoted 10 lines
fs/vboxsf/shfl_hostintf.h new file +10 -0
+	SHFL_FN_MAP_FOLDER_OLD,		/* Map folder (legacy) */
+	SHFL_FN_UNMAP_FOLDER,		/* Unmap folder */
+	SHFL_FN_RENAME,			/* Rename object */
+	SHFL_FN_FLUSH,			/* Flush file */
+	SHFL_FN_SET_UTF8,		/* Select UTF8 filename encoding */
+	SHFL_FN_MAP_FOLDER,		/* Map folder */
+	SHFL_FN_READLINK,		/* Read symlink dest (as of VBox 4.0) */
+	SHFL_FN_SYMLINK,		/* Create symlink (as of VBox 4.0) */
+	SHFL_FN_SET_SYMLINKS,		/* Ask host to show symlinks (as of 4.0) */
+};

If these are protocol numbers that can't be changed, I would assign the value on all of them. If they're used by userspace, should they be moved into a uapi header (and the same for the other stuff in this file)?

fs/vboxsf/super.c line 55

Quoted 11 lines
fs/vboxsf/super.c new file +11 -0
+static const struct fs_parameter_spec vboxsf_param_specs[] = {
+	fsparam_string("nls", opt_nls),
+	fsparam_u32("uid", opt_uid),
+	fsparam_u32("gid", opt_gid),
+	fsparam_u32("ttl", opt_ttl),
+	fsparam_u32oct("dmode", opt_dmode),
+	fsparam_u32oct("fmode", opt_fmode),
+	fsparam_u32oct("dmask", opt_dmask),
+	fsparam_u32oct("fmask", opt_fmask),
+	{}
+};

I would format this with tabs so that everything nicely lines up:

	static const struct fs_parameter_spec vboxsf_param_specs[] = {
		fsparam_string	("nls",		opt_nls),
		fsparam_u32	("uid",		opt_uid),
		fsparam_u32	("gid",		opt_gid),
		fsparam_u32	("ttl",		opt_ttl),
		fsparam_u32oct	("dmode",	opt_dmode),
		fsparam_u32oct	("fmode",	opt_fmode),
		fsparam_u32oct	("dmask",	opt_dmask),
		fsparam_u32oct	("fmask",	opt_fmask),
		{}
	};

but, otherwise, good!

fs/vboxsf/super.c line 86

Quoted 6 lines
fs/vboxsf/super.c new file +6 -0
+	case opt_uid:
+		ctx->o.uid = result.uint_32;
+		break;
+	case opt_gid:
+		ctx->o.gid = result.uint_32;
+		break;

Should you be using kuid/kgid transforms for the appropriate namespace?

		opts->fs_uid = make_kuid(current_user_ns(), option);
		if (!uid_valid(opts->fs_uid))
			return -EINVAL;

sort of thing (excerpt from fs/fat/inode.c).

fs/vboxsf/super.c line 89

Quoted 3 lines
fs/vboxsf/super.c new file +3 -0
+	case opt_ttl:
+		ctx->o.ttl = msecs_to_jiffies(result.uint_32);
+		break;

Is 0 valid?

fs/vboxsf/super.c line 101

Quoted 12 lines
fs/vboxsf/super.c new file +12 -0
+	case opt_dmode:
+		ctx->o.dmode = result.uint_32;
+		break;
+	case opt_fmode:
+		ctx->o.fmode = result.uint_32;
+		break;
+	case opt_dmask:
+		ctx->o.dmask = result.uint_32;
+		break;
+	case opt_fmask:
+		ctx->o.fmask = result.uint_32;
+		break;

Do these need vetting? I guess you kind of do:

		inode->i_mode =
			sf_g->o.dmode != ~0 ? (sf_g->o.dmode & 0777) : mode;
		inode->i_mode &= ~sf_g->o.dmask;

I'm guessing you're stuck with the mount options?

fs/vboxsf/vfsmod.h line 24

Quoted 2 lines
fs/vboxsf/vfsmod.h new file +2 -0
+struct vboxsf_options {
+	int ttl;

jiffies are unsigned long.

fs/vboxsf/vfsmod.h line 26

Quoted 2 lines
fs/vboxsf/vfsmod.h new file +2 -0
+	int uid;
+	int gid;

kuid_t & kgid_t.

fs/vboxsf/vfsmod.h line 31

Quoted 5 lines
fs/vboxsf/vfsmod.h new file +5 -0
+	int dmode;
+	int fmode;
+	int dmask;
+	int fmask;
+};

umode_t.

#3 lore raw

Hi,

Thank you for the quick review.

Quoted 19 lines · David Howells
Quoted 17 lines · Hans de Goede
fs/vboxsf/shfl_hostintf.h new file +17 -0
+enum {
+	SHFL_FN_QUERY_MAPPINGS = 1,	/* Query mappings changes. */
+	SHFL_FN_QUERY_MAP_NAME,		/* Query map name. */
+	SHFL_FN_CREATE,			/* Open/create object. */
+	SHFL_FN_CLOSE,			/* Close object handle. */
+	SHFL_FN_READ,			/* Read object content. */
+	SHFL_FN_WRITE,			/* Write new object content. */
+	SHFL_FN_LOCK,			/* Lock/unlock a range in the object. */
+	SHFL_FN_LIST,			/* List object content. */
+	SHFL_FN_INFORMATION,		/* Query/set object information. */
+	/* Note function number 10 is not used! */
+	SHFL_FN_REMOVE = 11,		/* Remove object */
+	SHFL_FN_MAP_FOLDER_OLD,		/* Map folder (legacy) */
+	SHFL_FN_UNMAP_FOLDER,		/* Unmap folder */
+	SHFL_FN_RENAME,			/* Rename object */
+	SHFL_FN_FLUSH,			/* Flush file */
+	SHFL_FN_SET_UTF8,		/* Select UTF8 filename encoding */
Quoted 8 lines

fs/vboxsf/shfl_hostintf.h line 48

Quoted 5 lines
fs/vboxsf/shfl_hostintf.h new file +5 -0
+	SHFL_FN_MAP_FOLDER,		/* Map folder */
+	SHFL_FN_READLINK,		/* Read symlink dest (as of VBox 4.0) */
+	SHFL_FN_SYMLINK,		/* Create symlink (as of VBox 4.0) */
+	SHFL_FN_SET_SYMLINKS,		/* Ask host to show symlinks (as of 4.0) */
+};

If these are protocol numbers that can't be changed, I would assign the value on all of them.

Ok, will do for the next version.

Quoted 2 lines

If they're used by userspace, should they be moved into a uapi header (and the same for the other stuff in this file)?

This is the protocol between the guest and the hypervisor, so it is not uapi.

Quoted 26 lines

fs/vboxsf/super.c line 55

Quoted 11 lines
fs/vboxsf/super.c new file +11 -0
+static const struct fs_parameter_spec vboxsf_param_specs[] = {
+	fsparam_string("nls", opt_nls),
+	fsparam_u32("uid", opt_uid),
+	fsparam_u32("gid", opt_gid),
+	fsparam_u32("ttl", opt_ttl),
+	fsparam_u32oct("dmode", opt_dmode),
+	fsparam_u32oct("fmode", opt_fmode),
+	fsparam_u32oct("dmask", opt_dmask),
+	fsparam_u32oct("fmask", opt_fmask),
+	{}
+};

I would format this with tabs so that everything nicely lines up:

	static const struct fs_parameter_spec vboxsf_param_specs[] = {
		fsparam_string	("nls",		opt_nls),
		fsparam_u32	("uid",		opt_uid),
		fsparam_u32	("gid",		opt_gid),
		fsparam_u32	("ttl",		opt_ttl),
		fsparam_u32oct	("dmode",	opt_dmode),
		fsparam_u32oct	("fmode",	opt_fmode),
		fsparam_u32oct	("dmask",	opt_dmask),
		fsparam_u32oct	("fmask",	opt_fmask),
		{}
	};
Quoted 1 line

but, otherwise, good!

Ok, will fix.

Quoted 8 lines

fs/vboxsf/super.c line 86

Quoted 6 lines
fs/vboxsf/super.c new file +6 -0
+	case opt_uid:
+		ctx->o.uid = result.uint_32;
+		break;
+	case opt_gid:
+		ctx->o.gid = result.uint_32;
+		break;

Should you be using kuid/kgid transforms for the appropriate namespace?

That is probably a good idea, will do for the next version.

Quoted 6 lines
		opts->fs_uid = make_kuid(current_user_ns(), option);
		if (!uid_valid(opts->fs_uid))
			return -EINVAL;

sort of thing (excerpt from fs/fat/inode.c).

Shouldn't this use the user-namespace from the filesystem-context?

Quoted 6 lines

fs/vboxsf/super.c line 89

Quoted 4 lines
fs/vboxsf/super.c new file +3 -0
+	case opt_ttl:
+		ctx->o.ttl = msecs_to_jiffies(result.uint_32);
+		break;

Is 0 valid?

Yes, 0 means to always pass any stat() calls through to the host instead of relying on cached values.

Quoted 15 lines

fs/vboxsf/super.c line 101

Quoted 13 lines
fs/vboxsf/super.c new file +12 -0
+	case opt_dmode:
+		ctx->o.dmode = result.uint_32;
+		break;
+	case opt_fmode:
+		ctx->o.fmode = result.uint_32;
+		break;
+	case opt_dmask:
+		ctx->o.dmask = result.uint_32;
+		break;
+	case opt_fmask:
+		ctx->o.fmask = result.uint_32;
+		break;

Do these need vetting? I guess you kind of do:

Well I could refuse values where (result.uint_32 & 0777) is true here I guess; and then remove the & 0777 below:

Quoted 5 lines
		inode->i_mode =
			sf_g->o.dmode != ~0 ? (sf_g->o.dmode & 0777) : mode;
		inode->i_mode &= ~sf_g->o.dmask;

I'm guessing you're stuck with the mount options?

More or less, yes. Changing them is going to be quite painful from a userspace compat pov.

Quoted 5 lines

fs/vboxsf/vfsmod.h line 24

Quoted 3 lines
fs/vboxsf/vfsmod.h new file +2 -0
+struct vboxsf_options {
+	int ttl;

jiffies are unsigned long.

Ok.

Quoted 4 lines

fs/vboxsf/vfsmod.h line 26

Quoted 2 lines
fs/vboxsf/vfsmod.h new file +2 -0
+	int uid;
+	int gid;

kuid_t & kgid_t.

Ok.

Quoted 8 lines

fs/vboxsf/vfsmod.h line 31

Quoted 6 lines
fs/vboxsf/vfsmod.h new file +5 -0
+	int dmode;
+	int fmode;
+	int dmask;
+	int fmask;
+};

umode_t.

Ok.

Regards,

Hans

#4 lore raw
Quoted 8 lines · Hans de Goede
Quoted 6 lines
		opts->fs_uid = make_kuid(current_user_ns(), option);
		if (!uid_valid(opts->fs_uid))
			return -EINVAL;

sort of thing (excerpt from fs/fat/inode.c).

Shouldn't this use the user-namespace from the filesystem-context?

No. The uid you're given is with regard to the calling process's namespace, not the filesystem's.

David