treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 145
[linux-block.git] / fs / configfs / mount.c
CommitLineData
328970de 1// SPDX-License-Identifier: GPL-2.0-or-later
7063fbf2
JB
2/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * mount.c - operations for initializing and mounting configfs.
6 *
7063fbf2
JB
7 * Based on sysfs:
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9 *
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
11 */
12
13#include <linux/fs.h>
14#include <linux/module.h>
15#include <linux/mount.h>
16#include <linux/pagemap.h>
17#include <linux/init.h>
5a0e3ad6 18#include <linux/slab.h>
7063fbf2
JB
19
20#include <linux/configfs.h>
21#include "configfs_internal.h"
22
23/* Random magic number */
24#define CONFIGFS_MAGIC 0x62656570
25
2a152ad3 26static struct vfsmount *configfs_mount = NULL;
e18b890b 27struct kmem_cache *configfs_dir_cachep;
7063fbf2
JB
28static int configfs_mnt_count = 0;
29
ee9b6d61 30static const struct super_operations configfs_ops = {
7063fbf2
JB
31 .statfs = simple_statfs,
32 .drop_inode = generic_delete_inode,
33};
34
35static 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
42int configfs_is_root(struct config_item *item)
43{
44 return item == &configfs_root_group.cg_item;
45}
46
47static struct configfs_dirent configfs_root = {
48 .s_sibling = LIST_HEAD_INIT(configfs_root.s_sibling),
49 .s_children = LIST_HEAD_INIT(configfs_root.s_children),
50 .s_element = &configfs_root_group.cg_item,
51 .s_type = CONFIGFS_ROOT,
3d0f89bb 52 .s_iattr = NULL,
7063fbf2
JB
53};
54
55static int configfs_fill_super(struct super_block *sb, void *data, int silent)
56{
57 struct inode *inode;
58 struct dentry *root;
59
09cbfeaf
KS
60 sb->s_blocksize = PAGE_SIZE;
61 sb->s_blocksize_bits = PAGE_SHIFT;
7063fbf2
JB
62 sb->s_magic = CONFIGFS_MAGIC;
63 sb->s_op = &configfs_ops;
3d0f89bb 64 sb->s_time_gran = 1;
7063fbf2 65
3d0f89bb 66 inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
b7c177fc 67 &configfs_root, sb);
7063fbf2 68 if (inode) {
81d44ed1 69 inode->i_op = &configfs_root_inode_operations;
7063fbf2
JB
70 inode->i_fop = &configfs_dir_operations;
71 /* directory inodes start off with i_nlink == 2 (for "." entry) */
d8c76e6f 72 inc_nlink(inode);
7063fbf2 73 } else {
1d88aa44 74 pr_debug("could not get root inode\n");
7063fbf2
JB
75 return -ENOMEM;
76 }
77
48fde701 78 root = d_make_root(inode);
7063fbf2 79 if (!root) {
8e24eea7 80 pr_debug("%s: could not get root dentry!\n",__func__);
7063fbf2
JB
81 return -ENOMEM;
82 }
83 config_group_init(&configfs_root_group);
84 configfs_root_group.cg_item.ci_dentry = root;
85 root->d_fsdata = &configfs_root;
86 sb->s_root = root;
d463a0c4 87 sb->s_d_op = &configfs_dentry_ops; /* the rest get that */
7063fbf2
JB
88 return 0;
89}
90
fc14f2fe
AV
91static struct dentry *configfs_do_mount(struct file_system_type *fs_type,
92 int flags, const char *dev_name, void *data)
7063fbf2 93{
fc14f2fe 94 return mount_single(fs_type, flags, data, configfs_fill_super);
7063fbf2
JB
95}
96
97static struct file_system_type configfs_fs_type = {
98 .owner = THIS_MODULE,
99 .name = "configfs",
fc14f2fe 100 .mount = configfs_do_mount,
7063fbf2
JB
101 .kill_sb = kill_litter_super,
102};
7f78e035 103MODULE_ALIAS_FS("configfs");
7063fbf2 104
2a152ad3 105struct dentry *configfs_pin_fs(void)
7063fbf2 106{
2a152ad3 107 int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
7063fbf2 108 &configfs_mnt_count);
2a152ad3 109 return err ? ERR_PTR(err) : configfs_mount->mnt_root;
7063fbf2
JB
110}
111
112void configfs_release_fs(void)
113{
114 simple_release_fs(&configfs_mount, &configfs_mnt_count);
115}
116
117
7063fbf2
JB
118static int __init configfs_init(void)
119{
3d0f89bb
JB
120 int err = -ENOMEM;
121
122 configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
123 sizeof(struct configfs_dirent),
20c2df83 124 0, 0, NULL);
3d0f89bb
JB
125 if (!configfs_dir_cachep)
126 goto out;
7063fbf2 127
f9bb4882
EB
128 err = sysfs_create_mount_point(kernel_kobj, "config");
129 if (err)
7c6455e3
AV
130 goto out2;
131
7063fbf2 132 err = register_filesystem(&configfs_fs_type);
7c6455e3 133 if (err)
b4caecd4 134 goto out3;
7063fbf2 135
7c6455e3 136 return 0;
7c6455e3 137out3:
b4caecd4 138 pr_err("Unable to register filesystem!\n");
f9bb4882 139 sysfs_remove_mount_point(kernel_kobj, "config");
7c6455e3
AV
140out2:
141 kmem_cache_destroy(configfs_dir_cachep);
142 configfs_dir_cachep = NULL;
3d0f89bb 143out:
7063fbf2
JB
144 return err;
145}
146
147static void __exit configfs_exit(void)
148{
149 unregister_filesystem(&configfs_fs_type);
f9bb4882 150 sysfs_remove_mount_point(kernel_kobj, "config");
3d0f89bb
JB
151 kmem_cache_destroy(configfs_dir_cachep);
152 configfs_dir_cachep = NULL;
7063fbf2
JB
153}
154
155MODULE_AUTHOR("Oracle");
156MODULE_LICENSE("GPL");
3d0f89bb 157MODULE_VERSION("0.0.2");
7063fbf2
JB
158MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
159
f5b69770 160core_initcall(configfs_init);
7063fbf2 161module_exit(configfs_exit);