mount.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if (!(flags & MS_KERNMOUNT)) {
  26. if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
  27. return ERR_PTR(-EPERM);
  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, ns);
  33. if (IS_ERR(root))
  34. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  35. return root;
  36. }
  37. static void sysfs_kill_sb(struct super_block *sb)
  38. {
  39. void *ns = (void *)kernfs_super_ns(sb);
  40. kernfs_kill_sb(sb);
  41. kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
  42. }
  43. static struct file_system_type sysfs_fs_type = {
  44. .name = "sysfs",
  45. .mount = sysfs_mount,
  46. .kill_sb = sysfs_kill_sb,
  47. .fs_flags = FS_USERNS_MOUNT,
  48. };
  49. int __init sysfs_init(void)
  50. {
  51. int err;
  52. sysfs_root = kernfs_create_root(NULL, NULL);
  53. if (IS_ERR(sysfs_root))
  54. return PTR_ERR(sysfs_root);
  55. sysfs_root_kn = sysfs_root->kn;
  56. err = register_filesystem(&sysfs_fs_type);
  57. if (err) {
  58. kernfs_destroy_root(sysfs_root);
  59. return err;
  60. }
  61. return 0;
  62. }