mount.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/magic.h>
  15. #include <linux/mount.h>
  16. #include <linux/init.h>
  17. #include <linux/user_namespace.h>
  18. #include "sysfs.h"
  19. static struct kernfs_root *sysfs_root;
  20. struct kernfs_node *sysfs_root_kn;
  21. static struct dentry *sysfs_mount(struct file_system_type *fs_type,
  22. int flags, const char *dev_name, void *data)
  23. {
  24. struct dentry *root;
  25. void *ns;
  26. bool new_sb;
  27. if (!(flags & MS_KERNMOUNT)) {
  28. if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
  29. return ERR_PTR(-EPERM);
  30. }
  31. ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
  32. root = kernfs_mount_ns(fs_type, flags, sysfs_root,
  33. SYSFS_MAGIC, &new_sb, ns);
  34. if (IS_ERR(root) || !new_sb)
  35. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  36. else if (new_sb)
  37. /* Userspace would break if executables appear on sysfs */
  38. root->d_sb->s_iflags |= SB_I_NOEXEC;
  39. return root;
  40. }
  41. static void sysfs_kill_sb(struct super_block *sb)
  42. {
  43. void *ns = (void *)kernfs_super_ns(sb);
  44. kernfs_kill_sb(sb);
  45. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  46. }
  47. static struct file_system_type sysfs_fs_type = {
  48. .name = "sysfs",
  49. .mount = sysfs_mount,
  50. .kill_sb = sysfs_kill_sb,
  51. .fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
  52. };
  53. int __init sysfs_init(void)
  54. {
  55. int err;
  56. sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
  57. NULL);
  58. if (IS_ERR(sysfs_root))
  59. return PTR_ERR(sysfs_root);
  60. sysfs_root_kn = sysfs_root->kn;
  61. err = register_filesystem(&sysfs_fs_type);
  62. if (err) {
  63. kernfs_destroy_root(sysfs_root);
  64. return err;
  65. }
  66. return 0;
  67. }