mount.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <asm/semaphore.h>
  10. #include "sysfs.h"
  11. /* Random magic number */
  12. #define SYSFS_MAGIC 0x62656572
  13. struct vfsmount *sysfs_mount;
  14. struct super_block * sysfs_sb = NULL;
  15. struct kmem_cache *sysfs_dir_cachep;
  16. static void sysfs_clear_inode(struct inode *inode);
  17. static const struct super_operations sysfs_ops = {
  18. .statfs = simple_statfs,
  19. .drop_inode = sysfs_delete_inode,
  20. .clear_inode = sysfs_clear_inode,
  21. };
  22. static struct sysfs_dirent sysfs_root = {
  23. .s_sibling = LIST_HEAD_INIT(sysfs_root.s_sibling),
  24. .s_children = LIST_HEAD_INIT(sysfs_root.s_children),
  25. .s_element = NULL,
  26. .s_type = SYSFS_ROOT,
  27. .s_iattr = NULL,
  28. };
  29. static void sysfs_clear_inode(struct inode *inode)
  30. {
  31. kfree(inode->i_private);
  32. }
  33. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  34. {
  35. struct inode *inode;
  36. struct dentry *root;
  37. sb->s_blocksize = PAGE_CACHE_SIZE;
  38. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  39. sb->s_magic = SYSFS_MAGIC;
  40. sb->s_op = &sysfs_ops;
  41. sb->s_time_gran = 1;
  42. sysfs_sb = sb;
  43. inode = sysfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  44. &sysfs_root);
  45. if (inode) {
  46. inode->i_op = &sysfs_dir_inode_operations;
  47. inode->i_fop = &sysfs_dir_operations;
  48. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  49. inc_nlink(inode);
  50. } else {
  51. pr_debug("sysfs: could not get root inode\n");
  52. return -ENOMEM;
  53. }
  54. root = d_alloc_root(inode);
  55. if (!root) {
  56. pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
  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_litter_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, NULL);
  80. if (!sysfs_dir_cachep)
  81. goto out;
  82. err = register_filesystem(&sysfs_fs_type);
  83. if (!err) {
  84. sysfs_mount = kern_mount(&sysfs_fs_type);
  85. if (IS_ERR(sysfs_mount)) {
  86. printk(KERN_ERR "sysfs: could not mount!\n");
  87. err = PTR_ERR(sysfs_mount);
  88. sysfs_mount = NULL;
  89. unregister_filesystem(&sysfs_fs_type);
  90. goto out_err;
  91. }
  92. } else
  93. goto out_err;
  94. out:
  95. return err;
  96. out_err:
  97. kmem_cache_destroy(sysfs_dir_cachep);
  98. sysfs_dir_cachep = NULL;
  99. goto out;
  100. }