fs: Add VirtualBox guest shared folder (vboxsf)
Subsystems (2)
v12 cover fs: Add VirtualBox guest shared folder (vboxsf)
Hello Everyone,
Here is a second resend of the 12th version of my cleaned-up / refactored version of the VirtualBox shared-folder VFS driver. It seems that the list does not like patches being over 100000 char, so this time I've trimmed the changelog below the --- in the commit messages, if you still want to see the full changelog, see the end of this coverletter.
This version hopefully addresses all issues pointed out in David Howell's review of v11 (thank you for the review David):
Changes in v12:
-Move make_kuid / make_kgid calls to option parsing time and add
uid_valid / gid_valid checks.
-In init_fs_context call current_uid_gid() to init uid and gid -Validate dmode, fmode, dmask and fmask options during option parsing -Use correct types for various mount option variables (kuid_t, kgid_t, umode_t) -Some small coding-style tweaks
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
---
Full changelog:
Changes in v12:
-Move make_kuid / make_kgid calls to option parsing time and add
uid_valid / gid_valid checks.
-In init_fs_context call current_uid_gid() to init uid and gid -Validate dmode, fmode, dmask and fmask options during option parsing -Use correct types for various mount option variables (kuid_t, kgid_t, umode_t) -Some small coding-style tweaks
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
PATCH RESEND v12 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 v12:
-Move make_kuid / make_kgid calls to option parsing time and add
uid_valid / gid_valid checks.
-In init_fs_context call current_uid_gid() to init uid and gid -Validate dmode, fmode, dmask and fmask options during option parsing -Use correct types for various mount option variables (kuid_t, kgid_t, umode_t) -Some small coding-style tweaks
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
See cover-letter for full changelog including older versions
--- 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 | 498 ++++++++++++++++++++ fs/vboxsf/utils.c | 565 ++++++++++++++++++++++ fs/vboxsf/vboxsf_wrappers.c | 379 +++++++++++++++ fs/vboxsf/vfsmod.h | 140 ++++++ 12 files changed, 3518 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
@@ -17158,6 +17158,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
@@ -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
@@ -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 +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 +498 -0
498 lines not on this page · raw message
fs/vboxsf/utils.c new file +565 -0
551 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 +140 -0
140 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.22.0
Part of the diff is not shown here. View the raw message.
Re: [PATCH v12 resend] fs: Add VirtualBox guest shared folder (vboxsf) support
A couple comments from a quick look:
fs/vboxsf/Makefile line 3
Quoted 7 lines
index 000000000000..fcbd488f9eec
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
All new files, including the build system should have a SPDX tag.
Also please use the modern "foo-y +=" synax instead of "foo-objs :="
fs/vboxsf/Makefile
Quoted 11 lines
fs/vboxsf/Makefile new file +11
-0+ +/** + * sf_dir_open - Open a directory + * @inode: inode + * @file: file + * + * Open a directory. Read the complete content into a buffer. + * + * Returns: + * 0 or negative errno value. + */
Nitpick: I don't think verbose kerneldoc comments on functions implementing VFS or other core kernel methods calls are useful. In many ways they actually are harmful, as they are almost guranteed to be out of date eventually, e.g. in this case if someone finally decided to kill the pointless inode argument using a script.
Quoted 1 line
+static int
sf_dir_open(struct inode *inode, struct file *file)
Why is this a sf_ prefix instead of vboxsf?
fs/vboxsf/Makefile
Quoted 2 lines
fs/vboxsf/Makefile new file +2
-0+{ + struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb);
Any chance to use a more normal naming scheme here, e.g.:
struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+ err = vboxsf_create_at_dentry(file_dentry(file), ¶ms); + if (err == 0) { + if (params.result == SHFL_FILE_EXISTS) { + err = vboxsf_dir_read_all(sf_g, sf_d, params.handle); + if (!err) + file->private_data = sf_d;
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ } else + err = -ENOENT; + + vboxsf_close(sf_g->root, params.handle); + } + + if (err) + vboxsf_dir_info_free(sf_d); + + return err;
Why not normal goto based unwinding here:
err = vboxsf_create_at_dentry(file_dentry(file), ¶ms);
if (err)
goto out_free_dir_info;
if (params.result == SHFL_FILE_EXISTS) {
err = -ENOENT;
goto out_close;
}
err = vboxsf_dir_read_all(sf_g, sf_d, params.handle);
if (err)
goto out_close;
file->private_data = sf_d;
return 0;
out_close:
vboxsf_close(sf_g->root, params.handle);
out_free_dir_info:
vboxsf_dir_info_free(sf_d);
return err;fs/vboxsf/Makefile
Quoted 2 lines
fs/vboxsf/Makefile new file +2
-0+static int sf_getdent(struct file *dir, loff_t pos, + char d_name[NAME_MAX], unsigned int *d_type)
Array sizing in parameters in C is ignored, so this is a bit mislead. I'd just use a normal char pointer. But more importantly it would seem saner to pass down the dir context, so that we can just call dir_emit on the original string and avoid a pointless memcpy for the non-nls case.
Quoted 4 lines
fs/vboxsf/Makefile new file +4
-0+static int sf_dentry_revalidate(struct dentry *dentry, unsigned int flags) +{ + if (flags & LOOKUP_RCU) + return -ECHILD;
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ + if (d_really_is_positive(dentry)) + return vboxsf_inode_revalidate(dentry) == 0; + else + return vboxsf_stat_dentry(dentry, NULL) == -ENOENT; +} + +const struct dentry_operations vboxsf_dentry_ops = { + .d_revalidate = sf_dentry_revalidate +};
I'd really like to see Al look over all the dentry stuff, and the by path operations later on, as some of this looks rather odd, but It's been a while since I have been fluent in that area.
fs/vboxsf/Makefile
Quoted 1 line
fs/vboxsf/Makefile new file +1
-0+ sf_i = GET_INODE_INFO(inode);
The normal name for this would be VOXSF_I.
fs/vboxsf/Makefile
Quoted 12 lines
fs/vboxsf/Makefile new file +12
-0+/** + * sf_create_aux - Create a new regular file / directory + * @parent: inode of the parent directory + * @dentry: directory entry + * @mode: file mode + * @is_dir: true if directory, false otherwise + * + * Returns: + * 0 or negative errno value. + */ +static int sf_create_aux(struct inode *parent, struct dentry *dentry, + umode_t mode, int is_dir)
Where does the aux come from? The name sounds a little weird.
Quoted 15 lines
fs/vboxsf/Makefile new file +15
-0+/** + * sf_unlink_aux - Remove a regular file + * @parent: inode of the parent directory + * @dentry: directory entry + * + * Returns: + * 0 or negative errno value. + */ +static int sf_unlink(struct inode *parent, struct dentry *dentry) +{ + return sf_unlink_aux(parent, dentry, 0); +} + +/** + * sf_rmdir - Remove a directory
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ * @parent: inode of the parent directory + * @dentry: directory entry + * + * Returns: + * 0 or negative errno value. + */ +static int sf_rmdir(struct inode *parent, struct dentry *dentry) +{ + return sf_unlink_aux(parent, dentry, 1); +}
You can just set both methods to the same function and check S_IDIR to handle rmdir vs unlink.
fs/vboxsf/Makefile
Quoted 4 lines
fs/vboxsf/Makefile new file +4
-0+ if (IS_ERR(new_path)) { + __putname(old_path); + return PTR_ERR(new_path); + }
Use a goto to share the cleanup code?
Quoted 5 lines
fs/vboxsf/Makefile new file +5
-0+static ssize_t sf_reg_read(struct file *file, char __user *buf, size_t size, + loff_t *off) +{ + struct sf_handle *sf_handle = file->private_data; + u64 pos = *off;
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ u32 nread; + int err; + + if (!size) + return 0; + + if (size > SHFL_MAX_RW_COUNT) + nread = SHFL_MAX_RW_COUNT; + else + nread = size;
Use min/min_t, please.
fs/vboxsf/Makefile
Quoted 9 lines
fs/vboxsf/Makefile new file +9
-0+ + err = vboxsf_read(sf_handle->root, sf_handle->handle, pos, &nread, + (uintptr_t)buf, true); + if (err) + return err; + + *off += nread; + return nread; +}
Please implement read_iter/write_iter for a new file system. Also these casts to uintptr_t for a call that reads data look very odd.
Quoted 14 lines
fs/vboxsf/Makefile new file +14
-0+static ssize_t sf_reg_write(struct file *file, const char __user *buf, + size_t size, loff_t *off) +{ + struct inode *inode = file_inode(file); + struct sf_inode_info *sf_i = GET_INODE_INFO(inode); + struct sf_handle *sf_handle = file->private_data; + u32 nwritten; + u64 pos; + int err; + + if (file->f_flags & O_APPEND) + pos = i_size_read(inode); + else + pos = *off;
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ + if (!size) + return 0; + + if (size > SHFL_MAX_RW_COUNT) + nwritten = SHFL_MAX_RW_COUNT; + else + nwritten = size; + + /* Make sure any pending writes done through mmap are flushed */
Why?
fs/vboxsf/Makefile
Quoted 3 lines
fs/vboxsf/Makefile new file +3
-0+ err = filemap_fdatawait_range(inode->i_mapping, pos, pos + nwritten); + if (err) + return err;
Also this whole write function seems to miss i_rwsem.
fs/vboxsf/Makefile
Quoted 12 lines
fs/vboxsf/Makefile new file +12
-0+ + err = vboxsf_write(sf_handle->root, sf_handle->handle, pos, &nwritten, + (uintptr_t)buf, true); + if (err) + return err; + + if (pos + nwritten > i_size_read(inode)) + i_size_write(inode, pos + nwritten); + + /* Invalidate page-cache so that mmap using apps see the changes too */ + invalidate_mapping_pages(inode->i_mapping, pos >> PAGE_SHIFT, + (pos + nwritten) >> PAGE_SHIFT);
I think you really need to use the page cache for regular writes insted of coming up with your own ad-hoc cache coherency scheme.
fs/vboxsf/Makefile
Quoted 13 lines
fs/vboxsf/Makefile new file +13
-0+static vm_fault_t sf_page_mkwrite(struct vm_fault *vmf) +{ + struct page *page = vmf->page; + struct inode *inode = file_inode(vmf->vma->vm_file); + + lock_page(page); + if (page->mapping != inode->i_mapping) { + unlock_page(page); + return VM_FAULT_NOPAGE; + } + + return VM_FAULT_LOCKED; +}
What is this supposed to help with?
fs/vboxsf/Makefile
Quoted 1 line
fs/vboxsf/Makefile new file +1
-0+ SET_GLOB_INFO(sb, sf_g);
No need to have a wrapper for this assignment.
fs/vboxsf/Makefile
Quoted 3 lines
fs/vboxsf/Makefile new file +3
-0+static void sf_inode_init_once(void *data) +{ + struct sf_inode_info *sf_i = (struct sf_inode_info *)data;
No need for a cast here.
Quoted 5 lines
fs/vboxsf/Makefile new file +5
-0+static void sf_i_callback(struct rcu_head *head) +{ + struct inode *inode = container_of(head, struct inode, i_rcu); + struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb); +
fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ spin_lock(&sf_g->ino_idr_lock); + idr_remove(&sf_g->ino_idr, inode->i_ino); + spin_unlock(&sf_g->ino_idr_lock); + kmem_cache_free(sf_inode_cachep, GET_INODE_INFO(inode)); +} + +static void sf_destroy_inode(struct inode *inode) +{ + call_rcu(&inode->i_rcu, sf_i_callback); +}
I think you want to implement the new free_inode callback instead.
Re: [PATCH v12 resend] fs: Add VirtualBox guest shared folder (vboxsf) support
Hi Christoph,
Quoted 1 line · Christoph Hellwig
A couple comments from a quick look:
Thank you for taking the time to take a look at this.
Quoted 11 lines
fs/vboxsf/Makefile line 3
Quoted 7 lines
index 000000000000..fcbd488f9eec
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.oAll new files, including the build system should have a SPDX tag.
Also please use the modern "foo-y +=" synax instead of "foo-objs :="
Ok, I will fix both for the next version.
Quoted 18 lines
fs/vboxsf/Makefile
Quoted 11 lines
fs/vboxsf/Makefile new file +11
-0+ +/** + * sf_dir_open - Open a directory + * @inode: inode + * @file: file + * + * Open a directory. Read the complete content into a buffer. + * + * Returns: + * 0 or negative errno value. + */Nitpick: I don't think verbose kerneldoc comments on functions implementing VFS or other core kernel methods calls are useful. In many ways they actually are harmful, as they are almost guranteed to be out of date eventually, e.g. in this case if someone finally decided to kill the pointless inode argument using a script.
Quoted 3 lines
Quoted 1 line
+static int
sf_dir_open(struct inode *inode, struct file *file)Why is this a
sf_prefix instead of vboxsf?
This case is based on the out-of-tree vboxsf driver which VirtualBox upstream has been shipping for years (as out of tree code) together as part of their Linux guest-additions. I've been working on getting all the guest-drivers mainlined (vboxsf is the last one mmissing) so that distros can integrate properly as vbox guest without needing out of tree modules.
All the original code used the sf (shared folder) prefix. I've prefixed most new / cleaned up functions with vboxsf, this is left over from the old code. I will go over the code and rename the remaining sf_ prefixed things for the next version.
Quoted 7 lines
fs/vboxsf/Makefile
Quoted 3 lines
fs/vboxsf/Makefile new file +2
-0+{ + struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb);Any chance to use a more normal naming scheme here, e.g.:
struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
Sure, will fix for the next version.
Quoted 31 lines
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+ err = vboxsf_create_at_dentry(file_dentry(file), ¶ms); + if (err == 0) { + if (params.result == SHFL_FILE_EXISTS) { + err = vboxsf_dir_read_all(sf_g, sf_d, params.handle); + if (!err) + file->private_data = sf_d;fs/vboxsf/Makefile
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+ } else + err = -ENOENT; + + vboxsf_close(sf_g->root, params.handle); + } + + if (err) + vboxsf_dir_info_free(sf_d); + + return err;Why not normal goto based unwinding here:
err = vboxsf_create_at_dentry(file_dentry(file), ¶ms); if (err) goto out_free_dir_info; if (params.result == SHFL_FILE_EXISTS) { err = -ENOENT; goto out_close; } err = vboxsf_dir_read_all(sf_g, sf_d, params.handle); if (err) goto out_close;
Quoted 7 lines
file->private_data = sf_d; return 0; out_close: vboxsf_close(sf_g->root, params.handle); out_free_dir_info: vboxsf_dir_info_free(sf_d); return err;
Ack, will fix.
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 2 lines
fs/vboxsf/Makefile new file +2
-0+static int sf_getdent(struct file *dir, loff_t pos, + char d_name[NAME_MAX], unsigned int *d_type)Array sizing in parameters in C is ignored, so this is a bit mislead. I'd just use a normal char pointer. But more importantly it would seem saner to pass down the dir context, so that we can just call
dir_emiton the original string and avoid a pointless memcpy for the non-nls case.
Ok, I will rework this as you suggest for the next version.
Quoted 10 lines
Quoted 10 lines
fs/vboxsf/Makefile new file +10
-0+static int sf_dentry_revalidate(struct dentry *dentry, unsigned int flags) +{ + if (flags & LOOKUP_RCU) + return -ECHILD; + + if (d_really_is_positive(dentry)) + return vboxsf_inode_revalidate(dentry) == 0; + else + return vboxsf_stat_dentry(dentry, NULL) == -ENOENT; +}
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 4 lines
fs/vboxsf/Makefile new file +4
-0+ +const struct dentry_operations vboxsf_dentry_ops = { + .d_revalidate = sf_dentry_revalidate +};I'd really like to see Al look over all the dentry stuff, and the by path operations later on, as some of this looks rather odd, but It's been a while since I have been fluent in that area.
FWIW Al did go over the dentry stuff in an older version.
Quoted 4 lines
fs/vboxsf/Makefile
Quoted 2 lines
fs/vboxsf/Makefile new file +1
-0+ sf_i = GET_INODE_INFO(inode);The normal name for this would be
VOXSF_I.
Ok.
Quoted 6 lines
Quoted 6 lines
+/**
+ *sf_create_aux- Create a new regular file / directory
+ * @parent: inode of the parent directory
+ * @dentry: directory entry
+ * @mode: file mode
+ * @is_dir: true if directory, false otherwise
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 6 lines
+ *
+ * Returns:
+ * 0 or negative errno value.
+ */fs/vboxsf/Makefile new file +2
-0+static int sf_create_aux(struct inode *parent, struct dentry *dentry, + umode_t mode, int is_dir)Where does the aux come from? The name sounds a little weird.
I honestly don't know, I will drop this for the next version.
Quoted 20 lines
Quoted 20 lines
fs/vboxsf/Makefile new file +20
-0+/** + * sf_unlink_aux - Remove a regular file + * @parent: inode of the parent directory + * @dentry: directory entry + * + * Returns: + * 0 or negative errno value. + */ +static int sf_unlink(struct inode *parent, struct dentry *dentry) +{ + return sf_unlink_aux(parent, dentry, 0); +} + +/** + * sf_rmdir - Remove a directory + * @parent: inode of the parent directory + * @dentry: directory entry + * + * Returns: + * 0 or negative errno value.
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 5 lines
+ */
fs/vboxsf/Makefile new file +4
-0+static int sf_rmdir(struct inode *parent, struct dentry *dentry) +{ + return sf_unlink_aux(parent, dentry, 1); +}You can just set both methods to the same function and check
S_IDIRto handle rmdir vs unlink.
Ok, will fix.
Quoted 6 lines
fs/vboxsf/Makefile
Quoted 4 lines
fs/vboxsf/Makefile new file +4
-0+ if (IS_ERR(new_path)) { + __putname(old_path); + return PTR_ERR(new_path); + }Use a goto to share the cleanup code?
Ok, will fix.
Quoted 9 lines
Quoted 9 lines
fs/vboxsf/Makefile new file +9
-0+static ssize_t sf_reg_read(struct file *file, char __user *buf, size_t size, + loff_t *off) +{ + struct sf_handle *sf_handle = file->private_data; + u64 pos = *off; + u32 nread; + int err; + + if (!size)
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+ return 0; + + if (size > SHFL_MAX_RW_COUNT) + nread = SHFL_MAX_RW_COUNT; + else + nread = size;Use min/min_t, please.
Ok, will fix.
Quoted 11 lines
fs/vboxsf/Makefile
Quoted 9 lines
fs/vboxsf/Makefile new file +9
-0+ + err = vboxsf_read(sf_handle->root, sf_handle->handle, pos, &nread, + (uintptr_t)buf, true); + if (err) + return err; + + *off += nread; + return nread; +}Please implement read_iter/write_iter for a new file system.
Ok, will fix.
Quoted 2 lines
Also these casts to
uintptr_tfor a call that reads data look very odd.
Yes, as I already discussed with Al, that is because vboxsf_read can be (and is) used with both kernel and userspace buffer pointers.
In case of a userspace pointer the underlying IPC code to the host takes care of copy to / from user for us. That IPC code can also be used from userspace through ioctls on /dev/vboxguest, so the handling of both in kernel and userspace addresses is something which it must be able to handle anyways, at which point we might as well use it in vboxsf too.
But since both Al and you pointed this out as being ugly, I will add 2 separate vboxsf_read_user and vboxsf_read_kernel functions for the next version, then the cast (and the true flag) can both go away.
Quoted 19 lines
Quoted 19 lines
fs/vboxsf/Makefile new file +18
-0+static ssize_t sf_reg_write(struct file *file, const char __user *buf, + size_t size, loff_t *off) +{ + struct inode *inode = file_inode(file); + struct sf_inode_info *sf_i = GET_INODE_INFO(inode); + struct sf_handle *sf_handle = file->private_data; + u32 nwritten; + u64 pos; + int err; + + if (file->f_flags & O_APPEND) + pos = i_size_read(inode); + else + pos = *off; + + if (!size) + return 0; +
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+ if (size > SHFL_MAX_RW_COUNT) + nwritten = SHFL_MAX_RW_COUNT; + else + nwritten = size; + + /* Make sure any pending writes done through mmap are flushed */Why?
I believe that if we were doing everything through the page-cache then a regular write to the same range as a write done through mmap, with the regular write happening after (in time) the mmap write, will overwrite the mmap written data, we want the same behavior here.
Quoted 5 lines
fs/vboxsf/Makefile
Quoted 3 lines
fs/vboxsf/Makefile new file +3
-0+ err = filemap_fdatawait_range(inode->i_mapping, pos, pos + nwritten); + if (err) + return err;Also this whole write function seems to miss
i_rwsem.
Hmm, I do not see e.g. v9fs_file_write_iter take that either, nor a couple of other similar not block-backed filesystems. Will this still be necessary after converting to the iter interfaces?
Quoted 7 lines
Quoted 7 lines
fs/vboxsf/Makefile new file +7
-0+ + err = vboxsf_write(sf_handle->root, sf_handle->handle, pos, &nwritten, + (uintptr_t)buf, true); + if (err) + return err; + + if (pos + nwritten > i_size_read(inode))
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 5 lines
fs/vboxsf/Makefile new file +5
-0+ i_size_write(inode, pos + nwritten); + + /* Invalidate page-cache so that mmap using apps see the changes too */ + invalidate_mapping_pages(inode->i_mapping, pos >> PAGE_SHIFT, + (pos + nwritten) >> PAGE_SHIFT);I think you really need to use the page cache for regular writes insted of coming up with your own ad-hoc cache coherency scheme.
The problem is that the IPC to the host which we build upon only offers regular read / write calls. So the most consistent (also cache coherent) mapping which we can offer is to directly mapping read -> read and wrtie->write without the pagecache. Ideally we would be able to just say sorry cannot do mmap, but too much apps rely on mmap and the out of tree driver has this mmap "emulation" which means not offering it in the mainline version would be a serious regression.
In essence this is the same situation as a bunch of network filesystems are in and I've looked at several for inspiration. Looking again at e.g. v9fs_file_write_iter it does similar regular read -> read mapping with invalidation of the page-cache for mmap users.
Quoted 7 lines
Quoted 7 lines
fs/vboxsf/Makefile new file +7
-0+static vm_fault_t sf_page_mkwrite(struct vm_fault *vmf) +{ + struct page *page = vmf->page; + struct inode *inode = file_inode(vmf->vma->vm_file); + + lock_page(page); + if (page->mapping != inode->i_mapping) {
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+ unlock_page(page); + return VM_FAULT_NOPAGE; + } + + return VM_FAULT_LOCKED; +}What is this supposed to help with?
I must admit that I've mostly cargo-culted this from other fs code such as the 9p code, or the cifs code which has:
/*
* If the page is mmap'ed into a process' page tables, then we need to make
* sure that it doesn't change while being written back.
*/
static vm_fault_tcifs_page_mkwrite(struct vm_fault *vmf)
{
struct page *page = vmf->page;
lock_page(page);
return VM_FAULT_LOCKED;
}The if (page->mapping != inode->i_mapping) is used in several places including the 9p code, bit as you can see no in the cifs code. I couldn't really find a rational for that check, so I'm fine with dropping that check.
Quoted 3 lines
fs/vboxsf/Makefile
Quoted 1 line
fs/vboxsf/Makefile new file +1
-0+ SET_GLOB_INFO(sb, sf_g);No need to have a wrapper for this assignment.
Ok.
Quoted 5 lines
fs/vboxsf/Makefile
Quoted 3 lines
fs/vboxsf/Makefile new file +3
-0+static void sf_inode_init_once(void *data) +{ + struct sf_inode_info *sf_i = (struct sf_inode_info *)data;No need for a cast here.
Ok.
Quoted 9 lines
Quoted 9 lines
fs/vboxsf/Makefile new file +9
-0+static void sf_i_callback(struct rcu_head *head) +{ + struct inode *inode = container_of(head, struct inode, i_rcu); + struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb); + + spin_lock(&sf_g->ino_idr_lock); + idr_remove(&sf_g->ino_idr, inode->i_ino); + spin_unlock(&sf_g->ino_idr_lock); + kmem_cache_free(sf_inode_cachep, GET_INODE_INFO(inode));
Quoted 8 lines
fs/vboxsf/Makefile
Quoted 6 lines
fs/vboxsf/Makefile new file +6
-0+} + +static void sf_destroy_inode(struct inode *inode) +{ + call_rcu(&inode->i_rcu, sf_i_callback); +}I think you want to implement the new
free_inodecallback instead.
I will take a look at this.
Regards,
Hans
Re: [PATCH v12 resend] fs: Add VirtualBox guest shared folder (vboxsf) support
Quoted 12 lines · Hans de Goede
Quoted 2 lines
Also these casts to
uintptr_tfor a call that reads data look very odd.Yes, as I already discussed with Al, that is because
vboxsf_readcan be (and is) used with both kernel and userspace buffer pointers.In case of a userspace pointer the underlying IPC code to the host takes care of copy to / from user for us. That IPC code can also be used from userspace through ioctls on
/dev/vboxguest, so the handling of both in kernel and userspace addresses is something which it must be able to handle anyways, at which point we might as well use it in vboxsf too.
Quoted 3 lines
But since both Al and you pointed this out as being ugly, I will add 2 separate
vboxsf_read_userandvboxsf_read_kernelfunctions for the next version, then the cast (and the true flag) can both go away.
What might be even better is to pass a struct iov_iter to the low-level function. That gets you 90% of implementing the read_iter and write_iter methods, as well as a versatile low-level primite that can deal with kernel and user address, as well as pages.
Quoted 8 lines
Quoted 3 lines
fs/vboxsf/file.c line 87
Quoted 1 line
fs/vboxsf/file.c new file +1
-0+/* Make sure any pending writes done through mmap are flushed */ Why?
I believe that if we were doing everything through the page-cache then a regular write to the same range as a write done through mmap, with the regular write happening after (in time) the mmap write, will overwrite the mmap written data, we want the same behavior here.
But what happens if you mmap and write at the same or at least barely the same time.
Quoted 10 lines
Quoted 6 lines
fs/vboxsf/file.c line 90
Quoted 4 lines
fs/vboxsf/file.c new file +3
-0+ err =filemap_fdatawait_range (inode->i_mapping, pos, pos + nwritten); +if (err) +return err;Also this whole write function seems to miss
i_rwsem.Hmm, I do not see e.g.
v9fs_file_write_itertake that either, nor a couple of other similar not block-backed filesystems. Will this still be necessary after converting to the iter interfaces?
Yes.
Quoted 8 lines
The problem is that the IPC to the host which we build upon only offers regular read / write calls. So the most consistent (also cache coherent) mapping which we can offer is to directly mapping read -> read and
wrtie->writewithout the pagecache. Ideally we would be able to just say sorry cannot do mmap, but too much apps rely on mmap and the out of tree driver has this mmap "emulation" which means not offering it in the mainline version would be a serious regression.
Quoted 4 lines
In essence this is the same situation as a bunch of network filesystems are in and I've looked at several for inspiration. Looking again at e.g.
v9fs_file_write_iterit does similar regular read -> read mapping with invalidation of the page-cache for mmap users.
v9 is probably not a good idea to copy in general. While not the best idea to copy directly either I'd rather look at nfs - that is another protocol without a real distributed lock manager, but at least the NFS close to open semantics are reasonably well defined and allow using the pagecache.
Quoted 16 lines
I must admit that I've mostly cargo-culted this from other fs code such as the 9p code, or the cifs code which has:
/*
* If the page is mmap'ed into a process' page tables, then we need to make
* sure that it doesn't change while being written back.
*/
staticvm_fault_tcifs_page_mkwrite(struct vm_fault *vmf){ struct page *page = vmf->page; lock_page(page); return VM_FAULT_LOCKED; }
Quoted 3 lines
The if (
page->mapping!=inode->i_mapping) is used in several places including the 9p code, bit as you can see no in the cifs code. I couldn't really find a rational for that check, so I'm fine with dropping that check.
If you don't implement ->page_mkwrite the caller will just lock the page for you..
Re: [PATCH v12 resend] fs: Add VirtualBox guest shared folder (vboxsf) support
Hi,
On 12-08-19 16:17, Christoph Hellwig wrote:
<snip>
Quoted 13 lines
Quoted 8 lines
The problem is that the IPC to the host which we build upon only offers regular read / write calls. So the most consistent (also cache coherent) mapping which we can offer is to directly mapping read -> read and
wrtie->writewithout the pagecache. Ideally we would be able to just say sorry cannot do mmap, but too much apps rely on mmap and the out of tree driver has this mmap "emulation" which means not offering it in the mainline version would be a serious regression.Quoted 4 lines
In essence this is the same situation as a bunch of network filesystems are in and I've looked at several for inspiration. Looking again at e.g.
v9fs_file_write_iterit does similar regular read -> read mapping with invalidation of the page-cache for mmap users.
Quoted 5 lines
v9 is probably not a good idea to copy in general. While not the best idea to copy directly either I'd rather look at nfs - that is another protocol without a real distributed lock manager, but at least the NFS close to open semantics are reasonably well defined and allow using the pagecache.
Ok, I've been taking a quick peek at always using the page-cache for writes, like NFS is doing.
One scenario here which I still have questions about is normal write syscalls on a file opened in append mode. Currently I'm relying on passing through the append flag to the host while opening the file.
This is fine for address_space_operations.write_end which AFAICT will be used in case of implementing the write_iter callback through generic_perform_write, this is fine for write_end since in write_end I have access to file->private_data and thus to the IPC handle representing the open call with the append flag set, so I do not need to worry about the host having changed the file underneath us, since the host will make sure the write gets appended itself.
But what about address_space_operations.writepage? I guess this will never get called as the result of a write call on a file with the append flag set, right ? So I should have at least one handle around in the list of open handles for the inode, which does not have the append flag set, so which I can safely use to writeback dirty pages coming in through writepage(), right ?
Hmm, looking at my current vboxsf writepage code I see that I already only allow using handles which were opened without the append flag, so I'm pretty sure that I got this right, still if you can confirm that I've got this right, that would be great.
And mmap of a file with the append flag set is not supported, so we should be good there.
Regards,
Hans