mount.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * mount.c - operations for initializing and mounting sysfs.
  3. */
  4. #define DEBUG
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/init.h>
  9. #include "sysfs.h"
  10. /* Random magic number */
  11. #define SYSFS_MAGIC 0x62656572
  12. struct vfsmount *sysfs_mount;
  13. struct super_block * sysfs_sb = NULL;
  14. kmem_cache_t *sysfs_dir_cachep;
  15. static struct super_operations sysfs_ops = {
  16. .statfs = simple_statfs,
  17. .drop_inode = generic_delete_inode,
  18. };
  19. static struct sysfs_dirent sysfs_root = {
  20. .s_sibling = LIST_HEAD_INIT(sysfs_root.s_sibling),
  21. .s_children = LIST_HEAD_INIT(sysfs_root.s_children),
  22. .s_element = NULL,
  23. .s_type = SYSFS_ROOT,
  24. };
  25. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  26. {
  27. struct inode *inode;
  28. struct dentry *root;
  29. sb->s_blocksize = PAGE_CACHE_SIZE;
  30. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  31. sb->s_magic = SYSFS_MAGIC;
  32. sb->s_op = &sysfs_ops;
  33. sb->s_time_gran = 1;
  34. sysfs_sb = sb;
  35. inode = sysfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
  36. if (inode) {
  37. inode->i_op = &sysfs_dir_inode_operations;
  38. inode->i_fop = &sysfs_dir_operations;
  39. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  40. inode->i_nlink++;
  41. } else {
  42. pr_debug("sysfs: could not get root inode\n");
  43. return -ENOMEM;
  44. }
  45. root = d_alloc_root(inode);
  46. if (!root) {
  47. pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
  48. iput(inode);
  49. return -ENOMEM;
  50. }
  51. root->d_fsdata = &sysfs_root;
  52. sb->s_root = root;
  53. return 0;
  54. }
  55. static struct super_block *sysfs_get_sb(struct file_system_type *fs_type,
  56. int flags, const char *dev_name, void *data)
  57. {
  58. return get_sb_single(fs_type, flags, data, sysfs_fill_super);
  59. }
  60. static struct file_system_type sysfs_fs_type = {
  61. .name = "sysfs",
  62. .get_sb = sysfs_get_sb,
  63. .kill_sb = kill_litter_super,
  64. };
  65. int __init sysfs_init(void)
  66. {
  67. int err = -ENOMEM;
  68. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  69. sizeof(struct sysfs_dirent),
  70. 0, 0, NULL, NULL);
  71. if (!sysfs_dir_cachep)
  72. goto out;
  73. err = register_filesystem(&sysfs_fs_type);
  74. if (!err) {
  75. sysfs_mount = kern_mount(&sysfs_fs_type);
  76. if (IS_ERR(sysfs_mount)) {
  77. printk(KERN_ERR "sysfs: could not mount!\n");
  78. err = PTR_ERR(sysfs_mount);
  79. sysfs_mount = NULL;
  80. unregister_filesystem(&sysfs_fs_type);
  81. goto out_err;
  82. }
  83. } else
  84. goto out_err;
  85. out:
  86. return err;
  87. out_err:
  88. kmem_cache_destroy(sysfs_dir_cachep);
  89. sysfs_dir_cachep = NULL;
  90. goto out;
  91. }