mount.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = generic_delete_inode,
  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. sb->s_d_op = &configfs_dentry_ops; /* the rest get that */
  82. return 0;
  83. }
  84. static int configfs_get_tree(struct fs_context *fc)
  85. {
  86. return get_tree_single(fc, configfs_fill_super);
  87. }
  88. static const struct fs_context_operations configfs_context_ops = {
  89. .get_tree = configfs_get_tree,
  90. };
  91. static int configfs_init_fs_context(struct fs_context *fc)
  92. {
  93. fc->ops = &configfs_context_ops;
  94. return 0;
  95. }
  96. static struct file_system_type configfs_fs_type = {
  97. .owner = THIS_MODULE,
  98. .name = "configfs",
  99. .init_fs_context = configfs_init_fs_context,
  100. .kill_sb = kill_litter_super,
  101. };
  102. MODULE_ALIAS_FS("configfs");
  103. struct dentry *configfs_pin_fs(void)
  104. {
  105. int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
  106. &configfs_mnt_count);
  107. return err ? ERR_PTR(err) : configfs_mount->mnt_root;
  108. }
  109. void configfs_release_fs(void)
  110. {
  111. simple_release_fs(&configfs_mount, &configfs_mnt_count);
  112. }
  113. static int __init configfs_init(void)
  114. {
  115. int err = -ENOMEM;
  116. configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
  117. sizeof(struct configfs_dirent),
  118. 0, 0, NULL);
  119. if (!configfs_dir_cachep)
  120. goto out;
  121. err = sysfs_create_mount_point(kernel_kobj, "config");
  122. if (err)
  123. goto out2;
  124. err = register_filesystem(&configfs_fs_type);
  125. if (err)
  126. goto out3;
  127. return 0;
  128. out3:
  129. pr_err("Unable to register filesystem!\n");
  130. sysfs_remove_mount_point(kernel_kobj, "config");
  131. out2:
  132. kmem_cache_destroy(configfs_dir_cachep);
  133. configfs_dir_cachep = NULL;
  134. out:
  135. return err;
  136. }
  137. static void __exit configfs_exit(void)
  138. {
  139. unregister_filesystem(&configfs_fs_type);
  140. sysfs_remove_mount_point(kernel_kobj, "config");
  141. kmem_cache_destroy(configfs_dir_cachep);
  142. configfs_dir_cachep = NULL;
  143. }
  144. MODULE_AUTHOR("Oracle");
  145. MODULE_LICENSE("GPL");
  146. MODULE_VERSION("0.0.2");
  147. MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
  148. core_initcall(configfs_init);
  149. module_exit(configfs_exit);