blob: 791304fdde9d60f4e5783bc8d7ee65019e0cd996 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Becker7063fbf2005-12-15 14:29:43 -08002/* -*- 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 *
Joel Becker7063fbf2005-12-15 14:29:43 -08007 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080019
20#include <linux/configfs.h>
21#include "configfs_internal.h"
22
23/* Random magic number */
24#define CONFIGFS_MAGIC 0x62656570
25
Al Viro2a152ad2012-03-17 16:53:29 -040026static struct vfsmount *configfs_mount = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080027struct kmem_cache *configfs_dir_cachep;
Joel Becker7063fbf2005-12-15 14:29:43 -080028static int configfs_mnt_count = 0;
29
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080030static const struct super_operations configfs_ops = {
Joel Becker7063fbf2005-12-15 14:29:43 -080031 .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,
Joel Becker3d0f89b2006-01-25 13:31:07 -080052 .s_iattr = NULL,
Joel Becker7063fbf2005-12-15 14:29:43 -080053};
54
55static int configfs_fill_super(struct super_block *sb, void *data, int silent)
56{
57 struct inode *inode;
58 struct dentry *root;
59
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030060 sb->s_blocksize = PAGE_SIZE;
61 sb->s_blocksize_bits = PAGE_SHIFT;
Joel Becker7063fbf2005-12-15 14:29:43 -080062 sb->s_magic = CONFIGFS_MAGIC;
63 sb->s_op = &configfs_ops;
Joel Becker3d0f89b2006-01-25 13:31:07 -080064 sb->s_time_gran = 1;
Joel Becker7063fbf2005-12-15 14:29:43 -080065
Joel Becker3d0f89b2006-01-25 13:31:07 -080066 inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
Al Virob7c177f2012-03-17 16:24:54 -040067 &configfs_root, sb);
Joel Becker7063fbf2005-12-15 14:29:43 -080068 if (inode) {
Al Viro81d44ed12012-03-17 16:13:25 -040069 inode->i_op = &configfs_root_inode_operations;
Joel Becker7063fbf2005-12-15 14:29:43 -080070 inode->i_fop = &configfs_dir_operations;
71 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -070072 inc_nlink(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -080073 } else {
Fabian Frederick1d88aa42014-06-04 16:05:59 -070074 pr_debug("could not get root inode\n");
Joel Becker7063fbf2005-12-15 14:29:43 -080075 return -ENOMEM;
76 }
77
Al Viro48fde702012-01-08 22:15:13 -050078 root = d_make_root(inode);
Joel Becker7063fbf2005-12-15 14:29:43 -080079 if (!root) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -070080 pr_debug("%s: could not get root dentry!\n",__func__);
Joel Becker7063fbf2005-12-15 14:29:43 -080081 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;
Al Virod463a0c2011-01-12 16:41:05 -050087 sb->s_d_op = &configfs_dentry_ops; /* the rest get that */
Joel Becker7063fbf2005-12-15 14:29:43 -080088 return 0;
89}
90
Al Virofc14f2f2010-07-25 01:48:30 +040091static struct dentry *configfs_do_mount(struct file_system_type *fs_type,
92 int flags, const char *dev_name, void *data)
Joel Becker7063fbf2005-12-15 14:29:43 -080093{
Al Virofc14f2f2010-07-25 01:48:30 +040094 return mount_single(fs_type, flags, data, configfs_fill_super);
Joel Becker7063fbf2005-12-15 14:29:43 -080095}
96
97static struct file_system_type configfs_fs_type = {
98 .owner = THIS_MODULE,
99 .name = "configfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400100 .mount = configfs_do_mount,
Joel Becker7063fbf2005-12-15 14:29:43 -0800101 .kill_sb = kill_litter_super,
102};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800103MODULE_ALIAS_FS("configfs");
Joel Becker7063fbf2005-12-15 14:29:43 -0800104
Al Viro2a152ad2012-03-17 16:53:29 -0400105struct dentry *configfs_pin_fs(void)
Joel Becker7063fbf2005-12-15 14:29:43 -0800106{
Al Viro2a152ad2012-03-17 16:53:29 -0400107 int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
Joel Becker7063fbf2005-12-15 14:29:43 -0800108 &configfs_mnt_count);
Al Viro2a152ad2012-03-17 16:53:29 -0400109 return err ? ERR_PTR(err) : configfs_mount->mnt_root;
Joel Becker7063fbf2005-12-15 14:29:43 -0800110}
111
112void configfs_release_fs(void)
113{
114 simple_release_fs(&configfs_mount, &configfs_mnt_count);
115}
116
117
Joel Becker7063fbf2005-12-15 14:29:43 -0800118static int __init configfs_init(void)
119{
Joel Becker3d0f89b2006-01-25 13:31:07 -0800120 int err = -ENOMEM;
121
122 configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
123 sizeof(struct configfs_dirent),
Paul Mundt20c2df82007-07-20 10:11:58 +0900124 0, 0, NULL);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800125 if (!configfs_dir_cachep)
126 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800127
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500128 err = sysfs_create_mount_point(kernel_kobj, "config");
129 if (err)
Al Viro7c6455e2011-12-13 12:32:42 -0500130 goto out2;
Joel Becker7063fbf2005-12-15 14:29:43 -0800131
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100132 err = register_filesystem(&configfs_fs_type);
Al Viro7c6455e2011-12-13 12:32:42 -0500133 if (err)
134 goto out3;
135
Al Viro7c6455e2011-12-13 12:32:42 -0500136 return 0;
Al Viro7c6455e2011-12-13 12:32:42 -0500137out3:
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100138 pr_err("Unable to register filesystem!\n");
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500139 sysfs_remove_mount_point(kernel_kobj, "config");
Al Viro7c6455e2011-12-13 12:32:42 -0500140out2:
141 kmem_cache_destroy(configfs_dir_cachep);
142 configfs_dir_cachep = NULL;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800143out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800144 return err;
145}
146
147static void __exit configfs_exit(void)
148{
149 unregister_filesystem(&configfs_fs_type);
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500150 sysfs_remove_mount_point(kernel_kobj, "config");
Joel Becker3d0f89b2006-01-25 13:31:07 -0800151 kmem_cache_destroy(configfs_dir_cachep);
152 configfs_dir_cachep = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800153}
154
155MODULE_AUTHOR("Oracle");
156MODULE_LICENSE("GPL");
Joel Becker3d0f89b2006-01-25 13:31:07 -0800157MODULE_VERSION("0.0.2");
Joel Becker7063fbf2005-12-15 14:29:43 -0800158MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
159
Daniel Balutaf5b69772015-05-05 16:23:54 -0700160core_initcall(configfs_init);
Joel Becker7063fbf2005-12-15 14:29:43 -0800161module_exit(configfs_exit);