mount.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static struct vfsmount *sysfs_mount;
  13. struct super_block * sysfs_sb = NULL;
  14. struct kmem_cache *sysfs_dir_cachep;
  15. static const struct super_operations sysfs_ops = {
  16. .statfs = simple_statfs,
  17. .drop_inode = generic_delete_inode,
  18. };
  19. struct sysfs_dirent sysfs_root = {
  20. .s_name = "",
  21. .s_count = ATOMIC_INIT(1),
  22. .s_flags = SYSFS_DIR,
  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. /* instantiate and link root dentry */
  43. root = d_alloc_root(inode);
  44. if (!root) {
  45. pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
  46. iput(inode);
  47. return -ENOMEM;
  48. }
  49. root->d_fsdata = &sysfs_root;
  50. sb->s_root = root;
  51. return 0;
  52. }
  53. static int sysfs_get_sb(struct file_system_type *fs_type,
  54. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  55. {
  56. return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
  57. }
  58. static struct file_system_type sysfs_fs_type = {
  59. .name = "sysfs",
  60. .get_sb = sysfs_get_sb,
  61. .kill_sb = kill_anon_super,
  62. };
  63. int __init sysfs_init(void)
  64. {
  65. int err = -ENOMEM;
  66. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  67. sizeof(struct sysfs_dirent),
  68. 0, 0, NULL);
  69. if (!sysfs_dir_cachep)
  70. goto out;
  71. err = register_filesystem(&sysfs_fs_type);
  72. if (!err) {
  73. sysfs_mount = kern_mount(&sysfs_fs_type);
  74. if (IS_ERR(sysfs_mount)) {
  75. printk(KERN_ERR "sysfs: could not mount!\n");
  76. err = PTR_ERR(sysfs_mount);
  77. sysfs_mount = NULL;
  78. unregister_filesystem(&sysfs_fs_type);
  79. goto out_err;
  80. }
  81. } else
  82. goto out_err;
  83. out:
  84. return err;
  85. out_err:
  86. kmem_cache_destroy(sysfs_dir_cachep);
  87. sysfs_dir_cachep = NULL;
  88. goto out;
  89. }