mount.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #define DEBUG
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/init.h>
  17. #include "sysfs.h"
  18. /* Random magic number */
  19. #define SYSFS_MAGIC 0x62656572
  20. static struct vfsmount *sysfs_mount;
  21. struct super_block * sysfs_sb = NULL;
  22. struct kmem_cache *sysfs_dir_cachep;
  23. static const struct super_operations sysfs_ops = {
  24. .statfs = simple_statfs,
  25. .drop_inode = generic_delete_inode,
  26. };
  27. struct sysfs_dirent sysfs_root = {
  28. .s_name = "",
  29. .s_count = ATOMIC_INIT(1),
  30. .s_flags = SYSFS_DIR,
  31. .s_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  32. .s_ino = 1,
  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. /* get root inode, initialize and unlock it */
  45. inode = sysfs_get_inode(&sysfs_root);
  46. if (!inode) {
  47. pr_debug("sysfs: could not get root inode\n");
  48. return -ENOMEM;
  49. }
  50. /* instantiate and link root dentry */
  51. root = d_alloc_root(inode);
  52. if (!root) {
  53. pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
  54. iput(inode);
  55. return -ENOMEM;
  56. }
  57. root->d_fsdata = &sysfs_root;
  58. sb->s_root = root;
  59. return 0;
  60. }
  61. static int sysfs_get_sb(struct file_system_type *fs_type,
  62. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  63. {
  64. return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
  65. }
  66. static struct file_system_type sysfs_fs_type = {
  67. .name = "sysfs",
  68. .get_sb = sysfs_get_sb,
  69. .kill_sb = kill_anon_super,
  70. };
  71. int __init sysfs_init(void)
  72. {
  73. int err = -ENOMEM;
  74. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  75. sizeof(struct sysfs_dirent),
  76. 0, 0, NULL);
  77. if (!sysfs_dir_cachep)
  78. goto out;
  79. err = sysfs_inode_init();
  80. if (err)
  81. goto out_err;
  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. }