super.c 11 KB

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