mount.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * mount.c - operations for initializing and mounting configfs.
  4. *
  5. * Based on sysfs:
  6. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  7. *
  8. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/module.h>
  12. #include <linux/mount.h>
  13. #include <linux/fs_context.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/configfs.h>
  18. #include "configfs_internal.h"
  19. /* Random magic number */
  20. #define CONFIGFS_MAGIC 0x62656570
  21. static struct vfsmount *configfs_mount = NULL;
  22. struct kmem_cache *configfs_dir_cachep;
  23. static int configfs_mnt_count = 0;
  24. static void configfs_free_inode(struct inode *inode)
  25. {
  26. if (S_ISLNK(inode->i_mode))
  27. kfree(inode->i_link);
  28. free_inode_nonrcu(inode);
  29. }
  30. static const struct super_operations configfs_ops = {
  31. .statfs = simple_statfs,
  32. .drop_inode = inode_just_drop,
  33. .free_inode = configfs_free_inode,
  34. };
  35. static struct config_group configfs_root_group = {
  36. .cg_item = {
  37. .ci_namebuf = "root",
  38. .ci_name = configfs_root_group.cg_item.ci_namebuf,
  39. },
  40. };
  41. int configfs_is_root(struct config_item *item)
  42. {
  43. return item == &configfs_root_group.cg_item;
  44. }
  45. static struct configfs_dirent configfs_root = {
  46. .s_sibling = LIST_HEAD_INIT(configfs_root.s_sibling),
  47. .s_children = LIST_HEAD_INIT(configfs_root.s_children),
  48. .s_element = &configfs_root_group.cg_item,
  49. .s_type = CONFIGFS_ROOT,
  50. .s_iattr = NULL,
  51. };
  52. static int configfs_fill_super(struct super_block *sb, struct fs_context *fc)
  53. {
  54. struct inode *inode;
  55. struct dentry *root;
  56. sb->s_blocksize = PAGE_SIZE;
  57. sb->s_blocksize_bits = PAGE_SHIFT;
  58. sb->s_magic = CONFIGFS_MAGIC;
  59. sb->s_op = &configfs_ops;
  60. sb->s_time_gran = 1;
  61. inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  62. &configfs_root, sb);
  63. if (inode) {
  64. inode->i_op = &configfs_root_inode_operations;
  65. inode->i_fop = &configfs_dir_operations;
  66. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  67. inc_nlink(inode);
  68. } else {
  69. pr_debug("could not get root inode\n");
  70. return -ENOMEM;
  71. }
  72. root = d_make_root(inode);
  73. if (!root) {
  74. pr_debug("%s: could not get root dentry!\n",__func__);
  75. return -ENOMEM;
  76. }
  77. config_group_init(&configfs_root_group);
  78. configfs_root_group.cg_item.ci_dentry = root;
  79. root->d_fsdata = &configfs_root;
  80. sb->s_root = root;
  81. set_default_d_op(sb, &configfs_dentry_ops); /* the rest get that */
  82. sb->s_d_flags |= DCACHE_DONTCACHE;
  83. return 0;
  84. }
  85. static int configfs_get_tree(struct fs_context *fc)
  86. {
  87. return get_tree_single(fc, configfs_fill_super);
  88. }
  89. static const struct fs_context_operations configfs_context_ops = {
  90. .get_tree = configfs_get_tree,
  91. };
  92. static int configfs_init_fs_context(struct fs_context *fc)
  93. {
  94. fc->ops = &configfs_context_ops;
  95. return 0;
  96. }
  97. static struct file_system_type configfs_fs_type = {
  98. .owner = THIS_MODULE,
  99. .name = "configfs",
  100. .init_fs_context = configfs_init_fs_context,
  101. .kill_sb = kill_anon_super,
  102. };
  103. MODULE_ALIAS_FS("configfs");
  104. struct dentry *configfs_pin_fs(void)
  105. {
  106. int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
  107. &configfs_mnt_count);
  108. return err ? ERR_PTR(err) : configfs_mount->mnt_root;
  109. }
  110. void configfs_release_fs(void)
  111. {
  112. simple_release_fs(&configfs_mount, &configfs_mnt_count);
  113. }
  114. static int __init configfs_init(void)
  115. {
  116. int err = -ENOMEM;
  117. configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
  118. sizeof(struct configfs_dirent),
  119. 0, 0, NULL);
  120. if (!configfs_dir_cachep)
  121. goto out;
  122. err = sysfs_create_mount_point(kernel_kobj, "config");
  123. if (err)
  124. goto out2;
  125. err = register_filesystem(&configfs_fs_type);
  126. if (err)
  127. goto out3;
  128. return 0;
  129. out3:
  130. pr_err("Unable to register filesystem!\n");
  131. sysfs_remove_mount_point(kernel_kobj, "config");
  132. out2:
  133. kmem_cache_destroy(configfs_dir_cachep);
  134. configfs_dir_cachep = NULL;
  135. out:
  136. return err;
  137. }
  138. static void __exit configfs_exit(void)
  139. {
  140. unregister_filesystem(&configfs_fs_type);
  141. sysfs_remove_mount_point(kernel_kobj, "config");
  142. kmem_cache_destroy(configfs_dir_cachep);
  143. configfs_dir_cachep = NULL;
  144. }
  145. MODULE_AUTHOR("Oracle");
  146. MODULE_LICENSE("GPL");
  147. MODULE_VERSION("0.0.2");
  148. MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
  149. core_initcall(configfs_init);
  150. module_exit(configfs_exit);