xref: /openbmc/linux/fs/vboxsf/super.c (revision 36db6e8484ed455bbb320d89a119378897ae991c)
1 // SPDX-License-Identifier: MIT
2 /*
3  * VirtualBox Guest Shared Folders support: Virtual File System.
4  *
5  * Module initialization/finalization
6  * File system registration/deregistration
7  * Superblock reading
8  * Few utility functions
9  *
10  * Copyright (C) 2006-2018 Oracle Corporation
11  */
12 
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>
20 #include "vfsmod.h"
21 
22 #define VBOXSF_SUPER_MAGIC 0x786f4256 /* 'VBox' little endian */
23 
24 static const unsigned char VBSF_MOUNT_SIGNATURE[4] = { '\000', '\377', '\376',
25 						       '\375' };
26 
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");
31 
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;
37 
38 static char * const vboxsf_default_nls = CONFIG_NLS_DEFAULT;
39 
40 enum  { opt_nls, opt_uid, opt_gid, opt_ttl, opt_dmode, opt_fmode,
41 	opt_dmask, opt_fmask };
42 
43 static const struct fs_parameter_spec vboxsf_fs_parameters[] = {
44 	fsparam_string	("nls",		opt_nls),
45 	fsparam_u32	("uid",		opt_uid),
46 	fsparam_u32	("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),
52 	{}
53 };
54 
vboxsf_parse_param(struct fs_context * fc,struct fs_parameter * param)55 static int vboxsf_parse_param(struct fs_context *fc, struct fs_parameter *param)
56 {
57 	struct vboxsf_fs_context *ctx = fc->fs_private;
58 	struct fs_parse_result result;
59 	kuid_t uid;
60 	kgid_t gid;
61 	int opt;
62 
63 	opt = fs_parse(fc, vboxsf_fs_parameters, param, &result);
64 	if (opt < 0)
65 		return opt;
66 
67 	switch (opt) {
68 	case opt_nls:
69 		if (ctx->nls_name || fc->purpose != FS_CONTEXT_FOR_MOUNT) {
70 			vbg_err("vboxsf: Cannot reconfigure nls option\n");
71 			return -EINVAL;
72 		}
73 		ctx->nls_name = param->string;
74 		param->string = NULL;
75 		break;
76 	case opt_uid:
77 		uid = make_kuid(current_user_ns(), result.uint_32);
78 		if (!uid_valid(uid))
79 			return -EINVAL;
80 		ctx->o.uid = uid;
81 		break;
82 	case opt_gid:
83 		gid = make_kgid(current_user_ns(), result.uint_32);
84 		if (!gid_valid(gid))
85 			return -EINVAL;
86 		ctx->o.gid = gid;
87 		break;
88 	case opt_ttl:
89 		ctx->o.ttl = msecs_to_jiffies(result.uint_32);
90 		break;
91 	case opt_dmode:
92 		if (result.uint_32 & ~0777)
93 			return -EINVAL;
94 		ctx->o.dmode = result.uint_32;
95 		ctx->o.dmode_set = true;
96 		break;
97 	case opt_fmode:
98 		if (result.uint_32 & ~0777)
99 			return -EINVAL;
100 		ctx->o.fmode = result.uint_32;
101 		ctx->o.fmode_set = true;
102 		break;
103 	case opt_dmask:
104 		if (result.uint_32 & ~07777)
105 			return -EINVAL;
106 		ctx->o.dmask = result.uint_32;
107 		break;
108 	case opt_fmask:
109 		if (result.uint_32 & ~07777)
110 			return -EINVAL;
111 		ctx->o.fmask = result.uint_32;
112 		break;
113 	default:
114 		return -EINVAL;
115 	}
116 
117 	return 0;
118 }
119 
vboxsf_fill_super(struct super_block * sb,struct fs_context * fc)120 static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
121 {
122 	struct vboxsf_fs_context *ctx = fc->fs_private;
123 	struct shfl_string *folder_name, root_path;
124 	struct vboxsf_sbi *sbi;
125 	struct dentry *droot;
126 	struct inode *iroot;
127 	char *nls_name;
128 	size_t size;
129 	int err;
130 
131 	if (!fc->source)
132 		return -EINVAL;
133 
134 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
135 	if (!sbi)
136 		return -ENOMEM;
137 
138 	sbi->o = ctx->o;
139 	idr_init(&sbi->ino_idr);
140 	spin_lock_init(&sbi->ino_idr_lock);
141 	sbi->next_generation = 1;
142 	sbi->bdi_id = -1;
143 
144 	/* Load nls if not utf8 */
145 	nls_name = ctx->nls_name ? ctx->nls_name : vboxsf_default_nls;
146 	if (strcmp(nls_name, "utf8") != 0) {
147 		if (nls_name == vboxsf_default_nls)
148 			sbi->nls = load_nls_default();
149 		else
150 			sbi->nls = load_nls(nls_name);
151 
152 		if (!sbi->nls) {
153 			vbg_err("vboxsf: Count not load '%s' nls\n", nls_name);
154 			err = -EINVAL;
155 			goto fail_destroy_idr;
156 		}
157 	}
158 
159 	sbi->bdi_id = ida_simple_get(&vboxsf_bdi_ida, 0, 0, GFP_KERNEL);
160 	if (sbi->bdi_id < 0) {
161 		err = sbi->bdi_id;
162 		goto fail_free;
163 	}
164 
165 	err = super_setup_bdi_name(sb, "vboxsf-%d", sbi->bdi_id);
166 	if (err)
167 		goto fail_free;
168 	sb->s_bdi->ra_pages = 0;
169 	sb->s_bdi->io_pages = 0;
170 
171 	/* Turn source into a shfl_string and map the folder */
172 	size = strlen(fc->source) + 1;
173 	folder_name = kmalloc(SHFLSTRING_HEADER_SIZE + size, GFP_KERNEL);
174 	if (!folder_name) {
175 		err = -ENOMEM;
176 		goto fail_free;
177 	}
178 	folder_name->size = size;
179 	folder_name->length = size - 1;
180 	strscpy(folder_name->string.utf8, fc->source, size);
181 	err = vboxsf_map_folder(folder_name, &sbi->root);
182 	kfree(folder_name);
183 	if (err) {
184 		vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
185 			fc->source, err);
186 		goto fail_free;
187 	}
188 
189 	root_path.length = 1;
190 	root_path.size = 2;
191 	root_path.string.utf8[0] = '/';
192 	root_path.string.utf8[1] = 0;
193 	err = vboxsf_stat(sbi, &root_path, &sbi->root_info);
194 	if (err)
195 		goto fail_unmap;
196 
197 	sb->s_magic = VBOXSF_SUPER_MAGIC;
198 	sb->s_blocksize = 1024;
199 	sb->s_maxbytes = MAX_LFS_FILESIZE;
200 	sb->s_op = &vboxsf_super_ops;
201 	sb->s_d_op = &vboxsf_dentry_ops;
202 
203 	iroot = iget_locked(sb, 0);
204 	if (!iroot) {
205 		err = -ENOMEM;
206 		goto fail_unmap;
207 	}
208 	vboxsf_init_inode(sbi, iroot, &sbi->root_info, false);
209 	unlock_new_inode(iroot);
210 
211 	droot = d_make_root(iroot);
212 	if (!droot) {
213 		err = -ENOMEM;
214 		goto fail_unmap;
215 	}
216 
217 	sb->s_root = droot;
218 	sb->s_fs_info = sbi;
219 	return 0;
220 
221 fail_unmap:
222 	vboxsf_unmap_folder(sbi->root);
223 fail_free:
224 	if (sbi->bdi_id >= 0)
225 		ida_simple_remove(&vboxsf_bdi_ida, sbi->bdi_id);
226 	if (sbi->nls)
227 		unload_nls(sbi->nls);
228 fail_destroy_idr:
229 	idr_destroy(&sbi->ino_idr);
230 	kfree(sbi);
231 	return err;
232 }
233 
vboxsf_inode_init_once(void * data)234 static void vboxsf_inode_init_once(void *data)
235 {
236 	struct vboxsf_inode *sf_i = data;
237 
238 	mutex_init(&sf_i->handle_list_mutex);
239 	inode_init_once(&sf_i->vfs_inode);
240 }
241 
vboxsf_alloc_inode(struct super_block * sb)242 static struct inode *vboxsf_alloc_inode(struct super_block *sb)
243 {
244 	struct vboxsf_inode *sf_i;
245 
246 	sf_i = alloc_inode_sb(sb, vboxsf_inode_cachep, GFP_NOFS);
247 	if (!sf_i)
248 		return NULL;
249 
250 	sf_i->force_restat = 0;
251 	INIT_LIST_HEAD(&sf_i->handle_list);
252 
253 	return &sf_i->vfs_inode;
254 }
255 
vboxsf_free_inode(struct inode * inode)256 static void vboxsf_free_inode(struct inode *inode)
257 {
258 	struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
259 	unsigned long flags;
260 
261 	spin_lock_irqsave(&sbi->ino_idr_lock, flags);
262 	idr_remove(&sbi->ino_idr, inode->i_ino);
263 	spin_unlock_irqrestore(&sbi->ino_idr_lock, flags);
264 	kmem_cache_free(vboxsf_inode_cachep, VBOXSF_I(inode));
265 }
266 
vboxsf_put_super(struct super_block * sb)267 static void vboxsf_put_super(struct super_block *sb)
268 {
269 	struct vboxsf_sbi *sbi = VBOXSF_SBI(sb);
270 
271 	vboxsf_unmap_folder(sbi->root);
272 	if (sbi->bdi_id >= 0)
273 		ida_simple_remove(&vboxsf_bdi_ida, sbi->bdi_id);
274 	if (sbi->nls)
275 		unload_nls(sbi->nls);
276 
277 	/*
278 	 * vboxsf_free_inode uses the idr, make sure all delayed rcu free
279 	 * inodes are flushed.
280 	 */
281 	rcu_barrier();
282 	idr_destroy(&sbi->ino_idr);
283 	kfree(sbi);
284 }
285 
vboxsf_statfs(struct dentry * dentry,struct kstatfs * stat)286 static int vboxsf_statfs(struct dentry *dentry, struct kstatfs *stat)
287 {
288 	struct super_block *sb = dentry->d_sb;
289 	struct shfl_volinfo shfl_volinfo;
290 	struct vboxsf_sbi *sbi;
291 	u32 buf_len;
292 	int err;
293 
294 	sbi = VBOXSF_SBI(sb);
295 	buf_len = sizeof(shfl_volinfo);
296 	err = vboxsf_fsinfo(sbi->root, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
297 			    &buf_len, &shfl_volinfo);
298 	if (err)
299 		return err;
300 
301 	stat->f_type = VBOXSF_SUPER_MAGIC;
302 	stat->f_bsize = shfl_volinfo.bytes_per_allocation_unit;
303 
304 	do_div(shfl_volinfo.total_allocation_bytes,
305 	       shfl_volinfo.bytes_per_allocation_unit);
306 	stat->f_blocks = shfl_volinfo.total_allocation_bytes;
307 
308 	do_div(shfl_volinfo.available_allocation_bytes,
309 	       shfl_volinfo.bytes_per_allocation_unit);
310 	stat->f_bfree  = shfl_volinfo.available_allocation_bytes;
311 	stat->f_bavail = shfl_volinfo.available_allocation_bytes;
312 
313 	stat->f_files = 1000;
314 	/*
315 	 * Don't return 0 here since the guest may then think that it is not
316 	 * possible to create any more files.
317 	 */
318 	stat->f_ffree = 1000000;
319 	stat->f_fsid.val[0] = 0;
320 	stat->f_fsid.val[1] = 0;
321 	stat->f_namelen = 255;
322 	return 0;
323 }
324 
325 static struct super_operations vboxsf_super_ops = {
326 	.alloc_inode	= vboxsf_alloc_inode,
327 	.free_inode	= vboxsf_free_inode,
328 	.put_super	= vboxsf_put_super,
329 	.statfs		= vboxsf_statfs,
330 };
331 
vboxsf_setup(void)332 static int vboxsf_setup(void)
333 {
334 	int err;
335 
336 	mutex_lock(&vboxsf_setup_mutex);
337 
338 	if (vboxsf_setup_done)
339 		goto success;
340 
341 	vboxsf_inode_cachep =
342 		kmem_cache_create("vboxsf_inode_cache",
343 				  sizeof(struct vboxsf_inode), 0,
344 				  (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
345 				   SLAB_ACCOUNT),
346 				  vboxsf_inode_init_once);
347 	if (!vboxsf_inode_cachep) {
348 		err = -ENOMEM;
349 		goto fail_nomem;
350 	}
351 
352 	err = vboxsf_connect();
353 	if (err) {
354 		vbg_err("vboxsf: err %d connecting to guest PCI-device\n", err);
355 		vbg_err("vboxsf: make sure you are inside a VirtualBox VM\n");
356 		vbg_err("vboxsf: and check dmesg for vboxguest errors\n");
357 		goto fail_free_cache;
358 	}
359 
360 	err = vboxsf_set_utf8();
361 	if (err) {
362 		vbg_err("vboxsf_setutf8 error %d\n", err);
363 		goto fail_disconnect;
364 	}
365 
366 	if (!follow_symlinks) {
367 		err = vboxsf_set_symlinks();
368 		if (err)
369 			vbg_warn("vboxsf: Unable to show symlinks: %d\n", err);
370 	}
371 
372 	vboxsf_setup_done = true;
373 success:
374 	mutex_unlock(&vboxsf_setup_mutex);
375 	return 0;
376 
377 fail_disconnect:
378 	vboxsf_disconnect();
379 fail_free_cache:
380 	kmem_cache_destroy(vboxsf_inode_cachep);
381 fail_nomem:
382 	mutex_unlock(&vboxsf_setup_mutex);
383 	return err;
384 }
385 
vboxsf_parse_monolithic(struct fs_context * fc,void * data)386 static int vboxsf_parse_monolithic(struct fs_context *fc, void *data)
387 {
388 	if (data && !memcmp(data, VBSF_MOUNT_SIGNATURE, 4)) {
389 		vbg_err("vboxsf: Old binary mount data not supported, remove obsolete mount.vboxsf and/or update your VBoxService.\n");
390 		return -EINVAL;
391 	}
392 
393 	return generic_parse_monolithic(fc, data);
394 }
395 
vboxsf_get_tree(struct fs_context * fc)396 static int vboxsf_get_tree(struct fs_context *fc)
397 {
398 	int err;
399 
400 	err = vboxsf_setup();
401 	if (err)
402 		return err;
403 
404 	return get_tree_nodev(fc, vboxsf_fill_super);
405 }
406 
vboxsf_reconfigure(struct fs_context * fc)407 static int vboxsf_reconfigure(struct fs_context *fc)
408 {
409 	struct vboxsf_sbi *sbi = VBOXSF_SBI(fc->root->d_sb);
410 	struct vboxsf_fs_context *ctx = fc->fs_private;
411 	struct inode *iroot = fc->root->d_sb->s_root->d_inode;
412 
413 	/* Apply changed options to the root inode */
414 	sbi->o = ctx->o;
415 	vboxsf_init_inode(sbi, iroot, &sbi->root_info, true);
416 
417 	return 0;
418 }
419 
vboxsf_free_fc(struct fs_context * fc)420 static void vboxsf_free_fc(struct fs_context *fc)
421 {
422 	struct vboxsf_fs_context *ctx = fc->fs_private;
423 
424 	kfree(ctx->nls_name);
425 	kfree(ctx);
426 }
427 
428 static const struct fs_context_operations vboxsf_context_ops = {
429 	.free			= vboxsf_free_fc,
430 	.parse_param		= vboxsf_parse_param,
431 	.parse_monolithic	= vboxsf_parse_monolithic,
432 	.get_tree		= vboxsf_get_tree,
433 	.reconfigure		= vboxsf_reconfigure,
434 };
435 
vboxsf_init_fs_context(struct fs_context * fc)436 static int vboxsf_init_fs_context(struct fs_context *fc)
437 {
438 	struct vboxsf_fs_context *ctx;
439 
440 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
441 	if (!ctx)
442 		return -ENOMEM;
443 
444 	current_uid_gid(&ctx->o.uid, &ctx->o.gid);
445 
446 	fc->fs_private = ctx;
447 	fc->ops = &vboxsf_context_ops;
448 	return 0;
449 }
450 
451 static struct file_system_type vboxsf_fs_type = {
452 	.owner			= THIS_MODULE,
453 	.name			= "vboxsf",
454 	.init_fs_context	= vboxsf_init_fs_context,
455 	.kill_sb		= kill_anon_super
456 };
457 
458 /* Module initialization/finalization handlers */
vboxsf_init(void)459 static int __init vboxsf_init(void)
460 {
461 	return register_filesystem(&vboxsf_fs_type);
462 }
463 
vboxsf_fini(void)464 static void __exit vboxsf_fini(void)
465 {
466 	unregister_filesystem(&vboxsf_fs_type);
467 
468 	mutex_lock(&vboxsf_setup_mutex);
469 	if (vboxsf_setup_done) {
470 		vboxsf_disconnect();
471 		/*
472 		 * Make sure all delayed rcu free inodes are flushed
473 		 * before we destroy the cache.
474 		 */
475 		rcu_barrier();
476 		kmem_cache_destroy(vboxsf_inode_cachep);
477 	}
478 	mutex_unlock(&vboxsf_setup_mutex);
479 }
480 
481 module_init(vboxsf_init);
482 module_exit(vboxsf_fini);
483 
484 MODULE_DESCRIPTION("Oracle VM VirtualBox Module for Host File System Access");
485 MODULE_AUTHOR("Oracle Corporation");
486 MODULE_LICENSE("GPL v2");
487 MODULE_ALIAS_FS("vboxsf");
488