mount.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "sysfs.h"
  21. static struct vfsmount *sysfs_mount;
  22. struct kmem_cache *sysfs_dir_cachep;
  23. static const struct super_operations sysfs_ops = {
  24. .statfs = simple_statfs,
  25. .drop_inode = generic_delete_inode,
  26. .evict_inode = sysfs_evict_inode,
  27. };
  28. struct sysfs_dirent sysfs_root = {
  29. .s_name = "",
  30. .s_count = ATOMIC_INIT(1),
  31. .s_flags = SYSFS_DIR | (KOBJ_NS_TYPE_NONE << SYSFS_NS_TYPE_SHIFT),
  32. .s_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  33. .s_ino = 1,
  34. };
  35. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  36. {
  37. struct inode *inode;
  38. struct dentry *root;
  39. sb->s_blocksize = PAGE_CACHE_SIZE;
  40. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  41. sb->s_magic = SYSFS_MAGIC;
  42. sb->s_op = &sysfs_ops;
  43. sb->s_time_gran = 1;
  44. /* get root inode, initialize and unlock it */
  45. mutex_lock(&sysfs_mutex);
  46. inode = sysfs_get_inode(sb, &sysfs_root);
  47. mutex_unlock(&sysfs_mutex);
  48. if (!inode) {
  49. pr_debug("sysfs: could not get root inode\n");
  50. return -ENOMEM;
  51. }
  52. /* instantiate and link root dentry */
  53. root = d_alloc_root(inode);
  54. if (!root) {
  55. pr_debug("%s: could not get root dentry!\n",__func__);
  56. iput(inode);
  57. return -ENOMEM;
  58. }
  59. root->d_fsdata = &sysfs_root;
  60. sb->s_root = root;
  61. return 0;
  62. }
  63. static int sysfs_test_super(struct super_block *sb, void *data)
  64. {
  65. struct sysfs_super_info *sb_info = sysfs_info(sb);
  66. struct sysfs_super_info *info = data;
  67. enum kobj_ns_type type;
  68. int found = 1;
  69. for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++) {
  70. if (sb_info->ns[type] != info->ns[type])
  71. found = 0;
  72. }
  73. return found;
  74. }
  75. static int sysfs_set_super(struct super_block *sb, void *data)
  76. {
  77. int error;
  78. error = set_anon_super(sb, data);
  79. if (!error)
  80. sb->s_fs_info = data;
  81. return error;
  82. }
  83. static int sysfs_get_sb(struct file_system_type *fs_type,
  84. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  85. {
  86. struct sysfs_super_info *info;
  87. enum kobj_ns_type type;
  88. struct super_block *sb;
  89. int error;
  90. error = -ENOMEM;
  91. info = kzalloc(sizeof(*info), GFP_KERNEL);
  92. if (!info)
  93. goto out;
  94. for (type = KOBJ_NS_TYPE_NONE; type < KOBJ_NS_TYPES; type++)
  95. info->ns[type] = kobj_ns_current(type);
  96. sb = sget(fs_type, sysfs_test_super, sysfs_set_super, info);
  97. if (IS_ERR(sb) || sb->s_fs_info != info)
  98. kfree(info);
  99. if (IS_ERR(sb)) {
  100. error = PTR_ERR(sb);
  101. goto out;
  102. }
  103. if (!sb->s_root) {
  104. sb->s_flags = flags;
  105. error = sysfs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  106. if (error) {
  107. deactivate_locked_super(sb);
  108. goto out;
  109. }
  110. sb->s_flags |= MS_ACTIVE;
  111. }
  112. simple_set_mnt(mnt, sb);
  113. error = 0;
  114. out:
  115. return error;
  116. }
  117. static void sysfs_kill_sb(struct super_block *sb)
  118. {
  119. struct sysfs_super_info *info = sysfs_info(sb);
  120. /* Remove the superblock from fs_supers/s_instances
  121. * so we can't find it, before freeing sysfs_super_info.
  122. */
  123. kill_anon_super(sb);
  124. kfree(info);
  125. }
  126. static struct file_system_type sysfs_fs_type = {
  127. .name = "sysfs",
  128. .get_sb = sysfs_get_sb,
  129. .kill_sb = sysfs_kill_sb,
  130. };
  131. void sysfs_exit_ns(enum kobj_ns_type type, const void *ns)
  132. {
  133. struct super_block *sb;
  134. mutex_lock(&sysfs_mutex);
  135. spin_lock(&sb_lock);
  136. list_for_each_entry(sb, &sysfs_fs_type.fs_supers, s_instances) {
  137. struct sysfs_super_info *info = sysfs_info(sb);
  138. /*
  139. * If we see a superblock on the fs_supers/s_instances
  140. * list the unmount has not completed and sb->s_fs_info
  141. * points to a valid struct sysfs_super_info.
  142. */
  143. /* Ignore superblocks with the wrong ns */
  144. if (info->ns[type] != ns)
  145. continue;
  146. info->ns[type] = NULL;
  147. }
  148. spin_unlock(&sb_lock);
  149. mutex_unlock(&sysfs_mutex);
  150. }
  151. int __init sysfs_init(void)
  152. {
  153. int err = -ENOMEM;
  154. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  155. sizeof(struct sysfs_dirent),
  156. 0, 0, NULL);
  157. if (!sysfs_dir_cachep)
  158. goto out;
  159. err = sysfs_inode_init();
  160. if (err)
  161. goto out_err;
  162. err = register_filesystem(&sysfs_fs_type);
  163. if (!err) {
  164. sysfs_mount = kern_mount(&sysfs_fs_type);
  165. if (IS_ERR(sysfs_mount)) {
  166. printk(KERN_ERR "sysfs: could not mount!\n");
  167. err = PTR_ERR(sysfs_mount);
  168. sysfs_mount = NULL;
  169. unregister_filesystem(&sysfs_fs_type);
  170. goto out_err;
  171. }
  172. } else
  173. goto out_err;
  174. out:
  175. return err;
  176. out_err:
  177. kmem_cache_destroy(sysfs_dir_cachep);
  178. sysfs_dir_cachep = NULL;
  179. goto out;
  180. }
  181. #undef sysfs_get
  182. struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
  183. {
  184. return __sysfs_get(sd);
  185. }
  186. EXPORT_SYMBOL_GPL(sysfs_get);
  187. #undef sysfs_put
  188. void sysfs_put(struct sysfs_dirent *sd)
  189. {
  190. __sysfs_put(sd);
  191. }
  192. EXPORT_SYMBOL_GPL(sysfs_put);