Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Masahiro Yamada | fa60ce2 | 2021-05-06 18:06:44 -0700 | [diff] [blame] | 2 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 3 | * mount.c - operations for initializing and mounting configfs. |
| 4 | * |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 5 | * Based on sysfs: |
| 6 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel |
| 7 | * |
| 8 | * configfs Copyright (C) 2005 Oracle. All rights reserved. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mount.h> |
David Howells | 6bc62f2 | 2019-03-25 16:38:29 +0000 | [diff] [blame] | 14 | #include <linux/fs_context.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 18 | |
| 19 | #include <linux/configfs.h> |
| 20 | #include "configfs_internal.h" |
| 21 | |
| 22 | /* Random magic number */ |
| 23 | #define CONFIGFS_MAGIC 0x62656570 |
| 24 | |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 25 | static struct vfsmount *configfs_mount = NULL; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 26 | struct kmem_cache *configfs_dir_cachep; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 27 | static int configfs_mnt_count = 0; |
| 28 | |
Al Viro | e9c03af | 2019-09-11 09:00:01 +0200 | [diff] [blame] | 29 | |
| 30 | static void configfs_free_inode(struct inode *inode) |
| 31 | { |
| 32 | if (S_ISLNK(inode->i_mode)) |
| 33 | kfree(inode->i_link); |
| 34 | free_inode_nonrcu(inode); |
| 35 | } |
| 36 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 37 | static const struct super_operations configfs_ops = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 38 | .statfs = simple_statfs, |
| 39 | .drop_inode = generic_delete_inode, |
Al Viro | e9c03af | 2019-09-11 09:00:01 +0200 | [diff] [blame] | 40 | .free_inode = configfs_free_inode, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static struct config_group configfs_root_group = { |
| 44 | .cg_item = { |
| 45 | .ci_namebuf = "root", |
| 46 | .ci_name = configfs_root_group.cg_item.ci_namebuf, |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | int configfs_is_root(struct config_item *item) |
| 51 | { |
| 52 | return item == &configfs_root_group.cg_item; |
| 53 | } |
| 54 | |
| 55 | static struct configfs_dirent configfs_root = { |
| 56 | .s_sibling = LIST_HEAD_INIT(configfs_root.s_sibling), |
| 57 | .s_children = LIST_HEAD_INIT(configfs_root.s_children), |
| 58 | .s_element = &configfs_root_group.cg_item, |
| 59 | .s_type = CONFIGFS_ROOT, |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 60 | .s_iattr = NULL, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
David Howells | 6bc62f2 | 2019-03-25 16:38:29 +0000 | [diff] [blame] | 63 | static int configfs_fill_super(struct super_block *sb, struct fs_context *fc) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 64 | { |
| 65 | struct inode *inode; |
| 66 | struct dentry *root; |
| 67 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 68 | sb->s_blocksize = PAGE_SIZE; |
| 69 | sb->s_blocksize_bits = PAGE_SHIFT; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 70 | sb->s_magic = CONFIGFS_MAGIC; |
| 71 | sb->s_op = &configfs_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 72 | sb->s_time_gran = 1; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 73 | |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 74 | inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, |
Al Viro | b7c177f | 2012-03-17 16:24:54 -0400 | [diff] [blame] | 75 | &configfs_root, sb); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 76 | if (inode) { |
Al Viro | 81d44ed1 | 2012-03-17 16:13:25 -0400 | [diff] [blame] | 77 | inode->i_op = &configfs_root_inode_operations; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 78 | inode->i_fop = &configfs_dir_operations; |
| 79 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 80 | inc_nlink(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 81 | } else { |
Fabian Frederick | 1d88aa4 | 2014-06-04 16:05:59 -0700 | [diff] [blame] | 82 | pr_debug("could not get root inode\n"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 86 | root = d_make_root(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 87 | if (!root) { |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 88 | pr_debug("%s: could not get root dentry!\n",__func__); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 89 | return -ENOMEM; |
| 90 | } |
| 91 | config_group_init(&configfs_root_group); |
| 92 | configfs_root_group.cg_item.ci_dentry = root; |
| 93 | root->d_fsdata = &configfs_root; |
| 94 | sb->s_root = root; |
Al Viro | d463a0c | 2011-01-12 16:41:05 -0500 | [diff] [blame] | 95 | sb->s_d_op = &configfs_dentry_ops; /* the rest get that */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
David Howells | 6bc62f2 | 2019-03-25 16:38:29 +0000 | [diff] [blame] | 99 | static int configfs_get_tree(struct fs_context *fc) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 100 | { |
David Howells | 6bc62f2 | 2019-03-25 16:38:29 +0000 | [diff] [blame] | 101 | return get_tree_single(fc, configfs_fill_super); |
| 102 | } |
| 103 | |
| 104 | static const struct fs_context_operations configfs_context_ops = { |
| 105 | .get_tree = configfs_get_tree, |
| 106 | }; |
| 107 | |
| 108 | static int configfs_init_fs_context(struct fs_context *fc) |
| 109 | { |
| 110 | fc->ops = &configfs_context_ops; |
| 111 | return 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static struct file_system_type configfs_fs_type = { |
| 115 | .owner = THIS_MODULE, |
| 116 | .name = "configfs", |
David Howells | 6bc62f2 | 2019-03-25 16:38:29 +0000 | [diff] [blame] | 117 | .init_fs_context = configfs_init_fs_context, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 118 | .kill_sb = kill_litter_super, |
| 119 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 120 | MODULE_ALIAS_FS("configfs"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 121 | |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 122 | struct dentry *configfs_pin_fs(void) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 123 | { |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 124 | int err = simple_pin_fs(&configfs_fs_type, &configfs_mount, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 125 | &configfs_mnt_count); |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 126 | return err ? ERR_PTR(err) : configfs_mount->mnt_root; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void configfs_release_fs(void) |
| 130 | { |
| 131 | simple_release_fs(&configfs_mount, &configfs_mnt_count); |
| 132 | } |
| 133 | |
| 134 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 135 | static int __init configfs_init(void) |
| 136 | { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 137 | int err = -ENOMEM; |
| 138 | |
| 139 | configfs_dir_cachep = kmem_cache_create("configfs_dir_cache", |
| 140 | sizeof(struct configfs_dirent), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 141 | 0, 0, NULL); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 142 | if (!configfs_dir_cachep) |
| 143 | goto out; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 144 | |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 145 | err = sysfs_create_mount_point(kernel_kobj, "config"); |
| 146 | if (err) |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 147 | goto out2; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 148 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 149 | err = register_filesystem(&configfs_fs_type); |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 150 | if (err) |
| 151 | goto out3; |
| 152 | |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 153 | return 0; |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 154 | out3: |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 155 | pr_err("Unable to register filesystem!\n"); |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 156 | sysfs_remove_mount_point(kernel_kobj, "config"); |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 157 | out2: |
| 158 | kmem_cache_destroy(configfs_dir_cachep); |
| 159 | configfs_dir_cachep = NULL; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 160 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 161 | return err; |
| 162 | } |
| 163 | |
| 164 | static void __exit configfs_exit(void) |
| 165 | { |
| 166 | unregister_filesystem(&configfs_fs_type); |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 167 | sysfs_remove_mount_point(kernel_kobj, "config"); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 168 | kmem_cache_destroy(configfs_dir_cachep); |
| 169 | configfs_dir_cachep = NULL; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | MODULE_AUTHOR("Oracle"); |
| 173 | MODULE_LICENSE("GPL"); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 174 | MODULE_VERSION("0.0.2"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 175 | MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration."); |
| 176 | |
Daniel Baluta | f5b6977 | 2015-05-05 16:23:54 -0700 | [diff] [blame] | 177 | core_initcall(configfs_init); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 178 | module_exit(configfs_exit); |