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\375";
26 static int follow_symlinks
;
27 module_param(follow_symlinks
, int, 0444);
28 MODULE_PARM_DESC(follow_symlinks
,
29 "Let host resolve symlinks rather than showing them");
31 static DEFINE_IDA(vboxsf_bdi_ida
);
32 static DEFINE_MUTEX(vboxsf_setup_mutex
);
33 static bool vboxsf_setup_done
;
34 static struct super_operations vboxsf_super_ops
; /* forward declaration */
35 static struct kmem_cache
*vboxsf_inode_cachep
;
37 static char * const vboxsf_default_nls
= CONFIG_NLS_DEFAULT
;
39 enum { opt_nls
, opt_uid
, opt_gid
, opt_ttl
, opt_dmode
, opt_fmode
,
40 opt_dmask
, opt_fmask
};
42 static const struct fs_parameter_spec vboxsf_fs_parameters
[] = {
43 fsparam_string ("nls", opt_nls
),
44 fsparam_uid ("uid", opt_uid
),
45 fsparam_gid ("gid", opt_gid
),
46 fsparam_u32 ("ttl", opt_ttl
),
47 fsparam_u32oct ("dmode", opt_dmode
),
48 fsparam_u32oct ("fmode", opt_fmode
),
49 fsparam_u32oct ("dmask", opt_dmask
),
50 fsparam_u32oct ("fmask", opt_fmask
),
54 static int vboxsf_parse_param(struct fs_context
*fc
, struct fs_parameter
*param
)
56 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
57 struct fs_parse_result result
;
60 opt
= fs_parse(fc
, vboxsf_fs_parameters
, param
, &result
);
66 if (ctx
->nls_name
|| fc
->purpose
!= FS_CONTEXT_FOR_MOUNT
) {
67 vbg_err("vboxsf: Cannot reconfigure nls option\n");
70 ctx
->nls_name
= param
->string
;
74 ctx
->o
.uid
= result
.uid
;
77 ctx
->o
.gid
= result
.gid
;
80 ctx
->o
.ttl
= msecs_to_jiffies(result
.uint_32
);
83 if (result
.uint_32
& ~0777)
85 ctx
->o
.dmode
= result
.uint_32
;
86 ctx
->o
.dmode_set
= true;
89 if (result
.uint_32
& ~0777)
91 ctx
->o
.fmode
= result
.uint_32
;
92 ctx
->o
.fmode_set
= true;
95 if (result
.uint_32
& ~07777)
97 ctx
->o
.dmask
= result
.uint_32
;
100 if (result
.uint_32
& ~07777)
102 ctx
->o
.fmask
= result
.uint_32
;
111 static int vboxsf_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
113 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
114 struct shfl_string
*folder_name
, root_path
;
115 struct vboxsf_sbi
*sbi
;
116 struct dentry
*droot
;
125 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
130 idr_init(&sbi
->ino_idr
);
131 spin_lock_init(&sbi
->ino_idr_lock
);
132 sbi
->next_generation
= 1;
135 /* Load nls if not utf8 */
136 nls_name
= ctx
->nls_name
? ctx
->nls_name
: vboxsf_default_nls
;
137 if (strcmp(nls_name
, "utf8") != 0) {
138 if (nls_name
== vboxsf_default_nls
)
139 sbi
->nls
= load_nls_default();
141 sbi
->nls
= load_nls(nls_name
);
144 vbg_err("vboxsf: Count not load '%s' nls\n", nls_name
);
146 goto fail_destroy_idr
;
150 sbi
->bdi_id
= ida_alloc(&vboxsf_bdi_ida
, GFP_KERNEL
);
151 if (sbi
->bdi_id
< 0) {
156 err
= super_setup_bdi_name(sb
, "vboxsf-%d", sbi
->bdi_id
);
159 sb
->s_bdi
->ra_pages
= 0;
160 sb
->s_bdi
->io_pages
= 0;
162 /* Turn source into a shfl_string and map the folder */
163 size
= strlen(fc
->source
) + 1;
164 folder_name
= kmalloc(SHFLSTRING_HEADER_SIZE
+ size
, GFP_KERNEL
);
169 folder_name
->size
= size
;
170 folder_name
->length
= size
- 1;
171 strscpy(folder_name
->string
.utf8
, fc
->source
, size
);
172 err
= vboxsf_map_folder(folder_name
, &sbi
->root
);
175 vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
180 root_path
.length
= 1;
182 root_path
.string
.utf8
[0] = '/';
183 root_path
.string
.utf8
[1] = 0;
184 err
= vboxsf_stat(sbi
, &root_path
, &sbi
->root_info
);
188 sb
->s_magic
= VBOXSF_SUPER_MAGIC
;
189 sb
->s_blocksize
= 1024;
190 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
191 sb
->s_op
= &vboxsf_super_ops
;
192 sb
->s_d_op
= &vboxsf_dentry_ops
;
194 iroot
= iget_locked(sb
, 0);
199 vboxsf_init_inode(sbi
, iroot
, &sbi
->root_info
, false);
200 unlock_new_inode(iroot
);
202 droot
= d_make_root(iroot
);
213 vboxsf_unmap_folder(sbi
->root
);
215 if (sbi
->bdi_id
>= 0)
216 ida_free(&vboxsf_bdi_ida
, sbi
->bdi_id
);
218 unload_nls(sbi
->nls
);
220 idr_destroy(&sbi
->ino_idr
);
225 static void vboxsf_inode_init_once(void *data
)
227 struct vboxsf_inode
*sf_i
= data
;
229 mutex_init(&sf_i
->handle_list_mutex
);
230 inode_init_once(&sf_i
->vfs_inode
);
233 static struct inode
*vboxsf_alloc_inode(struct super_block
*sb
)
235 struct vboxsf_inode
*sf_i
;
237 sf_i
= alloc_inode_sb(sb
, vboxsf_inode_cachep
, GFP_NOFS
);
241 sf_i
->force_restat
= 0;
242 INIT_LIST_HEAD(&sf_i
->handle_list
);
244 return &sf_i
->vfs_inode
;
247 static void vboxsf_free_inode(struct inode
*inode
)
249 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(inode
->i_sb
);
252 spin_lock_irqsave(&sbi
->ino_idr_lock
, flags
);
253 idr_remove(&sbi
->ino_idr
, inode
->i_ino
);
254 spin_unlock_irqrestore(&sbi
->ino_idr_lock
, flags
);
255 kmem_cache_free(vboxsf_inode_cachep
, VBOXSF_I(inode
));
258 static void vboxsf_put_super(struct super_block
*sb
)
260 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(sb
);
262 vboxsf_unmap_folder(sbi
->root
);
263 if (sbi
->bdi_id
>= 0)
264 ida_free(&vboxsf_bdi_ida
, sbi
->bdi_id
);
266 unload_nls(sbi
->nls
);
269 * vboxsf_free_inode uses the idr, make sure all delayed rcu free
270 * inodes are flushed.
273 idr_destroy(&sbi
->ino_idr
);
277 static int vboxsf_statfs(struct dentry
*dentry
, struct kstatfs
*stat
)
279 struct super_block
*sb
= dentry
->d_sb
;
280 struct shfl_volinfo shfl_volinfo
;
281 struct vboxsf_sbi
*sbi
;
285 sbi
= VBOXSF_SBI(sb
);
286 buf_len
= sizeof(shfl_volinfo
);
287 err
= vboxsf_fsinfo(sbi
->root
, 0, SHFL_INFO_GET
| SHFL_INFO_VOLUME
,
288 &buf_len
, &shfl_volinfo
);
292 stat
->f_type
= VBOXSF_SUPER_MAGIC
;
293 stat
->f_bsize
= shfl_volinfo
.bytes_per_allocation_unit
;
295 do_div(shfl_volinfo
.total_allocation_bytes
,
296 shfl_volinfo
.bytes_per_allocation_unit
);
297 stat
->f_blocks
= shfl_volinfo
.total_allocation_bytes
;
299 do_div(shfl_volinfo
.available_allocation_bytes
,
300 shfl_volinfo
.bytes_per_allocation_unit
);
301 stat
->f_bfree
= shfl_volinfo
.available_allocation_bytes
;
302 stat
->f_bavail
= shfl_volinfo
.available_allocation_bytes
;
304 stat
->f_files
= 1000;
306 * Don't return 0 here since the guest may then think that it is not
307 * possible to create any more files.
309 stat
->f_ffree
= 1000000;
310 stat
->f_fsid
.val
[0] = 0;
311 stat
->f_fsid
.val
[1] = 0;
312 stat
->f_namelen
= 255;
316 static struct super_operations vboxsf_super_ops
= {
317 .alloc_inode
= vboxsf_alloc_inode
,
318 .free_inode
= vboxsf_free_inode
,
319 .put_super
= vboxsf_put_super
,
320 .statfs
= vboxsf_statfs
,
323 static int vboxsf_setup(void)
327 mutex_lock(&vboxsf_setup_mutex
);
329 if (vboxsf_setup_done
)
332 vboxsf_inode_cachep
=
333 kmem_cache_create("vboxsf_inode_cache",
334 sizeof(struct vboxsf_inode
), 0,
335 SLAB_RECLAIM_ACCOUNT
| SLAB_ACCOUNT
,
336 vboxsf_inode_init_once
);
337 if (!vboxsf_inode_cachep
) {
342 err
= vboxsf_connect();
344 vbg_err("vboxsf: err %d connecting to guest PCI-device\n", err
);
345 vbg_err("vboxsf: make sure you are inside a VirtualBox VM\n");
346 vbg_err("vboxsf: and check dmesg for vboxguest errors\n");
347 goto fail_free_cache
;
350 err
= vboxsf_set_utf8();
352 vbg_err("vboxsf_setutf8 error %d\n", err
);
353 goto fail_disconnect
;
356 if (!follow_symlinks
) {
357 err
= vboxsf_set_symlinks();
359 vbg_warn("vboxsf: Unable to show symlinks: %d\n", err
);
362 vboxsf_setup_done
= true;
364 mutex_unlock(&vboxsf_setup_mutex
);
370 kmem_cache_destroy(vboxsf_inode_cachep
);
372 mutex_unlock(&vboxsf_setup_mutex
);
376 static int vboxsf_parse_monolithic(struct fs_context
*fc
, void *data
)
378 if (data
&& !memcmp(data
, VBSF_MOUNT_SIGNATURE
, 4)) {
379 vbg_err("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n");
383 return generic_parse_monolithic(fc
, data
);
386 static int vboxsf_get_tree(struct fs_context
*fc
)
390 err
= vboxsf_setup();
394 return get_tree_nodev(fc
, vboxsf_fill_super
);
397 static int vboxsf_reconfigure(struct fs_context
*fc
)
399 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(fc
->root
->d_sb
);
400 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
401 struct inode
*iroot
= fc
->root
->d_sb
->s_root
->d_inode
;
403 /* Apply changed options to the root inode */
405 vboxsf_init_inode(sbi
, iroot
, &sbi
->root_info
, true);
410 static void vboxsf_free_fc(struct fs_context
*fc
)
412 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
414 kfree(ctx
->nls_name
);
418 static const struct fs_context_operations vboxsf_context_ops
= {
419 .free
= vboxsf_free_fc
,
420 .parse_param
= vboxsf_parse_param
,
421 .parse_monolithic
= vboxsf_parse_monolithic
,
422 .get_tree
= vboxsf_get_tree
,
423 .reconfigure
= vboxsf_reconfigure
,
426 static int vboxsf_init_fs_context(struct fs_context
*fc
)
428 struct vboxsf_fs_context
*ctx
;
430 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
434 current_uid_gid(&ctx
->o
.uid
, &ctx
->o
.gid
);
436 fc
->fs_private
= ctx
;
437 fc
->ops
= &vboxsf_context_ops
;
441 static struct file_system_type vboxsf_fs_type
= {
442 .owner
= THIS_MODULE
,
444 .init_fs_context
= vboxsf_init_fs_context
,
445 .kill_sb
= kill_anon_super
448 /* Module initialization/finalization handlers */
449 static int __init
vboxsf_init(void)
451 return register_filesystem(&vboxsf_fs_type
);
454 static void __exit
vboxsf_fini(void)
456 unregister_filesystem(&vboxsf_fs_type
);
458 mutex_lock(&vboxsf_setup_mutex
);
459 if (vboxsf_setup_done
) {
462 * Make sure all delayed rcu free inodes are flushed
463 * before we destroy the cache.
466 kmem_cache_destroy(vboxsf_inode_cachep
);
468 mutex_unlock(&vboxsf_setup_mutex
);
471 module_init(vboxsf_init
);
472 module_exit(vboxsf_fini
);
474 MODULE_DESCRIPTION("Oracle VM VirtualBox Module for Host File System Access");
475 MODULE_AUTHOR("Oracle Corporation");
476 MODULE_LICENSE("GPL v2");
477 MODULE_ALIAS_FS("vboxsf");