Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * VirtualBox Guest Shared Folders support: Virtual File System.
0004  *
0005  * Module initialization/finalization
0006  * File system registration/deregistration
0007  * Superblock reading
0008  * Few utility functions
0009  *
0010  * Copyright (C) 2006-2018 Oracle Corporation
0011  */
0012 
0013 #include <linux/idr.h>
0014 #include <linux/fs_parser.h>
0015 #include <linux/magic.h>
0016 #include <linux/module.h>
0017 #include <linux/nls.h>
0018 #include <linux/statfs.h>
0019 #include <linux/vbox_utils.h>
0020 #include "vfsmod.h"
0021 
0022 #define VBOXSF_SUPER_MAGIC 0x786f4256 /* 'VBox' little endian */
0023 
0024 static const unsigned char VBSF_MOUNT_SIGNATURE[4] = "\000\377\376\375";
0025 
0026 static int follow_symlinks;
0027 module_param(follow_symlinks, int, 0444);
0028 MODULE_PARM_DESC(follow_symlinks,
0029          "Let host resolve symlinks rather than showing them");
0030 
0031 static DEFINE_IDA(vboxsf_bdi_ida);
0032 static DEFINE_MUTEX(vboxsf_setup_mutex);
0033 static bool vboxsf_setup_done;
0034 static struct super_operations vboxsf_super_ops; /* forward declaration */
0035 static struct kmem_cache *vboxsf_inode_cachep;
0036 
0037 static char * const vboxsf_default_nls = CONFIG_NLS_DEFAULT;
0038 
0039 enum  { opt_nls, opt_uid, opt_gid, opt_ttl, opt_dmode, opt_fmode,
0040     opt_dmask, opt_fmask };
0041 
0042 static const struct fs_parameter_spec vboxsf_fs_parameters[] = {
0043     fsparam_string  ("nls",     opt_nls),
0044     fsparam_u32 ("uid",     opt_uid),
0045     fsparam_u32 ("gid",     opt_gid),
0046     fsparam_u32 ("ttl",     opt_ttl),
0047     fsparam_u32oct  ("dmode",   opt_dmode),
0048     fsparam_u32oct  ("fmode",   opt_fmode),
0049     fsparam_u32oct  ("dmask",   opt_dmask),
0050     fsparam_u32oct  ("fmask",   opt_fmask),
0051     {}
0052 };
0053 
0054 static int vboxsf_parse_param(struct fs_context *fc, struct fs_parameter *param)
0055 {
0056     struct vboxsf_fs_context *ctx = fc->fs_private;
0057     struct fs_parse_result result;
0058     kuid_t uid;
0059     kgid_t gid;
0060     int opt;
0061 
0062     opt = fs_parse(fc, vboxsf_fs_parameters, param, &result);
0063     if (opt < 0)
0064         return opt;
0065 
0066     switch (opt) {
0067     case opt_nls:
0068         if (ctx->nls_name || fc->purpose != FS_CONTEXT_FOR_MOUNT) {
0069             vbg_err("vboxsf: Cannot reconfigure nls option\n");
0070             return -EINVAL;
0071         }
0072         ctx->nls_name = param->string;
0073         param->string = NULL;
0074         break;
0075     case opt_uid:
0076         uid = make_kuid(current_user_ns(), result.uint_32);
0077         if (!uid_valid(uid))
0078             return -EINVAL;
0079         ctx->o.uid = uid;
0080         break;
0081     case opt_gid:
0082         gid = make_kgid(current_user_ns(), result.uint_32);
0083         if (!gid_valid(gid))
0084             return -EINVAL;
0085         ctx->o.gid = gid;
0086         break;
0087     case opt_ttl:
0088         ctx->o.ttl = msecs_to_jiffies(result.uint_32);
0089         break;
0090     case opt_dmode:
0091         if (result.uint_32 & ~0777)
0092             return -EINVAL;
0093         ctx->o.dmode = result.uint_32;
0094         ctx->o.dmode_set = true;
0095         break;
0096     case opt_fmode:
0097         if (result.uint_32 & ~0777)
0098             return -EINVAL;
0099         ctx->o.fmode = result.uint_32;
0100         ctx->o.fmode_set = true;
0101         break;
0102     case opt_dmask:
0103         if (result.uint_32 & ~07777)
0104             return -EINVAL;
0105         ctx->o.dmask = result.uint_32;
0106         break;
0107     case opt_fmask:
0108         if (result.uint_32 & ~07777)
0109             return -EINVAL;
0110         ctx->o.fmask = result.uint_32;
0111         break;
0112     default:
0113         return -EINVAL;
0114     }
0115 
0116     return 0;
0117 }
0118 
0119 static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
0120 {
0121     struct vboxsf_fs_context *ctx = fc->fs_private;
0122     struct shfl_string *folder_name, root_path;
0123     struct vboxsf_sbi *sbi;
0124     struct dentry *droot;
0125     struct inode *iroot;
0126     char *nls_name;
0127     size_t size;
0128     int err;
0129 
0130     if (!fc->source)
0131         return -EINVAL;
0132 
0133     sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
0134     if (!sbi)
0135         return -ENOMEM;
0136 
0137     sbi->o = ctx->o;
0138     idr_init(&sbi->ino_idr);
0139     spin_lock_init(&sbi->ino_idr_lock);
0140     sbi->next_generation = 1;
0141     sbi->bdi_id = -1;
0142 
0143     /* Load nls if not utf8 */
0144     nls_name = ctx->nls_name ? ctx->nls_name : vboxsf_default_nls;
0145     if (strcmp(nls_name, "utf8") != 0) {
0146         if (nls_name == vboxsf_default_nls)
0147             sbi->nls = load_nls_default();
0148         else
0149             sbi->nls = load_nls(nls_name);
0150 
0151         if (!sbi->nls) {
0152             vbg_err("vboxsf: Count not load '%s' nls\n", nls_name);
0153             err = -EINVAL;
0154             goto fail_free;
0155         }
0156     }
0157 
0158     sbi->bdi_id = ida_simple_get(&vboxsf_bdi_ida, 0, 0, GFP_KERNEL);
0159     if (sbi->bdi_id < 0) {
0160         err = sbi->bdi_id;
0161         goto fail_free;
0162     }
0163 
0164     err = super_setup_bdi_name(sb, "vboxsf-%d", sbi->bdi_id);
0165     if (err)
0166         goto fail_free;
0167     sb->s_bdi->ra_pages = 0;
0168     sb->s_bdi->io_pages = 0;
0169 
0170     /* Turn source into a shfl_string and map the folder */
0171     size = strlen(fc->source) + 1;
0172     folder_name = kmalloc(SHFLSTRING_HEADER_SIZE + size, GFP_KERNEL);
0173     if (!folder_name) {
0174         err = -ENOMEM;
0175         goto fail_free;
0176     }
0177     folder_name->size = size;
0178     folder_name->length = size - 1;
0179     strlcpy(folder_name->string.utf8, fc->source, size);
0180     err = vboxsf_map_folder(folder_name, &sbi->root);
0181     kfree(folder_name);
0182     if (err) {
0183         vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
0184             fc->source, err);
0185         goto fail_free;
0186     }
0187 
0188     root_path.length = 1;
0189     root_path.size = 2;
0190     root_path.string.utf8[0] = '/';
0191     root_path.string.utf8[1] = 0;
0192     err = vboxsf_stat(sbi, &root_path, &sbi->root_info);
0193     if (err)
0194         goto fail_unmap;
0195 
0196     sb->s_magic = VBOXSF_SUPER_MAGIC;
0197     sb->s_blocksize = 1024;
0198     sb->s_maxbytes = MAX_LFS_FILESIZE;
0199     sb->s_op = &vboxsf_super_ops;
0200     sb->s_d_op = &vboxsf_dentry_ops;
0201 
0202     iroot = iget_locked(sb, 0);
0203     if (!iroot) {
0204         err = -ENOMEM;
0205         goto fail_unmap;
0206     }
0207     vboxsf_init_inode(sbi, iroot, &sbi->root_info, false);
0208     unlock_new_inode(iroot);
0209 
0210     droot = d_make_root(iroot);
0211     if (!droot) {
0212         err = -ENOMEM;
0213         goto fail_unmap;
0214     }
0215 
0216     sb->s_root = droot;
0217     sb->s_fs_info = sbi;
0218     return 0;
0219 
0220 fail_unmap:
0221     vboxsf_unmap_folder(sbi->root);
0222 fail_free:
0223     if (sbi->bdi_id >= 0)
0224         ida_simple_remove(&vboxsf_bdi_ida, sbi->bdi_id);
0225     if (sbi->nls)
0226         unload_nls(sbi->nls);
0227     idr_destroy(&sbi->ino_idr);
0228     kfree(sbi);
0229     return err;
0230 }
0231 
0232 static void vboxsf_inode_init_once(void *data)
0233 {
0234     struct vboxsf_inode *sf_i = data;
0235 
0236     mutex_init(&sf_i->handle_list_mutex);
0237     inode_init_once(&sf_i->vfs_inode);
0238 }
0239 
0240 static struct inode *vboxsf_alloc_inode(struct super_block *sb)
0241 {
0242     struct vboxsf_inode *sf_i;
0243 
0244     sf_i = alloc_inode_sb(sb, vboxsf_inode_cachep, GFP_NOFS);
0245     if (!sf_i)
0246         return NULL;
0247 
0248     sf_i->force_restat = 0;
0249     INIT_LIST_HEAD(&sf_i->handle_list);
0250 
0251     return &sf_i->vfs_inode;
0252 }
0253 
0254 static void vboxsf_free_inode(struct inode *inode)
0255 {
0256     struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
0257     unsigned long flags;
0258 
0259     spin_lock_irqsave(&sbi->ino_idr_lock, flags);
0260     idr_remove(&sbi->ino_idr, inode->i_ino);
0261     spin_unlock_irqrestore(&sbi->ino_idr_lock, flags);
0262     kmem_cache_free(vboxsf_inode_cachep, VBOXSF_I(inode));
0263 }
0264 
0265 static void vboxsf_put_super(struct super_block *sb)
0266 {
0267     struct vboxsf_sbi *sbi = VBOXSF_SBI(sb);
0268 
0269     vboxsf_unmap_folder(sbi->root);
0270     if (sbi->bdi_id >= 0)
0271         ida_simple_remove(&vboxsf_bdi_ida, sbi->bdi_id);
0272     if (sbi->nls)
0273         unload_nls(sbi->nls);
0274 
0275     /*
0276      * vboxsf_free_inode uses the idr, make sure all delayed rcu free
0277      * inodes are flushed.
0278      */
0279     rcu_barrier();
0280     idr_destroy(&sbi->ino_idr);
0281     kfree(sbi);
0282 }
0283 
0284 static int vboxsf_statfs(struct dentry *dentry, struct kstatfs *stat)
0285 {
0286     struct super_block *sb = dentry->d_sb;
0287     struct shfl_volinfo shfl_volinfo;
0288     struct vboxsf_sbi *sbi;
0289     u32 buf_len;
0290     int err;
0291 
0292     sbi = VBOXSF_SBI(sb);
0293     buf_len = sizeof(shfl_volinfo);
0294     err = vboxsf_fsinfo(sbi->root, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
0295                 &buf_len, &shfl_volinfo);
0296     if (err)
0297         return err;
0298 
0299     stat->f_type = VBOXSF_SUPER_MAGIC;
0300     stat->f_bsize = shfl_volinfo.bytes_per_allocation_unit;
0301 
0302     do_div(shfl_volinfo.total_allocation_bytes,
0303            shfl_volinfo.bytes_per_allocation_unit);
0304     stat->f_blocks = shfl_volinfo.total_allocation_bytes;
0305 
0306     do_div(shfl_volinfo.available_allocation_bytes,
0307            shfl_volinfo.bytes_per_allocation_unit);
0308     stat->f_bfree  = shfl_volinfo.available_allocation_bytes;
0309     stat->f_bavail = shfl_volinfo.available_allocation_bytes;
0310 
0311     stat->f_files = 1000;
0312     /*
0313      * Don't return 0 here since the guest may then think that it is not
0314      * possible to create any more files.
0315      */
0316     stat->f_ffree = 1000000;
0317     stat->f_fsid.val[0] = 0;
0318     stat->f_fsid.val[1] = 0;
0319     stat->f_namelen = 255;
0320     return 0;
0321 }
0322 
0323 static struct super_operations vboxsf_super_ops = {
0324     .alloc_inode    = vboxsf_alloc_inode,
0325     .free_inode = vboxsf_free_inode,
0326     .put_super  = vboxsf_put_super,
0327     .statfs     = vboxsf_statfs,
0328 };
0329 
0330 static int vboxsf_setup(void)
0331 {
0332     int err;
0333 
0334     mutex_lock(&vboxsf_setup_mutex);
0335 
0336     if (vboxsf_setup_done)
0337         goto success;
0338 
0339     vboxsf_inode_cachep =
0340         kmem_cache_create("vboxsf_inode_cache",
0341                   sizeof(struct vboxsf_inode), 0,
0342                   (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
0343                    SLAB_ACCOUNT),
0344                   vboxsf_inode_init_once);
0345     if (!vboxsf_inode_cachep) {
0346         err = -ENOMEM;
0347         goto fail_nomem;
0348     }
0349 
0350     err = vboxsf_connect();
0351     if (err) {
0352         vbg_err("vboxsf: err %d connecting to guest PCI-device\n", err);
0353         vbg_err("vboxsf: make sure you are inside a VirtualBox VM\n");
0354         vbg_err("vboxsf: and check dmesg for vboxguest errors\n");
0355         goto fail_free_cache;
0356     }
0357 
0358     err = vboxsf_set_utf8();
0359     if (err) {
0360         vbg_err("vboxsf_setutf8 error %d\n", err);
0361         goto fail_disconnect;
0362     }
0363 
0364     if (!follow_symlinks) {
0365         err = vboxsf_set_symlinks();
0366         if (err)
0367             vbg_warn("vboxsf: Unable to show symlinks: %d\n", err);
0368     }
0369 
0370     vboxsf_setup_done = true;
0371 success:
0372     mutex_unlock(&vboxsf_setup_mutex);
0373     return 0;
0374 
0375 fail_disconnect:
0376     vboxsf_disconnect();
0377 fail_free_cache:
0378     kmem_cache_destroy(vboxsf_inode_cachep);
0379 fail_nomem:
0380     mutex_unlock(&vboxsf_setup_mutex);
0381     return err;
0382 }
0383 
0384 static int vboxsf_parse_monolithic(struct fs_context *fc, void *data)
0385 {
0386     if (data && !memcmp(data, VBSF_MOUNT_SIGNATURE, 4)) {
0387         vbg_err("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n");
0388         return -EINVAL;
0389     }
0390 
0391     return generic_parse_monolithic(fc, data);
0392 }
0393 
0394 static int vboxsf_get_tree(struct fs_context *fc)
0395 {
0396     int err;
0397 
0398     err = vboxsf_setup();
0399     if (err)
0400         return err;
0401 
0402     return get_tree_nodev(fc, vboxsf_fill_super);
0403 }
0404 
0405 static int vboxsf_reconfigure(struct fs_context *fc)
0406 {
0407     struct vboxsf_sbi *sbi = VBOXSF_SBI(fc->root->d_sb);
0408     struct vboxsf_fs_context *ctx = fc->fs_private;
0409     struct inode *iroot = fc->root->d_sb->s_root->d_inode;
0410 
0411     /* Apply changed options to the root inode */
0412     sbi->o = ctx->o;
0413     vboxsf_init_inode(sbi, iroot, &sbi->root_info, true);
0414 
0415     return 0;
0416 }
0417 
0418 static void vboxsf_free_fc(struct fs_context *fc)
0419 {
0420     struct vboxsf_fs_context *ctx = fc->fs_private;
0421 
0422     kfree(ctx->nls_name);
0423     kfree(ctx);
0424 }
0425 
0426 static const struct fs_context_operations vboxsf_context_ops = {
0427     .free           = vboxsf_free_fc,
0428     .parse_param        = vboxsf_parse_param,
0429     .parse_monolithic   = vboxsf_parse_monolithic,
0430     .get_tree       = vboxsf_get_tree,
0431     .reconfigure        = vboxsf_reconfigure,
0432 };
0433 
0434 static int vboxsf_init_fs_context(struct fs_context *fc)
0435 {
0436     struct vboxsf_fs_context *ctx;
0437 
0438     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0439     if (!ctx)
0440         return -ENOMEM;
0441 
0442     current_uid_gid(&ctx->o.uid, &ctx->o.gid);
0443 
0444     fc->fs_private = ctx;
0445     fc->ops = &vboxsf_context_ops;
0446     return 0;
0447 }
0448 
0449 static struct file_system_type vboxsf_fs_type = {
0450     .owner          = THIS_MODULE,
0451     .name           = "vboxsf",
0452     .init_fs_context    = vboxsf_init_fs_context,
0453     .kill_sb        = kill_anon_super
0454 };
0455 
0456 /* Module initialization/finalization handlers */
0457 static int __init vboxsf_init(void)
0458 {
0459     return register_filesystem(&vboxsf_fs_type);
0460 }
0461 
0462 static void __exit vboxsf_fini(void)
0463 {
0464     unregister_filesystem(&vboxsf_fs_type);
0465 
0466     mutex_lock(&vboxsf_setup_mutex);
0467     if (vboxsf_setup_done) {
0468         vboxsf_disconnect();
0469         /*
0470          * Make sure all delayed rcu free inodes are flushed
0471          * before we destroy the cache.
0472          */
0473         rcu_barrier();
0474         kmem_cache_destroy(vboxsf_inode_cachep);
0475     }
0476     mutex_unlock(&vboxsf_setup_mutex);
0477 }
0478 
0479 module_init(vboxsf_init);
0480 module_exit(vboxsf_fini);
0481 
0482 MODULE_DESCRIPTION("Oracle VM VirtualBox Module for Host File System Access");
0483 MODULE_AUTHOR("Oracle Corporation");
0484 MODULE_LICENSE("GPL v2");
0485 MODULE_ALIAS_FS("vboxsf");