mount.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
  4. *
  5. * Copyright (c) 2001-3 Patrick Mochel
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  8. *
  9. * Please see Documentation/filesystems/sysfs.txt for more information.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/magic.h>
  13. #include <linux/mount.h>
  14. #include <linux/init.h>
  15. #include <linux/user_namespace.h>
  16. #include "sysfs.h"
  17. static struct kernfs_root *sysfs_root;
  18. struct kernfs_node *sysfs_root_kn;
  19. static struct dentry *sysfs_mount(struct file_system_type *fs_type,
  20. int flags, const char *dev_name, void *data)
  21. {
  22. struct dentry *root;
  23. void *ns;
  24. bool new_sb = false;
  25. if (!(flags & SB_KERNMOUNT)) {
  26. if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
  27. return ERR_PTR(-EPERM);
  28. }
  29. ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
  30. root = kernfs_mount_ns(fs_type, flags, sysfs_root,
  31. SYSFS_MAGIC, &new_sb, ns);
  32. if (!new_sb)
  33. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  34. else if (!IS_ERR(root))
  35. root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
  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, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
  54. NULL);
  55. if (IS_ERR(sysfs_root))
  56. return PTR_ERR(sysfs_root);
  57. sysfs_root_kn = sysfs_root->kn;
  58. err = register_filesystem(&sysfs_fs_type);
  59. if (err) {
  60. kernfs_destroy_root(sysfs_root);
  61. return err;
  62. }
  63. return 0;
  64. }