mount.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "sysfs.h"
  20. static struct vfsmount *sysfs_mount;
  21. struct super_block * sysfs_sb = NULL;
  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. .delete_inode = sysfs_delete_inode,
  27. };
  28. struct sysfs_dirent sysfs_root = {
  29. .s_name = "",
  30. .s_count = ATOMIC_INIT(1),
  31. .s_flags = SYSFS_DIR,
  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. sysfs_sb = sb;
  45. /* get root inode, initialize and unlock it */
  46. mutex_lock(&sysfs_mutex);
  47. inode = sysfs_get_inode(&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_alloc_root(inode);
  55. if (!root) {
  56. pr_debug("%s: could not get root dentry!\n",__func__);
  57. iput(inode);
  58. return -ENOMEM;
  59. }
  60. root->d_fsdata = &sysfs_root;
  61. sb->s_root = root;
  62. return 0;
  63. }
  64. static int sysfs_get_sb(struct file_system_type *fs_type,
  65. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  66. {
  67. return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
  68. }
  69. static struct file_system_type sysfs_fs_type = {
  70. .name = "sysfs",
  71. .get_sb = sysfs_get_sb,
  72. .kill_sb = kill_anon_super,
  73. };
  74. int __init sysfs_init(void)
  75. {
  76. int err = -ENOMEM;
  77. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  78. sizeof(struct sysfs_dirent),
  79. 0, 0, NULL);
  80. if (!sysfs_dir_cachep)
  81. goto out;
  82. err = sysfs_inode_init();
  83. if (err)
  84. goto out_err;
  85. err = register_filesystem(&sysfs_fs_type);
  86. if (!err) {
  87. sysfs_mount = kern_mount(&sysfs_fs_type);
  88. if (IS_ERR(sysfs_mount)) {
  89. printk(KERN_ERR "sysfs: could not mount!\n");
  90. err = PTR_ERR(sysfs_mount);
  91. sysfs_mount = NULL;
  92. unregister_filesystem(&sysfs_fs_type);
  93. goto out_err;
  94. }
  95. } else
  96. goto out_err;
  97. out:
  98. return err;
  99. out_err:
  100. kmem_cache_destroy(sysfs_dir_cachep);
  101. sysfs_dir_cachep = NULL;
  102. goto out;
  103. }
  104. #undef sysfs_get
  105. struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
  106. {
  107. return __sysfs_get(sd);
  108. }
  109. EXPORT_SYMBOL_GPL(sysfs_get);
  110. #undef sysfs_put
  111. void sysfs_put(struct sysfs_dirent *sd)
  112. {
  113. __sysfs_put(sd);
  114. }
  115. EXPORT_SYMBOL_GPL(sysfs_put);