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 #define VBSF_MOUNT_SIGNATURE_BYTE_0 ('\000')
25 #define VBSF_MOUNT_SIGNATURE_BYTE_1 ('\377')
26 #define VBSF_MOUNT_SIGNATURE_BYTE_2 ('\376')
27 #define VBSF_MOUNT_SIGNATURE_BYTE_3 ('\375')
29 static int follow_symlinks
;
30 module_param(follow_symlinks
, int, 0444);
31 MODULE_PARM_DESC(follow_symlinks
,
32 "Let host resolve symlinks rather than showing them");
34 static DEFINE_IDA(vboxsf_bdi_ida
);
35 static DEFINE_MUTEX(vboxsf_setup_mutex
);
36 static bool vboxsf_setup_done
;
37 static struct super_operations vboxsf_super_ops
; /* forward declaration */
38 static struct kmem_cache
*vboxsf_inode_cachep
;
40 static char * const vboxsf_default_nls
= CONFIG_NLS_DEFAULT
;
42 enum { opt_nls
, opt_uid
, opt_gid
, opt_ttl
, opt_dmode
, opt_fmode
,
43 opt_dmask
, opt_fmask
};
45 static const struct fs_parameter_spec vboxsf_fs_parameters
[] = {
46 fsparam_string ("nls", opt_nls
),
47 fsparam_u32 ("uid", opt_uid
),
48 fsparam_u32 ("gid", opt_gid
),
49 fsparam_u32 ("ttl", opt_ttl
),
50 fsparam_u32oct ("dmode", opt_dmode
),
51 fsparam_u32oct ("fmode", opt_fmode
),
52 fsparam_u32oct ("dmask", opt_dmask
),
53 fsparam_u32oct ("fmask", opt_fmask
),
57 static int vboxsf_parse_param(struct fs_context
*fc
, struct fs_parameter
*param
)
59 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
60 struct fs_parse_result result
;
65 opt
= fs_parse(fc
, vboxsf_fs_parameters
, param
, &result
);
71 if (ctx
->nls_name
|| fc
->purpose
!= FS_CONTEXT_FOR_MOUNT
) {
72 vbg_err("vboxsf: Cannot reconfigure nls option\n");
75 ctx
->nls_name
= param
->string
;
79 uid
= make_kuid(current_user_ns(), result
.uint_32
);
85 gid
= make_kgid(current_user_ns(), result
.uint_32
);
91 ctx
->o
.ttl
= msecs_to_jiffies(result
.uint_32
);
94 if (result
.uint_32
& ~0777)
96 ctx
->o
.dmode
= result
.uint_32
;
97 ctx
->o
.dmode_set
= true;
100 if (result
.uint_32
& ~0777)
102 ctx
->o
.fmode
= result
.uint_32
;
103 ctx
->o
.fmode_set
= true;
106 if (result
.uint_32
& ~07777)
108 ctx
->o
.dmask
= result
.uint_32
;
111 if (result
.uint_32
& ~07777)
113 ctx
->o
.fmask
= result
.uint_32
;
122 static int vboxsf_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
124 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
125 struct shfl_string
*folder_name
, root_path
;
126 struct vboxsf_sbi
*sbi
;
127 struct dentry
*droot
;
136 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
141 idr_init(&sbi
->ino_idr
);
142 spin_lock_init(&sbi
->ino_idr_lock
);
143 sbi
->next_generation
= 1;
146 /* Load nls if not utf8 */
147 nls_name
= ctx
->nls_name
? ctx
->nls_name
: vboxsf_default_nls
;
148 if (strcmp(nls_name
, "utf8") != 0) {
149 if (nls_name
== vboxsf_default_nls
)
150 sbi
->nls
= load_nls_default();
152 sbi
->nls
= load_nls(nls_name
);
155 vbg_err("vboxsf: Count not load '%s' nls\n", nls_name
);
161 sbi
->bdi_id
= ida_simple_get(&vboxsf_bdi_ida
, 0, 0, GFP_KERNEL
);
162 if (sbi
->bdi_id
< 0) {
167 err
= super_setup_bdi_name(sb
, "vboxsf-%d", sbi
->bdi_id
);
170 sb
->s_bdi
->ra_pages
= 0;
171 sb
->s_bdi
->io_pages
= 0;
173 /* Turn source into a shfl_string and map the folder */
174 size
= strlen(fc
->source
) + 1;
175 folder_name
= kmalloc(SHFLSTRING_HEADER_SIZE
+ size
, GFP_KERNEL
);
180 folder_name
->size
= size
;
181 folder_name
->length
= size
- 1;
182 strlcpy(folder_name
->string
.utf8
, fc
->source
, size
);
183 err
= vboxsf_map_folder(folder_name
, &sbi
->root
);
186 vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
191 root_path
.length
= 1;
193 root_path
.string
.utf8
[0] = '/';
194 root_path
.string
.utf8
[1] = 0;
195 err
= vboxsf_stat(sbi
, &root_path
, &sbi
->root_info
);
199 sb
->s_magic
= VBOXSF_SUPER_MAGIC
;
200 sb
->s_blocksize
= 1024;
201 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
202 sb
->s_op
= &vboxsf_super_ops
;
203 sb
->s_d_op
= &vboxsf_dentry_ops
;
205 iroot
= iget_locked(sb
, 0);
210 vboxsf_init_inode(sbi
, iroot
, &sbi
->root_info
);
211 unlock_new_inode(iroot
);
213 droot
= d_make_root(iroot
);
224 vboxsf_unmap_folder(sbi
->root
);
226 if (sbi
->bdi_id
>= 0)
227 ida_simple_remove(&vboxsf_bdi_ida
, sbi
->bdi_id
);
229 unload_nls(sbi
->nls
);
230 idr_destroy(&sbi
->ino_idr
);
235 static void vboxsf_inode_init_once(void *data
)
237 struct vboxsf_inode
*sf_i
= data
;
239 mutex_init(&sf_i
->handle_list_mutex
);
240 inode_init_once(&sf_i
->vfs_inode
);
243 static struct inode
*vboxsf_alloc_inode(struct super_block
*sb
)
245 struct vboxsf_inode
*sf_i
;
247 sf_i
= kmem_cache_alloc(vboxsf_inode_cachep
, GFP_NOFS
);
251 sf_i
->force_restat
= 0;
252 INIT_LIST_HEAD(&sf_i
->handle_list
);
254 return &sf_i
->vfs_inode
;
257 static void vboxsf_free_inode(struct inode
*inode
)
259 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(inode
->i_sb
);
262 spin_lock_irqsave(&sbi
->ino_idr_lock
, flags
);
263 idr_remove(&sbi
->ino_idr
, inode
->i_ino
);
264 spin_unlock_irqrestore(&sbi
->ino_idr_lock
, flags
);
265 kmem_cache_free(vboxsf_inode_cachep
, VBOXSF_I(inode
));
268 static void vboxsf_put_super(struct super_block
*sb
)
270 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(sb
);
272 vboxsf_unmap_folder(sbi
->root
);
273 if (sbi
->bdi_id
>= 0)
274 ida_simple_remove(&vboxsf_bdi_ida
, sbi
->bdi_id
);
276 unload_nls(sbi
->nls
);
279 * vboxsf_free_inode uses the idr, make sure all delayed rcu free
280 * inodes are flushed.
283 idr_destroy(&sbi
->ino_idr
);
287 static int vboxsf_statfs(struct dentry
*dentry
, struct kstatfs
*stat
)
289 struct super_block
*sb
= dentry
->d_sb
;
290 struct shfl_volinfo shfl_volinfo
;
291 struct vboxsf_sbi
*sbi
;
295 sbi
= VBOXSF_SBI(sb
);
296 buf_len
= sizeof(shfl_volinfo
);
297 err
= vboxsf_fsinfo(sbi
->root
, 0, SHFL_INFO_GET
| SHFL_INFO_VOLUME
,
298 &buf_len
, &shfl_volinfo
);
302 stat
->f_type
= VBOXSF_SUPER_MAGIC
;
303 stat
->f_bsize
= shfl_volinfo
.bytes_per_allocation_unit
;
305 do_div(shfl_volinfo
.total_allocation_bytes
,
306 shfl_volinfo
.bytes_per_allocation_unit
);
307 stat
->f_blocks
= shfl_volinfo
.total_allocation_bytes
;
309 do_div(shfl_volinfo
.available_allocation_bytes
,
310 shfl_volinfo
.bytes_per_allocation_unit
);
311 stat
->f_bfree
= shfl_volinfo
.available_allocation_bytes
;
312 stat
->f_bavail
= shfl_volinfo
.available_allocation_bytes
;
314 stat
->f_files
= 1000;
316 * Don't return 0 here since the guest may then think that it is not
317 * possible to create any more files.
319 stat
->f_ffree
= 1000000;
320 stat
->f_fsid
.val
[0] = 0;
321 stat
->f_fsid
.val
[1] = 0;
322 stat
->f_namelen
= 255;
326 static struct super_operations vboxsf_super_ops
= {
327 .alloc_inode
= vboxsf_alloc_inode
,
328 .free_inode
= vboxsf_free_inode
,
329 .put_super
= vboxsf_put_super
,
330 .statfs
= vboxsf_statfs
,
333 static int vboxsf_setup(void)
337 mutex_lock(&vboxsf_setup_mutex
);
339 if (vboxsf_setup_done
)
342 vboxsf_inode_cachep
=
343 kmem_cache_create("vboxsf_inode_cache",
344 sizeof(struct vboxsf_inode
), 0,
345 (SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
|
347 vboxsf_inode_init_once
);
348 if (!vboxsf_inode_cachep
) {
353 err
= vboxsf_connect();
355 vbg_err("vboxsf: err %d connecting to guest PCI-device\n", err
);
356 vbg_err("vboxsf: make sure you are inside a VirtualBox VM\n");
357 vbg_err("vboxsf: and check dmesg for vboxguest errors\n");
358 goto fail_free_cache
;
361 err
= vboxsf_set_utf8();
363 vbg_err("vboxsf_setutf8 error %d\n", err
);
364 goto fail_disconnect
;
367 if (!follow_symlinks
) {
368 err
= vboxsf_set_symlinks();
370 vbg_warn("vboxsf: Unable to show symlinks: %d\n", err
);
373 vboxsf_setup_done
= true;
375 mutex_unlock(&vboxsf_setup_mutex
);
381 kmem_cache_destroy(vboxsf_inode_cachep
);
383 mutex_unlock(&vboxsf_setup_mutex
);
387 static int vboxsf_parse_monolithic(struct fs_context
*fc
, void *data
)
389 unsigned char *options
= data
;
391 if (options
&& options
[0] == VBSF_MOUNT_SIGNATURE_BYTE_0
&&
392 options
[1] == VBSF_MOUNT_SIGNATURE_BYTE_1
&&
393 options
[2] == VBSF_MOUNT_SIGNATURE_BYTE_2
&&
394 options
[3] == VBSF_MOUNT_SIGNATURE_BYTE_3
) {
395 vbg_err("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n");
399 return generic_parse_monolithic(fc
, data
);
402 static int vboxsf_get_tree(struct fs_context
*fc
)
406 err
= vboxsf_setup();
410 return get_tree_nodev(fc
, vboxsf_fill_super
);
413 static int vboxsf_reconfigure(struct fs_context
*fc
)
415 struct vboxsf_sbi
*sbi
= VBOXSF_SBI(fc
->root
->d_sb
);
416 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
417 struct inode
*iroot
= fc
->root
->d_sb
->s_root
->d_inode
;
419 /* Apply changed options to the root inode */
421 vboxsf_init_inode(sbi
, iroot
, &sbi
->root_info
);
426 static void vboxsf_free_fc(struct fs_context
*fc
)
428 struct vboxsf_fs_context
*ctx
= fc
->fs_private
;
430 kfree(ctx
->nls_name
);
434 static const struct fs_context_operations vboxsf_context_ops
= {
435 .free
= vboxsf_free_fc
,
436 .parse_param
= vboxsf_parse_param
,
437 .parse_monolithic
= vboxsf_parse_monolithic
,
438 .get_tree
= vboxsf_get_tree
,
439 .reconfigure
= vboxsf_reconfigure
,
442 static int vboxsf_init_fs_context(struct fs_context
*fc
)
444 struct vboxsf_fs_context
*ctx
;
446 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
450 current_uid_gid(&ctx
->o
.uid
, &ctx
->o
.gid
);
452 fc
->fs_private
= ctx
;
453 fc
->ops
= &vboxsf_context_ops
;
457 static struct file_system_type vboxsf_fs_type
= {
458 .owner
= THIS_MODULE
,
460 .init_fs_context
= vboxsf_init_fs_context
,
461 .kill_sb
= kill_anon_super
464 /* Module initialization/finalization handlers */
465 static int __init
vboxsf_init(void)
467 return register_filesystem(&vboxsf_fs_type
);
470 static void __exit
vboxsf_fini(void)
472 unregister_filesystem(&vboxsf_fs_type
);
474 mutex_lock(&vboxsf_setup_mutex
);
475 if (vboxsf_setup_done
) {
478 * Make sure all delayed rcu free inodes are flushed
479 * before we destroy the cache.
482 kmem_cache_destroy(vboxsf_inode_cachep
);
484 mutex_unlock(&vboxsf_setup_mutex
);
487 module_init(vboxsf_init
);
488 module_exit(vboxsf_fini
);
490 MODULE_DESCRIPTION("Oracle VM VirtualBox Module for Host File System Access");
491 MODULE_AUTHOR("Oracle Corporation");
492 MODULE_LICENSE("GPL v2");
493 MODULE_ALIAS_FS("vboxsf");