Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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 | * |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <linux/configfs.h> |
| 21 | #include "configfs_internal.h" |
| 22 | |
| 23 | /* Random magic number */ |
| 24 | #define CONFIGFS_MAGIC 0x62656570 |
| 25 | |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 26 | static struct vfsmount *configfs_mount = NULL; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 27 | struct kmem_cache *configfs_dir_cachep; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 28 | static int configfs_mnt_count = 0; |
| 29 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 30 | static const struct super_operations configfs_ops = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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, |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 52 | .s_iattr = NULL, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 60 | sb->s_blocksize = PAGE_SIZE; |
| 61 | sb->s_blocksize_bits = PAGE_SHIFT; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 62 | sb->s_magic = CONFIGFS_MAGIC; |
| 63 | sb->s_op = &configfs_ops; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 64 | sb->s_time_gran = 1; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 65 | |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 66 | inode = configfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, |
Al Viro | b7c177f | 2012-03-17 16:24:54 -0400 | [diff] [blame] | 67 | &configfs_root, sb); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 68 | if (inode) { |
Al Viro | 81d44ed1 | 2012-03-17 16:13:25 -0400 | [diff] [blame] | 69 | inode->i_op = &configfs_root_inode_operations; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 70 | inode->i_fop = &configfs_dir_operations; |
| 71 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 72 | inc_nlink(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 73 | } else { |
Fabian Frederick | 1d88aa4 | 2014-06-04 16:05:59 -0700 | [diff] [blame] | 74 | pr_debug("could not get root inode\n"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 75 | return -ENOMEM; |
| 76 | } |
| 77 | |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 78 | root = d_make_root(inode); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 79 | if (!root) { |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 80 | pr_debug("%s: could not get root dentry!\n",__func__); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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; |
Al Viro | d463a0c | 2011-01-12 16:41:05 -0500 | [diff] [blame] | 87 | sb->s_d_op = &configfs_dentry_ops; /* the rest get that */ |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 91 | static struct dentry *configfs_do_mount(struct file_system_type *fs_type, |
| 92 | int flags, const char *dev_name, void *data) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 93 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 94 | return mount_single(fs_type, flags, data, configfs_fill_super); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static struct file_system_type configfs_fs_type = { |
| 98 | .owner = THIS_MODULE, |
| 99 | .name = "configfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 100 | .mount = configfs_do_mount, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 101 | .kill_sb = kill_litter_super, |
| 102 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 103 | MODULE_ALIAS_FS("configfs"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 104 | |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 105 | struct dentry *configfs_pin_fs(void) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 106 | { |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 107 | int err = simple_pin_fs(&configfs_fs_type, &configfs_mount, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 108 | &configfs_mnt_count); |
Al Viro | 2a152ad | 2012-03-17 16:53:29 -0400 | [diff] [blame] | 109 | return err ? ERR_PTR(err) : configfs_mount->mnt_root; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void configfs_release_fs(void) |
| 113 | { |
| 114 | simple_release_fs(&configfs_mount, &configfs_mnt_count); |
| 115 | } |
| 116 | |
| 117 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 118 | static int __init configfs_init(void) |
| 119 | { |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 120 | int err = -ENOMEM; |
| 121 | |
| 122 | configfs_dir_cachep = kmem_cache_create("configfs_dir_cache", |
| 123 | sizeof(struct configfs_dirent), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 124 | 0, 0, NULL); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 125 | if (!configfs_dir_cachep) |
| 126 | goto out; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 127 | |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 128 | err = sysfs_create_mount_point(kernel_kobj, "config"); |
| 129 | if (err) |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 130 | goto out2; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 131 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 132 | err = register_filesystem(&configfs_fs_type); |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 133 | if (err) |
| 134 | goto out3; |
| 135 | |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 136 | return 0; |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 137 | out3: |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 138 | pr_err("Unable to register filesystem!\n"); |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 139 | sysfs_remove_mount_point(kernel_kobj, "config"); |
Al Viro | 7c6455e | 2011-12-13 12:32:42 -0500 | [diff] [blame] | 140 | out2: |
| 141 | kmem_cache_destroy(configfs_dir_cachep); |
| 142 | configfs_dir_cachep = NULL; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 143 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static void __exit configfs_exit(void) |
| 148 | { |
| 149 | unregister_filesystem(&configfs_fs_type); |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 150 | sysfs_remove_mount_point(kernel_kobj, "config"); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 151 | kmem_cache_destroy(configfs_dir_cachep); |
| 152 | configfs_dir_cachep = NULL; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | MODULE_AUTHOR("Oracle"); |
| 156 | MODULE_LICENSE("GPL"); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 157 | MODULE_VERSION("0.0.2"); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 158 | MODULE_DESCRIPTION("Simple RAM filesystem for user driven kernel subsystem configuration."); |
| 159 | |
Daniel Baluta | f5b6977 | 2015-05-05 16:23:54 -0700 | [diff] [blame] | 160 | core_initcall(configfs_init); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 161 | module_exit(configfs_exit); |