xref: /openbmc/linux/fs/configfs/mount.c (revision fa60ce2c)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2*fa60ce2cSMasahiro Yamada /*
37063fbf2SJoel Becker  * mount.c - operations for initializing and mounting configfs.
47063fbf2SJoel Becker  *
57063fbf2SJoel Becker  * Based on sysfs:
67063fbf2SJoel Becker  * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
77063fbf2SJoel Becker  *
87063fbf2SJoel Becker  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
97063fbf2SJoel Becker  */
107063fbf2SJoel Becker 
117063fbf2SJoel Becker #include <linux/fs.h>
127063fbf2SJoel Becker #include <linux/module.h>
137063fbf2SJoel Becker #include <linux/mount.h>
146bc62f20SDavid Howells #include <linux/fs_context.h>
157063fbf2SJoel Becker #include <linux/pagemap.h>
167063fbf2SJoel Becker #include <linux/init.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
187063fbf2SJoel Becker 
197063fbf2SJoel Becker #include <linux/configfs.h>
207063fbf2SJoel Becker #include "configfs_internal.h"
217063fbf2SJoel Becker 
227063fbf2SJoel Becker /* Random magic number */
237063fbf2SJoel Becker #define CONFIGFS_MAGIC 0x62656570
247063fbf2SJoel Becker 
252a152ad3SAl Viro static struct vfsmount *configfs_mount = NULL;
26e18b890bSChristoph Lameter struct kmem_cache *configfs_dir_cachep;
277063fbf2SJoel Becker static int configfs_mnt_count = 0;
287063fbf2SJoel Becker 
29e9c03af2SAl Viro 
configfs_free_inode(struct inode * inode)30e9c03af2SAl Viro static void configfs_free_inode(struct inode *inode)
31e9c03af2SAl Viro {
32e9c03af2SAl Viro 	if (S_ISLNK(inode->i_mode))
33e9c03af2SAl Viro 		kfree(inode->i_link);
34e9c03af2SAl Viro 	free_inode_nonrcu(inode);
35e9c03af2SAl Viro }
36e9c03af2SAl Viro 
37ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations configfs_ops = {
387063fbf2SJoel Becker 	.statfs		= simple_statfs,
397063fbf2SJoel Becker 	.drop_inode	= generic_delete_inode,
40e9c03af2SAl Viro 	.free_inode	= configfs_free_inode,
417063fbf2SJoel Becker };
427063fbf2SJoel Becker 
437063fbf2SJoel Becker static struct config_group configfs_root_group = {
447063fbf2SJoel Becker 	.cg_item = {
457063fbf2SJoel Becker 		.ci_namebuf	= "root",
467063fbf2SJoel Becker 		.ci_name	= configfs_root_group.cg_item.ci_namebuf,
477063fbf2SJoel Becker 	},
487063fbf2SJoel Becker };
497063fbf2SJoel Becker 
configfs_is_root(struct config_item * item)507063fbf2SJoel Becker int configfs_is_root(struct config_item *item)
517063fbf2SJoel Becker {
527063fbf2SJoel Becker 	return item == &configfs_root_group.cg_item;
537063fbf2SJoel Becker }
547063fbf2SJoel Becker 
557063fbf2SJoel Becker static struct configfs_dirent configfs_root = {
567063fbf2SJoel Becker 	.s_sibling	= LIST_HEAD_INIT(configfs_root.s_sibling),
577063fbf2SJoel Becker 	.s_children	= LIST_HEAD_INIT(configfs_root.s_children),
587063fbf2SJoel Becker 	.s_element	= &configfs_root_group.cg_item,
597063fbf2SJoel Becker 	.s_type		= CONFIGFS_ROOT,
603d0f89bbSJoel Becker 	.s_iattr	= NULL,
617063fbf2SJoel Becker };
627063fbf2SJoel Becker 
configfs_fill_super(struct super_block * sb,struct fs_context * fc)636bc62f20SDavid Howells static int configfs_fill_super(struct super_block *sb, struct fs_context *fc)
647063fbf2SJoel Becker {
657063fbf2SJoel Becker 	struct inode *inode;
667063fbf2SJoel Becker 	struct dentry *root;
677063fbf2SJoel Becker 
6809cbfeafSKirill A. Shutemov 	sb->s_blocksize = PAGE_SIZE;
6909cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
707063fbf2SJoel Becker 	sb->s_magic = CONFIGFS_MAGIC;
717063fbf2SJoel Becker 	sb->s_op = &configfs_ops;
723d0f89bbSJoel Becker 	sb->s_time_gran = 1;
737063fbf2SJoel Becker 
743d0f89bbSJoel Becker 	inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
75b7c177fcSAl Viro 				   &configfs_root, sb);
767063fbf2SJoel Becker 	if (inode) {
7781d44ed1SAl Viro 		inode->i_op = &configfs_root_inode_operations;
787063fbf2SJoel Becker 		inode->i_fop = &configfs_dir_operations;
797063fbf2SJoel Becker 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
80d8c76e6fSDave Hansen 		inc_nlink(inode);
817063fbf2SJoel Becker 	} else {
821d88aa44SFabian Frederick 		pr_debug("could not get root inode\n");
837063fbf2SJoel Becker 		return -ENOMEM;
847063fbf2SJoel Becker 	}
857063fbf2SJoel Becker 
8648fde701SAl Viro 	root = d_make_root(inode);
877063fbf2SJoel Becker 	if (!root) {
888e24eea7SHarvey Harrison 		pr_debug("%s: could not get root dentry!\n",__func__);
897063fbf2SJoel Becker 		return -ENOMEM;
907063fbf2SJoel Becker 	}
917063fbf2SJoel Becker 	config_group_init(&configfs_root_group);
927063fbf2SJoel Becker 	configfs_root_group.cg_item.ci_dentry = root;
937063fbf2SJoel Becker 	root->d_fsdata = &configfs_root;
947063fbf2SJoel Becker 	sb->s_root = root;
95d463a0c4SAl Viro 	sb->s_d_op = &configfs_dentry_ops; /* the rest get that */
967063fbf2SJoel Becker 	return 0;
977063fbf2SJoel Becker }
987063fbf2SJoel Becker 
configfs_get_tree(struct fs_context * fc)996bc62f20SDavid Howells static int configfs_get_tree(struct fs_context *fc)
1007063fbf2SJoel Becker {
1016bc62f20SDavid Howells 	return get_tree_single(fc, configfs_fill_super);
1026bc62f20SDavid Howells }
1036bc62f20SDavid Howells 
1046bc62f20SDavid Howells static const struct fs_context_operations configfs_context_ops = {
1056bc62f20SDavid Howells 	.get_tree	= configfs_get_tree,
1066bc62f20SDavid Howells };
1076bc62f20SDavid Howells 
configfs_init_fs_context(struct fs_context * fc)1086bc62f20SDavid Howells static int configfs_init_fs_context(struct fs_context *fc)
1096bc62f20SDavid Howells {
1106bc62f20SDavid Howells 	fc->ops = &configfs_context_ops;
1116bc62f20SDavid Howells 	return 0;
1127063fbf2SJoel Becker }
1137063fbf2SJoel Becker 
1147063fbf2SJoel Becker static struct file_system_type configfs_fs_type = {
1157063fbf2SJoel Becker 	.owner		= THIS_MODULE,
1167063fbf2SJoel Becker 	.name		= "configfs",
1176bc62f20SDavid Howells 	.init_fs_context = configfs_init_fs_context,
1187063fbf2SJoel Becker 	.kill_sb	= kill_litter_super,
1197063fbf2SJoel Becker };
1207f78e035SEric W. Biederman MODULE_ALIAS_FS("configfs");
1217063fbf2SJoel Becker 
configfs_pin_fs(void)1222a152ad3SAl Viro struct dentry *configfs_pin_fs(void)
1237063fbf2SJoel Becker {
1242a152ad3SAl Viro 	int err = simple_pin_fs(&configfs_fs_type, &configfs_mount,
1257063fbf2SJoel Becker 			     &configfs_mnt_count);
1262a152ad3SAl Viro 	return err ? ERR_PTR(err) : configfs_mount->mnt_root;
1277063fbf2SJoel Becker }
1287063fbf2SJoel Becker 
configfs_release_fs(void)1297063fbf2SJoel Becker void configfs_release_fs(void)
1307063fbf2SJoel Becker {
1317063fbf2SJoel Becker 	simple_release_fs(&configfs_mount, &configfs_mnt_count);
1327063fbf2SJoel Becker }
1337063fbf2SJoel Becker 
1347063fbf2SJoel Becker 
configfs_init(void)1357063fbf2SJoel Becker static int __init configfs_init(void)
1367063fbf2SJoel Becker {
1373d0f89bbSJoel Becker 	int err = -ENOMEM;
1383d0f89bbSJoel Becker 
1393d0f89bbSJoel Becker 	configfs_dir_cachep = kmem_cache_create("configfs_dir_cache",
1403d0f89bbSJoel Becker 						sizeof(struct configfs_dirent),
14120c2df83SPaul Mundt 						0, 0, NULL);
1423d0f89bbSJoel Becker 	if (!configfs_dir_cachep)
1433d0f89bbSJoel Becker 		goto out;
1447063fbf2SJoel Becker 
145f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(kernel_kobj, "config");
146f9bb4882SEric W. Biederman 	if (err)
1477c6455e3SAl Viro 		goto out2;
1487063fbf2SJoel Becker 
149b4caecd4SChristoph Hellwig 	err = register_filesystem(&configfs_fs_type);
1507c6455e3SAl Viro 	if (err)
1517c6455e3SAl Viro 		goto out3;
1527c6455e3SAl Viro 
1537c6455e3SAl Viro 	return 0;
1547c6455e3SAl Viro out3:
155b4caecd4SChristoph Hellwig 	pr_err("Unable to register filesystem!\n");
156f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(kernel_kobj, "config");
1577c6455e3SAl Viro out2:
158e0bf68ddSPeter Zijlstra 	kmem_cache_destroy(configfs_dir_cachep);
159e0bf68ddSPeter Zijlstra 	configfs_dir_cachep = NULL;
1603d0f89bbSJoel Becker out:
1617063fbf2SJoel Becker 	return err;
1627063fbf2SJoel Becker }
1637063fbf2SJoel Becker 
configfs_exit(void)1647063fbf2SJoel Becker static void __exit configfs_exit(void)
1657063fbf2SJoel Becker {
1667063fbf2SJoel Becker 	unregister_filesystem(&configfs_fs_type);
167f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(kernel_kobj, "config");
1683d0f89bbSJoel Becker 	kmem_cache_destroy(configfs_dir_cachep);
1693d0f89bbSJoel Becker 	configfs_dir_cachep = NULL;
1707063fbf2SJoel Becker }
1717063fbf2SJoel Becker 
1727063fbf2SJoel Becker MODULE_AUTHOR("Oracle");
1737063fbf2SJoel Becker MODULE_LICENSE("GPL");
1743d0f89bbSJoel Becker MODULE_VERSION("0.0.2");
1757063fbf2SJoel Becker MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration.");
1767063fbf2SJoel Becker 
177f5b69770SDaniel Baluta core_initcall(configfs_init);
1787063fbf2SJoel Becker module_exit(configfs_exit);
179