mount.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.rst 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/slab.h>
  16. #include <linux/user_namespace.h>
  17. #include <linux/fs_context.h>
  18. #include <net/net_namespace.h>
  19. #include "sysfs.h"
  20. static struct kernfs_root *sysfs_root;
  21. struct kernfs_node *sysfs_root_kn;
  22. static int sysfs_get_tree(struct fs_context *fc)
  23. {
  24. struct kernfs_fs_context *kfc = fc->fs_private;
  25. int ret;
  26. ret = kernfs_get_tree(fc);
  27. if (ret)
  28. return ret;
  29. if (kfc->new_sb_created)
  30. fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
  31. return 0;
  32. }
  33. static void sysfs_fs_context_free(struct fs_context *fc)
  34. {
  35. struct kernfs_fs_context *kfc = fc->fs_private;
  36. if (kfc->ns_tag)
  37. kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
  38. kernfs_free_fs_context(fc);
  39. kfree(kfc);
  40. }
  41. static const struct fs_context_operations sysfs_fs_context_ops = {
  42. .free = sysfs_fs_context_free,
  43. .get_tree = sysfs_get_tree,
  44. };
  45. static int sysfs_init_fs_context(struct fs_context *fc)
  46. {
  47. struct kernfs_fs_context *kfc;
  48. struct net *netns;
  49. if (!(fc->sb_flags & SB_KERNMOUNT)) {
  50. if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
  51. return -EPERM;
  52. }
  53. kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
  54. if (!kfc)
  55. return -ENOMEM;
  56. kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
  57. kfc->root = sysfs_root;
  58. kfc->magic = SYSFS_MAGIC;
  59. fc->fs_private = kfc;
  60. fc->ops = &sysfs_fs_context_ops;
  61. if (netns) {
  62. put_user_ns(fc->user_ns);
  63. fc->user_ns = get_user_ns(netns->user_ns);
  64. }
  65. fc->global = true;
  66. return 0;
  67. }
  68. static void sysfs_kill_sb(struct super_block *sb)
  69. {
  70. void *ns = (void *)kernfs_super_ns(sb);
  71. kernfs_kill_sb(sb);
  72. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  73. }
  74. static struct file_system_type sysfs_fs_type = {
  75. .name = "sysfs",
  76. .init_fs_context = sysfs_init_fs_context,
  77. .kill_sb = sysfs_kill_sb,
  78. .fs_flags = FS_USERNS_MOUNT,
  79. };
  80. int __init sysfs_init(void)
  81. {
  82. int err;
  83. sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
  84. NULL);
  85. if (IS_ERR(sysfs_root))
  86. return PTR_ERR(sysfs_root);
  87. sysfs_root_kn = sysfs_root->kn;
  88. err = register_filesystem(&sysfs_fs_type);
  89. if (err) {
  90. kernfs_destroy_root(sysfs_root);
  91. return err;
  92. }
  93. return 0;
  94. }