mount.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #define DEBUG
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/init.h>
  16. #include <linux/user_namespace.h>
  17. #include "sysfs.h"
  18. static struct kernfs_root *sysfs_root;
  19. struct kernfs_node *sysfs_root_kn;
  20. static struct dentry *sysfs_mount(struct file_system_type *fs_type,
  21. int flags, const char *dev_name, void *data)
  22. {
  23. struct dentry *root;
  24. void *ns;
  25. bool new_sb;
  26. if (!(flags & MS_KERNMOUNT)) {
  27. if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
  28. return ERR_PTR(-EPERM);
  29. if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
  30. return ERR_PTR(-EPERM);
  31. }
  32. ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
  33. root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
  34. if (IS_ERR(root) || !new_sb)
  35. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  36. return root;
  37. }
  38. static void sysfs_kill_sb(struct super_block *sb)
  39. {
  40. void *ns = (void *)kernfs_super_ns(sb);
  41. kernfs_kill_sb(sb);
  42. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  43. }
  44. static struct file_system_type sysfs_fs_type = {
  45. .name = "sysfs",
  46. .mount = sysfs_mount,
  47. .kill_sb = sysfs_kill_sb,
  48. .fs_flags = FS_USERNS_MOUNT,
  49. };
  50. int __init sysfs_init(void)
  51. {
  52. int err;
  53. sysfs_root = kernfs_create_root(NULL, 0, NULL);
  54. if (IS_ERR(sysfs_root))
  55. return PTR_ERR(sysfs_root);
  56. sysfs_root_kn = sysfs_root->kn;
  57. err = register_filesystem(&sysfs_fs_type);
  58. if (err) {
  59. kernfs_destroy_root(sysfs_root);
  60. return err;
  61. }
  62. return 0;
  63. }