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: 4 * 5 * mount.c - operations for initializing and mounting configfs. 6 * 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> 18 #include <linux/slab.h> 19 20 #include <linux/configfs.h> 21 #include "configfs_internal.h" 22 23 /* Random magic number */ 24 #define CONFIGFS_MAGIC 0x62656570 25 26 static struct vfsmount *configfs_mount = NULL; 27 struct kmem_cache *configfs_dir_cachep; 28 static int configfs_mnt_count = 0; 29 30 static const struct super_operations configfs_ops = { 31 .statfs = simple_statfs, 32 .drop_inode = generic_delete_inode, 33 }; 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 42 int configfs_is_root(struct config_item *item) 43 { 44 return item == &configfs_root_group.cg_item; 45 } 46 47 static 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, 52 .s_iattr = NULL, 53 }; 54 55 static int configfs_fill_super(struct super_block *sb, void *data, int silent) 56 { 57 struct inode *inode; 58 struct dentry *root; 59 60 sb->s_blocksize = PAGE_SIZE; 61 sb->s_blocksize_bits = PAGE_SHIFT; 62 sb->s_magic = CONFIGFS_MAGIC; 63 sb->s_op = &configfs_ops; 64 sb->s_time_gran = 1; 65 66 inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, 67 &configfs_root, sb); 68 if (inode) { 69 inode->i_op = &configfs_root_inode_operations; 70 inode->i_fop = &configfs_dir_operations; 71 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 72 inc_nlink(inode); 73 } else { 74 pr_debug("could not get root inode\n"); 75 return -ENOMEM; 76 } 77 78 root = d_make_root(inode); 79 if (!root) { 80 pr_debug("%s: could not get root dentry!\n",__func__); 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; 87 sb->s_d_op = &configfs_dentry_ops; /* the rest get that */ 88 return 0; 89 } 90 91 static struct dentry *configfs_do_mount(struct file_system_type *fs_type, 92 int flags, const char *dev_name, void *data) 93 { 94 return mount_single(fs_type, flags, data, configfs_fill_super); 95 } 96 97 static struct file_system_type configfs_fs_type = { 98 .owner = THIS_MODULE, 99 .name = "configfs", 100 .mount = configfs_do_mount, 101 .kill_sb = kill_litter_super, 102 }; 103 MODULE_ALIAS_FS("configfs"); 104 105 struct dentry *configfs_pin_fs(void) 106 { 107 int err = simple_pin_fs(&configfs_fs_type, &configfs_mount, 108 &configfs_mnt_count); 109 return err ? ERR_PTR(err) : configfs_mount->mnt_root; 110 } 111 112 void configfs_release_fs(void) 113 { 114 simple_release_fs(&configfs_mount, &configfs_mnt_count); 115 } 116 117 118 static int __init configfs_init(void) 119 { 120 int err = -ENOMEM; 121 122 configfs_dir_cachep = kmem_cache_create("configfs_dir_cache", 123 sizeof(struct configfs_dirent), 124 0, 0, NULL); 125 if (!configfs_dir_cachep) 126 goto out; 127 128 err = sysfs_create_mount_point(kernel_kobj, "config"); 129 if (err) 130 goto out2; 131 132 err = register_filesystem(&configfs_fs_type); 133 if (err) 134 goto out3; 135 136 return 0; 137 out3: 138 pr_err("Unable to register filesystem!\n"); 139 sysfs_remove_mount_point(kernel_kobj, "config"); 140 out2: 141 kmem_cache_destroy(configfs_dir_cachep); 142 configfs_dir_cachep = NULL; 143 out: 144 return err; 145 } 146 147 static void __exit configfs_exit(void) 148 { 149 unregister_filesystem(&configfs_fs_type); 150 sysfs_remove_mount_point(kernel_kobj, "config"); 151 kmem_cache_destroy(configfs_dir_cachep); 152 configfs_dir_cachep = NULL; 153 } 154 155 MODULE_AUTHOR("Oracle"); 156 MODULE_LICENSE("GPL"); 157 MODULE_VERSION("0.0.2"); 158 MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration."); 159 160 core_initcall(configfs_init); 161 module_exit(configfs_exit); 162