mount.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/pagemap.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/magic.h>
  19. #include <linux/slab.h>
  20. #include <linux/user_namespace.h>
  21. #include "sysfs.h"
  22. static struct vfsmount *sysfs_mnt;
  23. struct kmem_cache *sysfs_dir_cachep;
  24. static const struct super_operations sysfs_ops = {
  25. .statfs = simple_statfs,
  26. .drop_inode = generic_delete_inode,
  27. .evict_inode = sysfs_evict_inode,
  28. };
  29. struct sysfs_dirent sysfs_root = {
  30. .s_name = "",
  31. .s_count = ATOMIC_INIT(1),
  32. .s_flags = SYSFS_DIR,
  33. .s_mode = S_IFDIR | S_IRUGO | S_IXUGO,
  34. .s_ino = 1,
  35. };
  36. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  37. {
  38. struct inode *inode;
  39. struct dentry *root;
  40. sb->s_blocksize = PAGE_CACHE_SIZE;
  41. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  42. sb->s_magic = SYSFS_MAGIC;
  43. sb->s_op = &sysfs_ops;
  44. sb->s_time_gran = 1;
  45. /* get root inode, initialize and unlock it */
  46. mutex_lock(&sysfs_mutex);
  47. inode = sysfs_get_inode(sb, &sysfs_root);
  48. mutex_unlock(&sysfs_mutex);
  49. if (!inode) {
  50. pr_debug("sysfs: could not get root inode\n");
  51. return -ENOMEM;
  52. }
  53. /* instantiate and link root dentry */
  54. root = d_make_root(inode);
  55. if (!root) {
  56. pr_debug("%s: could not get root dentry!\n", __func__);
  57. return -ENOMEM;
  58. }
  59. root->d_fsdata = &sysfs_root;
  60. sb->s_root = root;
  61. sb->s_d_op = &sysfs_dentry_ops;
  62. return 0;
  63. }
  64. static int sysfs_test_super(struct super_block *sb, void *data)
  65. {
  66. struct sysfs_super_info *sb_info = sysfs_info(sb);
  67. struct sysfs_super_info *info = data;
  68. return sb_info->ns == info->ns;
  69. }
  70. static int sysfs_set_super(struct super_block *sb, void *data)
  71. {
  72. int error;
  73. error = set_anon_super(sb, data);
  74. if (!error)
  75. sb->s_fs_info = data;
  76. return error;
  77. }
  78. static void free_sysfs_super_info(struct sysfs_super_info *info)
  79. {
  80. kobj_ns_drop(KOBJ_NS_TYPE_NET, info->ns);
  81. kfree(info);
  82. }
  83. static struct dentry *sysfs_mount(struct file_system_type *fs_type,
  84. int flags, const char *dev_name, void *data)
  85. {
  86. struct sysfs_super_info *info;
  87. struct super_block *sb;
  88. int error;
  89. if (!(flags & MS_KERNMOUNT)) {
  90. if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
  91. return ERR_PTR(-EPERM);
  92. if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
  93. return ERR_PTR(-EPERM);
  94. }
  95. info = kzalloc(sizeof(*info), GFP_KERNEL);
  96. if (!info)
  97. return ERR_PTR(-ENOMEM);
  98. info->ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
  99. sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
  100. if (IS_ERR(sb) || sb->s_fs_info != info)
  101. free_sysfs_super_info(info);
  102. if (IS_ERR(sb))
  103. return ERR_CAST(sb);
  104. if (!sb->s_root) {
  105. error = sysfs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  106. if (error) {
  107. deactivate_locked_super(sb);
  108. return ERR_PTR(error);
  109. }
  110. sb->s_flags |= MS_ACTIVE;
  111. }
  112. return dget(sb->s_root);
  113. }
  114. static void sysfs_kill_sb(struct super_block *sb)
  115. {
  116. struct sysfs_super_info *info = sysfs_info(sb);
  117. /* Remove the superblock from fs_supers/s_instances
  118. * so we can't find it, before freeing sysfs_super_info.
  119. */
  120. kill_anon_super(sb);
  121. free_sysfs_super_info(info);
  122. }
  123. static struct file_system_type sysfs_fs_type = {
  124. .name = "sysfs",
  125. .mount = sysfs_mount,
  126. .kill_sb = sysfs_kill_sb,
  127. .fs_flags = FS_USERNS_MOUNT,
  128. };
  129. int __init sysfs_init(void)
  130. {
  131. int err = -ENOMEM;
  132. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  133. sizeof(struct sysfs_dirent),
  134. 0, 0, NULL);
  135. if (!sysfs_dir_cachep)
  136. goto out;
  137. err = sysfs_inode_init();
  138. if (err)
  139. goto out_err;
  140. err = register_filesystem(&sysfs_fs_type);
  141. if (!err) {
  142. sysfs_mnt = kern_mount(&sysfs_fs_type);
  143. if (IS_ERR(sysfs_mnt)) {
  144. printk(KERN_ERR "sysfs: could not mount!\n");
  145. err = PTR_ERR(sysfs_mnt);
  146. sysfs_mnt = NULL;
  147. unregister_filesystem(&sysfs_fs_type);
  148. goto out_err;
  149. }
  150. } else
  151. goto out_err;
  152. out:
  153. return err;
  154. out_err:
  155. kmem_cache_destroy(sysfs_dir_cachep);
  156. sysfs_dir_cachep = NULL;
  157. goto out;
  158. }
  159. #undef sysfs_get
  160. struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
  161. {
  162. return __sysfs_get(sd);
  163. }
  164. EXPORT_SYMBOL_GPL(sysfs_get);
  165. #undef sysfs_put
  166. void sysfs_put(struct sysfs_dirent *sd)
  167. {
  168. __sysfs_put(sd);
  169. }
  170. EXPORT_SYMBOL_GPL(sysfs_put);