1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
5 * mount.c - operations for initializing and mounting configfs.
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
14 #include <linux/module.h>
15 #include <linux/mount.h>
16 #include <linux/fs_context.h>
17 #include <linux/pagemap.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
21 #include <linux/configfs.h>
22 #include "configfs_internal.h"
24 /* Random magic number */
25 #define CONFIGFS_MAGIC 0x62656570
27 static struct vfsmount *configfs_mount = NULL;
28 struct kmem_cache *configfs_dir_cachep;
29 static int configfs_mnt_count = 0;
31 static const struct super_operations configfs_ops = {
32 .statfs = simple_statfs,
33 .drop_inode = generic_delete_inode,
36 static struct config_group configfs_root_group = {
39 .ci_name = configfs_root_group.cg_item.ci_namebuf,
43 int configfs_is_root(struct config_item *item)
45 return item == &configfs_root_group.cg_item;
48 static struct configfs_dirent configfs_root = {
49 .s_sibling = LIST_HEAD_INIT(configfs_root.s_sibling),
50 .s_children = LIST_HEAD_INIT(configfs_root.s_children),
51 .s_element = &configfs_root_group.cg_item,
52 .s_type = CONFIGFS_ROOT,
56 static int configfs_fill_super(struct super_block *sb, struct fs_context *fc)
61 sb->s_blocksize = PAGE_SIZE;
62 sb->s_blocksize_bits = PAGE_SHIFT;
63 sb->s_magic = CONFIGFS_MAGIC;
64 sb->s_op = &configfs_ops;
67 inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
70 inode->i_op = &configfs_root_inode_operations;
71 inode->i_fop = &configfs_dir_operations;
72 /* directory inodes start off with i_nlink == 2 (for "." entry) */
75 pr_debug("could not get root inode\n");
79 root = d_make_root(inode);
81 pr_debug("%s: could not get root dentry!\n",__func__);
84 config_group_init(&configfs_root_group);
85 configfs_root_group.cg_item.ci_dentry = root;
86 root->d_fsdata = &configfs_root;
88 sb->s_d_op = &configfs_dentry_ops; /* the rest get that */
92 static int configfs_get_tree(struct fs_context *fc)
94 return get_tree_single(fc, configfs_fill_super);
97 static const struct fs_context_operations configfs_context_ops = {
98 .get_tree = configfs_get_tree,
101 static int configfs_init_fs_context(struct fs_context *fc)
103 fc->ops = &configfs_context_ops;
107 static struct file_system_type configfs_fs_type = {
108 .owner = THIS_MODULE,
110 .init_fs_context = configfs_init_fs_context,
111 .kill_sb = kill_litter_super,
113 MODULE_ALIAS_FS("configfs");
115 struct dentry *configfs_pin_fs(void)
117 int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
118 &configfs_mnt_count);
119 return err ? ERR_PTR(err) : configfs_mount->mnt_root;
122 void configfs_release_fs(void)
124 simple_release_fs(&configfs_mount, &configfs_mnt_count);
128 static int __init configfs_init(void)
132 configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
133 sizeof(struct configfs_dirent),
135 if (!configfs_dir_cachep)
138 err = sysfs_create_mount_point(kernel_kobj, "config");
142 err = register_filesystem(&configfs_fs_type);
148 pr_err("Unable to register filesystem!\n");
149 sysfs_remove_mount_point(kernel_kobj, "config");
151 kmem_cache_destroy(configfs_dir_cachep);
152 configfs_dir_cachep = NULL;
157 static void __exit configfs_exit(void)
159 unregister_filesystem(&configfs_fs_type);
160 sysfs_remove_mount_point(kernel_kobj, "config");
161 kmem_cache_destroy(configfs_dir_cachep);
162 configfs_dir_cachep = NULL;
165 MODULE_AUTHOR("Oracle");
166 MODULE_LICENSE("GPL");
167 MODULE_VERSION("0.0.2");
168 MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
170 core_initcall(configfs_init);
171 module_exit(configfs_exit);