mount.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 const struct super_operations sysfs_ops = {
  17. .statfs = simple_statfs,
  18. .drop_inode = sysfs_delete_inode,
  19. };
  20. struct sysfs_dirent sysfs_root = {
  21. .s_count = ATOMIC_INIT(1),
  22. .s_flags = SYSFS_ROOT,
  23. .s_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  24. .s_ino = 1,
  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. /* get root inode, initialize and unlock it */
  37. inode = sysfs_get_inode(&sysfs_root);
  38. if (!inode) {
  39. pr_debug("sysfs: could not get root inode\n");
  40. return -ENOMEM;
  41. }
  42. inode->i_op = &sysfs_dir_inode_operations;
  43. inode->i_fop = &sysfs_dir_operations;
  44. inc_nlink(inode); /* directory, account for "." */
  45. unlock_new_inode(inode);
  46. /* instantiate and link root dentry */
  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. sysfs_root.s_dentry = root;
  54. root->d_fsdata = &sysfs_root;
  55. sb->s_root = root;
  56. return 0;
  57. }
  58. static int sysfs_get_sb(struct file_system_type *fs_type,
  59. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  60. {
  61. return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
  62. }
  63. static struct file_system_type sysfs_fs_type = {
  64. .name = "sysfs",
  65. .get_sb = sysfs_get_sb,
  66. .kill_sb = kill_litter_super,
  67. };
  68. int __init sysfs_init(void)
  69. {
  70. int err = -ENOMEM;
  71. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  72. sizeof(struct sysfs_dirent),
  73. 0, 0, NULL);
  74. if (!sysfs_dir_cachep)
  75. goto out;
  76. err = register_filesystem(&sysfs_fs_type);
  77. if (!err) {
  78. sysfs_mount = kern_mount(&sysfs_fs_type);
  79. if (IS_ERR(sysfs_mount)) {
  80. printk(KERN_ERR "sysfs: could not mount!\n");
  81. err = PTR_ERR(sysfs_mount);
  82. sysfs_mount = NULL;
  83. unregister_filesystem(&sysfs_fs_type);
  84. goto out_err;
  85. }
  86. } else
  87. goto out_err;
  88. out:
  89. return err;
  90. out_err:
  91. kmem_cache_destroy(sysfs_dir_cachep);
  92. sysfs_dir_cachep = NULL;
  93. goto out;
  94. }