super.c 11 KB

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