1 // SPDX-License-Identifier: MIT
3 * VirtualBox Guest Shared Folders support: Virtual File System.
5 * Module initialization/finalization
6 * File system registration/deregistration
8 * Few utility functions
10 * Copyright (C) 2006-2018 Oracle Corporation
13 #include <linux/idr.h>
14 #include <linux/fs_parser.h>
15 #include <linux/magic.h>
16 #include <linux/module.h>
17 #include <linux/nls.h>
18 #include <linux/statfs.h>
19 #include <linux/vbox_utils.h>
22 #define VBOXSF_SUPER_MAGIC 0x786f4256 /* 'VBox' little endian */
24 static const unsigned char VBSF_MOUNT_SIGNATURE[4] = { '\000', '\377', '\376',
27 static int follow_symlinks;
28 module_param(follow_symlinks, int, 0444);
29 MODULE_PARM_DESC(follow_symlinks,
30 "Let host resolve symlinks rather than showing them");
32 static DEFINE_IDA(vboxsf_bdi_ida);
33 static DEFINE_MUTEX(vboxsf_setup_mutex);
34 static bool vboxsf_setup_done;
35 static struct super_operations vboxsf_super_ops; /* forward declaration */
36 static struct kmem_cache *vboxsf_inode_cachep;
38 static char * const vboxsf_default_nls = CONFIG_NLS_DEFAULT;
40 enum { opt_nls, opt_uid, opt_gid, opt_ttl, opt_dmode, opt_fmode,
41 opt_dmask, opt_fmask };
43 static const struct fs_parameter_spec vboxsf_fs_parameters[] = {
44 fsparam_string ("nls", opt_nls),
45 fsparam_uid ("uid", opt_uid),
46 fsparam_gid ("gid", opt_gid),
47 fsparam_u32 ("ttl", opt_ttl),
48 fsparam_u32oct ("dmode", opt_dmode),
49 fsparam_u32oct ("fmode", opt_fmode),
50 fsparam_u32oct ("dmask", opt_dmask),
51 fsparam_u32oct ("fmask", opt_fmask),
55 static int vboxsf_parse_param(struct fs_context *fc, struct fs_parameter *param)
57 struct vboxsf_fs_context *ctx = fc->fs_private;
58 struct fs_parse_result result;
61 opt = fs_parse(fc, vboxsf_fs_parameters, param, &result);
67 if (ctx->nls_name || fc->purpose != FS_CONTEXT_FOR_MOUNT) {
68 vbg_err("vboxsf: Cannot reconfigure nls option\n");
71 ctx->nls_name = param->string;
75 ctx->o.uid = result.uid;
78 ctx->o.gid = result.gid;
81 ctx->o.ttl = msecs_to_jiffies(result.uint_32);
84 if (result.uint_32 & ~0777)
86 ctx->o.dmode = result.uint_32;
87 ctx->o.dmode_set = true;
90 if (result.uint_32 & ~0777)
92 ctx->o.fmode = result.uint_32;
93 ctx->o.fmode_set = true;
96 if (result.uint_32 & ~07777)
98 ctx->o.dmask = result.uint_32;
101 if (result.uint_32 & ~07777)
103 ctx->o.fmask = result.uint_32;
112 static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
114 struct vboxsf_fs_context *ctx = fc->fs_private;
115 struct shfl_string *folder_name, root_path;
116 struct vboxsf_sbi *sbi;
117 struct dentry *droot;
126 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
131 idr_init(&sbi->ino_idr);
132 spin_lock_init(&sbi->ino_idr_lock);
133 sbi->next_generation = 1;
136 /* Load nls if not utf8 */
137 nls_name = ctx->nls_name ? ctx->nls_name : vboxsf_default_nls;
138 if (strcmp(nls_name, "utf8") != 0) {
139 if (nls_name == vboxsf_default_nls)
140 sbi->nls = load_nls_default();
142 sbi->nls = load_nls(nls_name);
145 vbg_err("vboxsf: Count not load '%s' nls\n", nls_name);
147 goto fail_destroy_idr;
151 sbi->bdi_id = ida_alloc(&vboxsf_bdi_ida, GFP_KERNEL);
152 if (sbi->bdi_id < 0) {
157 err = super_setup_bdi_name(sb, "vboxsf-%d", sbi->bdi_id);
160 sb->s_bdi->ra_pages = 0;
161 sb->s_bdi->io_pages = 0;
163 /* Turn source into a shfl_string and map the folder */
164 size = strlen(fc->source) + 1;
165 folder_name = kmalloc(SHFLSTRING_HEADER_SIZE + size, GFP_KERNEL);
170 folder_name->size = size;
171 folder_name->length = size - 1;
172 strscpy(folder_name->string.utf8, fc->source, size);
173 err = vboxsf_map_folder(folder_name, &sbi->root);
176 vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
181 root_path.length = 1;
183 root_path.string.utf8[0] = '/';
184 root_path.string.utf8[1] = 0;
185 err = vboxsf_stat(sbi, &root_path, &sbi->root_info);
189 sb->s_magic = VBOXSF_SUPER_MAGIC;
190 sb->s_blocksize = 1024;
191 sb->s_maxbytes = MAX_LFS_FILESIZE;
192 sb->s_op = &vboxsf_super_ops;
193 sb->s_d_op = &vboxsf_dentry_ops;
195 iroot = iget_locked(sb, 0);
200 vboxsf_init_inode(sbi, iroot, &sbi->root_info, false);
201 unlock_new_inode(iroot);
203 droot = d_make_root(iroot);
214 vboxsf_unmap_folder(sbi->root);
216 if (sbi->bdi_id >= 0)
217 ida_free(&vboxsf_bdi_ida, sbi->bdi_id);
219 unload_nls(sbi->nls);
221 idr_destroy(&sbi->ino_idr);
226 static void vboxsf_inode_init_once(void *data)
228 struct vboxsf_inode *sf_i = data;
230 mutex_init(&sf_i->handle_list_mutex);
231 inode_init_once(&sf_i->vfs_inode);
234 static struct inode *vboxsf_alloc_inode(struct super_block *sb)
236 struct vboxsf_inode *sf_i;
238 sf_i = alloc_inode_sb(sb, vboxsf_inode_cachep, GFP_NOFS);
242 sf_i->force_restat = 0;
243 INIT_LIST_HEAD(&sf_i->handle_list);
245 return &sf_i->vfs_inode;
248 static void vboxsf_free_inode(struct inode *inode)
250 struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
253 spin_lock_irqsave(&sbi->ino_idr_lock, flags);
254 idr_remove(&sbi->ino_idr, inode->i_ino);
255 spin_unlock_irqrestore(&sbi->ino_idr_lock, flags);
256 kmem_cache_free(vboxsf_inode_cachep, VBOXSF_I(inode));
259 static void vboxsf_put_super(struct super_block *sb)
261 struct vboxsf_sbi *sbi = VBOXSF_SBI(sb);
263 vboxsf_unmap_folder(sbi->root);
264 if (sbi->bdi_id >= 0)
265 ida_free(&vboxsf_bdi_ida, sbi->bdi_id);
267 unload_nls(sbi->nls);
270 * vboxsf_free_inode uses the idr, make sure all delayed rcu free
271 * inodes are flushed.
274 idr_destroy(&sbi->ino_idr);
278 static int vboxsf_statfs(struct dentry *dentry, struct kstatfs *stat)
280 struct super_block *sb = dentry->d_sb;
281 struct shfl_volinfo shfl_volinfo;
282 struct vboxsf_sbi *sbi;
286 sbi = VBOXSF_SBI(sb);
287 buf_len = sizeof(shfl_volinfo);
288 err = vboxsf_fsinfo(sbi->root, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
289 &buf_len, &shfl_volinfo);
293 stat->f_type = VBOXSF_SUPER_MAGIC;
294 stat->f_bsize = shfl_volinfo.bytes_per_allocation_unit;
296 do_div(shfl_volinfo.total_allocation_bytes,
297 shfl_volinfo.bytes_per_allocation_unit);
298 stat->f_blocks = shfl_volinfo.total_allocation_bytes;
300 do_div(shfl_volinfo.available_allocation_bytes,
301 shfl_volinfo.bytes_per_allocation_unit);
302 stat->f_bfree = shfl_volinfo.available_allocation_bytes;
303 stat->f_bavail = shfl_volinfo.available_allocation_bytes;
305 stat->f_files = 1000;
307 * Don't return 0 here since the guest may then think that it is not
308 * possible to create any more files.
310 stat->f_ffree = 1000000;
311 stat->f_fsid.val[0] = 0;
312 stat->f_fsid.val[1] = 0;
313 stat->f_namelen = 255;
317 static struct super_operations vboxsf_super_ops = {
318 .alloc_inode = vboxsf_alloc_inode,
319 .free_inode = vboxsf_free_inode,
320 .put_super = vboxsf_put_super,
321 .statfs = vboxsf_statfs,
324 static int vboxsf_setup(void)
328 mutex_lock(&vboxsf_setup_mutex);
330 if (vboxsf_setup_done)
333 vboxsf_inode_cachep =
334 kmem_cache_create("vboxsf_inode_cache",
335 sizeof(struct vboxsf_inode), 0,
336 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
337 vboxsf_inode_init_once);
338 if (!vboxsf_inode_cachep) {
343 err = vboxsf_connect();
345 vbg_err("vboxsf: err %d connecting to guest PCI-device\n", err);
346 vbg_err("vboxsf: make sure you are inside a VirtualBox VM\n");
347 vbg_err("vboxsf: and check dmesg for vboxguest errors\n");
348 goto fail_free_cache;
351 err = vboxsf_set_utf8();
353 vbg_err("vboxsf_setutf8 error %d\n", err);
354 goto fail_disconnect;
357 if (!follow_symlinks) {
358 err = vboxsf_set_symlinks();
360 vbg_warn("vboxsf: Unable to show symlinks: %d\n", err);
363 vboxsf_setup_done = true;
365 mutex_unlock(&vboxsf_setup_mutex);
371 kmem_cache_destroy(vboxsf_inode_cachep);
373 mutex_unlock(&vboxsf_setup_mutex);
377 static int vboxsf_parse_monolithic(struct fs_context *fc, void *data)
379 if (data && !memcmp(data, VBSF_MOUNT_SIGNATURE, 4)) {
380 vbg_err("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n");
384 return generic_parse_monolithic(fc, data);
387 static int vboxsf_get_tree(struct fs_context *fc)
391 err = vboxsf_setup();
395 return get_tree_nodev(fc, vboxsf_fill_super);
398 static int vboxsf_reconfigure(struct fs_context *fc)
400 struct vboxsf_sbi *sbi = VBOXSF_SBI(fc->root->d_sb);
401 struct vboxsf_fs_context *ctx = fc->fs_private;
402 struct inode *iroot = fc->root->d_sb->s_root->d_inode;
404 /* Apply changed options to the root inode */
406 vboxsf_init_inode(sbi, iroot, &sbi->root_info, true);
411 static void vboxsf_free_fc(struct fs_context *fc)
413 struct vboxsf_fs_context *ctx = fc->fs_private;
415 kfree(ctx->nls_name);
419 static const struct fs_context_operations vboxsf_context_ops = {
420 .free = vboxsf_free_fc,
421 .parse_param = vboxsf_parse_param,
422 .parse_monolithic = vboxsf_parse_monolithic,
423 .get_tree = vboxsf_get_tree,
424 .reconfigure = vboxsf_reconfigure,
427 static int vboxsf_init_fs_context(struct fs_context *fc)
429 struct vboxsf_fs_context *ctx;
431 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
435 current_uid_gid(&ctx->o.uid, &ctx->o.gid);
437 fc->fs_private = ctx;
438 fc->ops = &vboxsf_context_ops;
442 static struct file_system_type vboxsf_fs_type = {
443 .owner = THIS_MODULE,
445 .init_fs_context = vboxsf_init_fs_context,
446 .kill_sb = kill_anon_super
449 /* Module initialization/finalization handlers */
450 static int __init vboxsf_init(void)
452 return register_filesystem(&vboxsf_fs_type);
455 static void __exit vboxsf_fini(void)
457 unregister_filesystem(&vboxsf_fs_type);
459 mutex_lock(&vboxsf_setup_mutex);
460 if (vboxsf_setup_done) {
463 * Make sure all delayed rcu free inodes are flushed
464 * before we destroy the cache.
467 kmem_cache_destroy(vboxsf_inode_cachep);
469 mutex_unlock(&vboxsf_setup_mutex);
472 module_init(vboxsf_init);
473 module_exit(vboxsf_fini);
475 MODULE_DESCRIPTION("Oracle VM VirtualBox Module for Host File System Access");
476 MODULE_AUTHOR("Oracle Corporation");
477 MODULE_LICENSE("GPL v2");
478 MODULE_ALIAS_FS("vboxsf");