super.c 11 KB

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