mount.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <linux/module.h>
  18. #include "sysfs.h"
  19. /* Random magic number */
  20. #define SYSFS_MAGIC 0x62656572
  21. static struct vfsmount *sysfs_mount;
  22. struct super_block * sysfs_sb = NULL;
  23. struct kmem_cache *sysfs_dir_cachep;
  24. static const struct super_operations sysfs_ops = {
  25. .statfs = simple_statfs,
  26. .drop_inode = generic_delete_inode,
  27. };
  28. struct sysfs_dirent sysfs_root = {
  29. .s_name = "",
  30. .s_count = ATOMIC_INIT(1),
  31. .s_flags = SYSFS_DIR,
  32. .s_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  33. .s_ino = 1,
  34. };
  35. static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
  36. {
  37. struct inode *inode;
  38. struct dentry *root;
  39. sb->s_blocksize = PAGE_CACHE_SIZE;
  40. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  41. sb->s_magic = SYSFS_MAGIC;
  42. sb->s_op = &sysfs_ops;
  43. sb->s_time_gran = 1;
  44. sysfs_sb = sb;
  45. /* get root inode, initialize and unlock it */
  46. inode = sysfs_get_inode(&sysfs_root);
  47. if (!inode) {
  48. pr_debug("sysfs: could not get root inode\n");
  49. return -ENOMEM;
  50. }
  51. /* instantiate and link root dentry */
  52. root = d_alloc_root(inode);
  53. if (!root) {
  54. pr_debug("%s: could not get root dentry!\n",__func__);
  55. iput(inode);
  56. return -ENOMEM;
  57. }
  58. root->d_fsdata = &sysfs_root;
  59. sb->s_root = root;
  60. return 0;
  61. }
  62. static int sysfs_get_sb(struct file_system_type *fs_type,
  63. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  64. {
  65. return get_sb_single(fs_type, flags, data, sysfs_fill_super, mnt);
  66. }
  67. static struct file_system_type sysfs_fs_type = {
  68. .name = "sysfs",
  69. .get_sb = sysfs_get_sb,
  70. .kill_sb = kill_anon_super,
  71. };
  72. int __init sysfs_init(void)
  73. {
  74. int err = -ENOMEM;
  75. sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
  76. sizeof(struct sysfs_dirent),
  77. 0, 0, NULL);
  78. if (!sysfs_dir_cachep)
  79. goto out;
  80. err = sysfs_inode_init();
  81. if (err)
  82. goto out_err;
  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. }
  102. #undef sysfs_get
  103. struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
  104. {
  105. return __sysfs_get(sd);
  106. }
  107. EXPORT_SYMBOL_GPL(sysfs_get);
  108. #undef sysfs_put
  109. void sysfs_put(struct sysfs_dirent *sd)
  110. {
  111. __sysfs_put(sd);
  112. }
  113. EXPORT_SYMBOL_GPL(sysfs_put);