xref: /linux/fs/vboxsf/super.c (revision e8cc149ed906a371a5962ff8065393bae28165c9)
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_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),
52 	{}
53 };
54 
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 	int opt;
60 
61 	opt = fs_parse(fc, vboxsf_fs_parameters, param, &result);
62 	if (opt < 0)
63 		return opt;
64 
65 	switch (opt) {
66 	case opt_nls:
67 		if (ctx->nls_name || fc->purpose != FS_CONTEXT_FOR_MOUNT) {
68 			vbg_err("vboxsf: Cannot reconfigure nls option\n");
69 			return -EINVAL;
70 		}
71 		ctx->nls_name = param->string;
72 		param->string = NULL;
73 		break;
74 	case opt_uid:
75 		ctx->o.uid = result.uid;
76 		break;
77 	case opt_gid:
78 		ctx->o.gid = result.gid;
79 		break;
80 	case opt_ttl:
81 		ctx->o.ttl = msecs_to_jiffies(result.uint_32);
82 		break;
83 	case opt_dmode:
84 		if (result.uint_32 & ~0777)
85 			return -EINVAL;
86 		ctx->o.dmode = result.uint_32;
87 		ctx->o.dmode_set = true;
88 		break;
89 	case opt_fmode:
90 		if (result.uint_32 & ~0777)
91 			return -EINVAL;
92 		ctx->o.fmode = result.uint_32;
93 		ctx->o.fmode_set = true;
94 		break;
95 	case opt_dmask:
96 		if (result.uint_32 & ~07777)
97 			return -EINVAL;
98 		ctx->o.dmask = result.uint_32;
99 		break;
100 	case opt_fmask:
101 		if (result.uint_32 & ~07777)
102 			return -EINVAL;
103 		ctx->o.fmask = result.uint_32;
104 		break;
105 	default:
106 		return -EINVAL;
107 	}
108 
109 	return 0;
110 }
111 
112 static int vboxsf_fill_super(struct super_block *sb, struct fs_context *fc)
113 {
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;
118 	struct inode *iroot;
119 	char *nls_name;
120 	size_t size;
121 	int err;
122 
123 	if (!fc->source)
124 		return -EINVAL;
125 
126 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
127 	if (!sbi)
128 		return -ENOMEM;
129 
130 	sbi->o = ctx->o;
131 	idr_init(&sbi->ino_idr);
132 	spin_lock_init(&sbi->ino_idr_lock);
133 	sbi->next_generation = 1;
134 	sbi->bdi_id = -1;
135 
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();
141 		else
142 			sbi->nls = load_nls(nls_name);
143 
144 		if (!sbi->nls) {
145 			vbg_err("vboxsf: Count not load '%s' nls\n", nls_name);
146 			err = -EINVAL;
147 			goto fail_destroy_idr;
148 		}
149 	}
150 
151 	sbi->bdi_id = ida_alloc(&vboxsf_bdi_ida, GFP_KERNEL);
152 	if (sbi->bdi_id < 0) {
153 		err = sbi->bdi_id;
154 		goto fail_free;
155 	}
156 
157 	err = super_setup_bdi_name(sb, "vboxsf-%d", sbi->bdi_id);
158 	if (err)
159 		goto fail_free;
160 	sb->s_bdi->ra_pages = 0;
161 	sb->s_bdi->io_pages = 0;
162 
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);
166 	if (!folder_name) {
167 		err = -ENOMEM;
168 		goto fail_free;
169 	}
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);
174 	kfree(folder_name);
175 	if (err) {
176 		vbg_err("vboxsf: Host rejected mount of '%s' with error %d\n",
177 			fc->source, err);
178 		goto fail_free;
179 	}
180 
181 	root_path.length = 1;
182 	root_path.size = 2;
183 	root_path.string.utf8[0] = '/';
184 	root_path.string.utf8[1] = 0;
185 	err = vboxsf_stat(sbi, &root_path, &sbi->root_info);
186 	if (err)
187 		goto fail_unmap;
188 
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;
194 
195 	iroot = iget_locked(sb, 0);
196 	if (!iroot) {
197 		err = -ENOMEM;
198 		goto fail_unmap;
199 	}
200 	vboxsf_init_inode(sbi, iroot, &sbi->root_info, false);
201 	unlock_new_inode(iroot);
202 
203 	droot = d_make_root(iroot);
204 	if (!droot) {
205 		err = -ENOMEM;
206 		goto fail_unmap;
207 	}
208 
209 	sb->s_root = droot;
210 	sb->s_fs_info = sbi;
211 	return 0;
212 
213 fail_unmap:
214 	vboxsf_unmap_folder(sbi->root);
215 fail_free:
216 	if (sbi->bdi_id >= 0)
217 		ida_free(&vboxsf_bdi_ida, sbi->bdi_id);
218 	if (sbi->nls)
219 		unload_nls(sbi->nls);
220 fail_destroy_idr:
221 	idr_destroy(&sbi->ino_idr);
222 	kfree(sbi);
223 	return err;
224 }
225 
226 static void vboxsf_inode_init_once(void *data)
227 {
228 	struct vboxsf_inode *sf_i = data;
229 
230 	mutex_init(&sf_i->handle_list_mutex);
231 	inode_init_once(&sf_i->vfs_inode);
232 }
233 
234 static struct inode *vboxsf_alloc_inode(struct super_block *sb)
235 {
236 	struct vboxsf_inode *sf_i;
237 
238 	sf_i = alloc_inode_sb(sb, vboxsf_inode_cachep, GFP_NOFS);
239 	if (!sf_i)
240 		return NULL;
241 
242 	sf_i->force_restat = 0;
243 	INIT_LIST_HEAD(&sf_i->handle_list);
244 
245 	return &sf_i->vfs_inode;
246 }
247 
248 static void vboxsf_free_inode(struct inode *inode)
249 {
250 	struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
251 	unsigned long flags;
252 
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));
257 }
258 
259 static void vboxsf_put_super(struct super_block *sb)
260 {
261 	struct vboxsf_sbi *sbi = VBOXSF_SBI(sb);
262 
263 	vboxsf_unmap_folder(sbi->root);
264 	if (sbi->bdi_id >= 0)
265 		ida_free(&vboxsf_bdi_ida, sbi->bdi_id);
266 	if (sbi->nls)
267 		unload_nls(sbi->nls);
268 
269 	/*
270 	 * vboxsf_free_inode uses the idr, make sure all delayed rcu free
271 	 * inodes are flushed.
272 	 */
273 	rcu_barrier();
274 	idr_destroy(&sbi->ino_idr);
275 	kfree(sbi);
276 }
277 
278 static int vboxsf_statfs(struct dentry *dentry, struct kstatfs *stat)
279 {
280 	struct super_block *sb = dentry->d_sb;
281 	struct shfl_volinfo shfl_volinfo;
282 	struct vboxsf_sbi *sbi;
283 	u32 buf_len;
284 	int err;
285 
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);
290 	if (err)
291 		return err;
292 
293 	stat->f_type = VBOXSF_SUPER_MAGIC;
294 	stat->f_bsize = shfl_volinfo.bytes_per_allocation_unit;
295 
296 	do_div(shfl_volinfo.total_allocation_bytes,
297 	       shfl_volinfo.bytes_per_allocation_unit);
298 	stat->f_blocks = shfl_volinfo.total_allocation_bytes;
299 
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;
304 
305 	stat->f_files = 1000;
306 	/*
307 	 * Don't return 0 here since the guest may then think that it is not
308 	 * possible to create any more files.
309 	 */
310 	stat->f_ffree = 1000000;
311 	stat->f_fsid.val[0] = 0;
312 	stat->f_fsid.val[1] = 0;
313 	stat->f_namelen = 255;
314 	return 0;
315 }
316 
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,
322 };
323 
324 static int vboxsf_setup(void)
325 {
326 	int err;
327 
328 	mutex_lock(&vboxsf_setup_mutex);
329 
330 	if (vboxsf_setup_done)
331 		goto success;
332 
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) {
339 		err = -ENOMEM;
340 		goto fail_nomem;
341 	}
342 
343 	err = vboxsf_connect();
344 	if (err) {
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;
349 	}
350 
351 	err = vboxsf_set_utf8();
352 	if (err) {
353 		vbg_err("vboxsf_setutf8 error %d\n", err);
354 		goto fail_disconnect;
355 	}
356 
357 	if (!follow_symlinks) {
358 		err = vboxsf_set_symlinks();
359 		if (err)
360 			vbg_warn("vboxsf: Unable to show symlinks: %d\n", err);
361 	}
362 
363 	vboxsf_setup_done = true;
364 success:
365 	mutex_unlock(&vboxsf_setup_mutex);
366 	return 0;
367 
368 fail_disconnect:
369 	vboxsf_disconnect();
370 fail_free_cache:
371 	kmem_cache_destroy(vboxsf_inode_cachep);
372 fail_nomem:
373 	mutex_unlock(&vboxsf_setup_mutex);
374 	return err;
375 }
376 
377 static int vboxsf_parse_monolithic(struct fs_context *fc, void *data)
378 {
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");
381 		return -EINVAL;
382 	}
383 
384 	return generic_parse_monolithic(fc, data);
385 }
386 
387 static int vboxsf_get_tree(struct fs_context *fc)
388 {
389 	int err;
390 
391 	err = vboxsf_setup();
392 	if (err)
393 		return err;
394 
395 	return get_tree_nodev(fc, vboxsf_fill_super);
396 }
397 
398 static int vboxsf_reconfigure(struct fs_context *fc)
399 {
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;
403 
404 	/* Apply changed options to the root inode */
405 	sbi->o = ctx->o;
406 	vboxsf_init_inode(sbi, iroot, &sbi->root_info, true);
407 
408 	return 0;
409 }
410 
411 static void vboxsf_free_fc(struct fs_context *fc)
412 {
413 	struct vboxsf_fs_context *ctx = fc->fs_private;
414 
415 	kfree(ctx->nls_name);
416 	kfree(ctx);
417 }
418 
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,
425 };
426 
427 static int vboxsf_init_fs_context(struct fs_context *fc)
428 {
429 	struct vboxsf_fs_context *ctx;
430 
431 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
432 	if (!ctx)
433 		return -ENOMEM;
434 
435 	current_uid_gid(&ctx->o.uid, &ctx->o.gid);
436 
437 	fc->fs_private = ctx;
438 	fc->ops = &vboxsf_context_ops;
439 	return 0;
440 }
441 
442 static struct file_system_type vboxsf_fs_type = {
443 	.owner			= THIS_MODULE,
444 	.name			= "vboxsf",
445 	.init_fs_context	= vboxsf_init_fs_context,
446 	.kill_sb		= kill_anon_super
447 };
448 
449 /* Module initialization/finalization handlers */
450 static int __init vboxsf_init(void)
451 {
452 	return register_filesystem(&vboxsf_fs_type);
453 }
454 
455 static void __exit vboxsf_fini(void)
456 {
457 	unregister_filesystem(&vboxsf_fs_type);
458 
459 	mutex_lock(&vboxsf_setup_mutex);
460 	if (vboxsf_setup_done) {
461 		vboxsf_disconnect();
462 		/*
463 		 * Make sure all delayed rcu free inodes are flushed
464 		 * before we destroy the cache.
465 		 */
466 		rcu_barrier();
467 		kmem_cache_destroy(vboxsf_inode_cachep);
468 	}
469 	mutex_unlock(&vboxsf_setup_mutex);
470 }
471 
472 module_init(vboxsf_init);
473 module_exit(vboxsf_fini);
474 
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");
479