mount.c 2.7 KB

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