mount.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. .s_iattr = NULL,
  25. };
  26. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  27. {
  28. struct inode *inode;
  29. struct dentry *root;
  30. sb->s_blocksize = PAGE_CACHE_SIZE;
  31. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  32. sb->s_magic = SYSFS_MAGIC;
  33. sb->s_op = &sysfs_ops;
  34. sb->s_time_gran = 1;
  35. sysfs_sb = sb;
  36. inode = sysfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  37. &sysfs_root);
  38. if (inode) {
  39. inode->i_op = &sysfs_dir_inode_operations;
  40. inode->i_fop = &sysfs_dir_operations;
  41. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  42. inode->i_nlink++;
  43. } else {
  44. pr_debug("sysfs: could not get root inode\n");
  45. return -ENOMEM;
  46. }
  47. root = d_alloc_root(inode);
  48. if (!root) {
  49. pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
  50. iput(inode);
  51. return -ENOMEM;
  52. }
  53. root->d_fsdata = &sysfs_root;
  54. sb->s_root = root;
  55. return 0;
  56. }
  57. static struct super_block *sysfs_get_sb(struct file_system_type *fs_type,
  58. int flags, const char *dev_name, void *data)
  59. {
  60. return get_sb_single(fs_type, flags, data, sysfs_fill_super);
  61. }
  62. static struct file_system_type sysfs_fs_type = {
  63. .name = "sysfs",
  64. .get_sb = sysfs_get_sb,
  65. .kill_sb = kill_litter_super,
  66. };
  67. int __init sysfs_init(void)
  68. {
  69. int err = -ENOMEM;
  70. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  71. sizeof(struct sysfs_dirent),
  72. 0, 0, NULL, NULL);
  73. if (!sysfs_dir_cachep)
  74. goto out;
  75. err = register_filesystem(&sysfs_fs_type);
  76. if (!err) {
  77. sysfs_mount = kern_mount(&sysfs_fs_type);
  78. if (IS_ERR(sysfs_mount)) {
  79. printk(KERN_ERR "sysfs: could not mount!\n");
  80. err = PTR_ERR(sysfs_mount);
  81. sysfs_mount = NULL;
  82. unregister_filesystem(&sysfs_fs_type);
  83. goto out_err;
  84. }
  85. } else
  86. goto out_err;
  87. out:
  88. return err;
  89. out_err:
  90. kmem_cache_destroy(sysfs_dir_cachep);
  91. sysfs_dir_cachep = NULL;
  92. goto out;
  93. }