Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: capifs.c,v 1.1.2.3 2004/01/16 21:09:26 keil Exp $ |
| 2 | * |
| 3 | * Copyright 2000 by Carsten Paeth <calle@calle.de> |
| 4 | * |
| 5 | * Heavily based on devpts filesystem from H. Peter Anvin |
| 6 | * |
| 7 | * This software may be used and distributed according to the terms |
| 8 | * of the GNU General Public License, incorporated herein by reference. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/namei.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/ctype.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/sched.h> /* current */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Adrian Bunk | 6e21bd9 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 21 | #include "capifs.h" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | MODULE_DESCRIPTION("CAPI4Linux: /dev/capi/ filesystem"); |
| 24 | MODULE_AUTHOR("Carsten Paeth"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
| 27 | /* ------------------------------------------------------------------ */ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define CAPIFS_SUPER_MAGIC (('C'<<8)|'N') |
| 30 | |
| 31 | static struct vfsmount *capifs_mnt; |
Jan Kiszka | e11e7ac | 2010-02-08 10:12:08 +0000 | [diff] [blame] | 32 | static int capifs_mnt_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static struct { |
| 35 | int setuid; |
| 36 | int setgid; |
| 37 | uid_t uid; |
| 38 | gid_t gid; |
| 39 | umode_t mode; |
| 40 | } config = {.mode = 0600}; |
| 41 | |
| 42 | /* ------------------------------------------------------------------ */ |
| 43 | |
| 44 | static int capifs_remount(struct super_block *s, int *flags, char *data) |
| 45 | { |
| 46 | int setuid = 0; |
| 47 | int setgid = 0; |
| 48 | uid_t uid = 0; |
| 49 | gid_t gid = 0; |
| 50 | umode_t mode = 0600; |
| 51 | char *this_char; |
Miklos Szeredi | e55e212 | 2008-02-08 04:21:40 -0800 | [diff] [blame] | 52 | char *new_opt = kstrdup(data, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | this_char = NULL; |
| 55 | while ((this_char = strsep(&data, ",")) != NULL) { |
| 56 | int n; |
| 57 | char dummy; |
| 58 | if (!*this_char) |
| 59 | continue; |
| 60 | if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) { |
| 61 | setuid = 1; |
| 62 | uid = n; |
| 63 | } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) { |
| 64 | setgid = 1; |
| 65 | gid = n; |
| 66 | } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1) |
| 67 | mode = n & ~S_IFMT; |
| 68 | else { |
Cyrill Gorcunov | c24e9b3 | 2008-04-28 02:14:41 -0700 | [diff] [blame] | 69 | kfree(new_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | printk("capifs: called with bogus options\n"); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | } |
Miklos Szeredi | e55e212 | 2008-02-08 04:21:40 -0800 | [diff] [blame] | 74 | |
Al Viro | b0c4f32 | 2009-05-08 16:23:30 -0400 | [diff] [blame] | 75 | mutex_lock(&s->s_root->d_inode->i_mutex); |
Miklos Szeredi | e55e212 | 2008-02-08 04:21:40 -0800 | [diff] [blame] | 76 | |
Al Viro | b0c4f32 | 2009-05-08 16:23:30 -0400 | [diff] [blame] | 77 | replace_mount_options(s, new_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | config.setuid = setuid; |
| 79 | config.setgid = setgid; |
| 80 | config.uid = uid; |
| 81 | config.gid = gid; |
| 82 | config.mode = mode; |
Miklos Szeredi | e55e212 | 2008-02-08 04:21:40 -0800 | [diff] [blame] | 83 | |
Al Viro | b0c4f32 | 2009-05-08 16:23:30 -0400 | [diff] [blame] | 84 | mutex_unlock(&s->s_root->d_inode->i_mutex); |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 89 | static const struct super_operations capifs_sops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | .statfs = simple_statfs, |
| 92 | .remount_fs = capifs_remount, |
Miklos Szeredi | e55e212 | 2008-02-08 04:21:40 -0800 | [diff] [blame] | 93 | .show_options = generic_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
| 97 | static int |
| 98 | capifs_fill_super(struct super_block *s, void *data, int silent) |
| 99 | { |
| 100 | struct inode * inode; |
| 101 | |
| 102 | s->s_blocksize = 1024; |
| 103 | s->s_blocksize_bits = 10; |
| 104 | s->s_magic = CAPIFS_SUPER_MAGIC; |
| 105 | s->s_op = &capifs_sops; |
| 106 | s->s_time_gran = 1; |
| 107 | |
| 108 | inode = new_inode(s); |
| 109 | if (!inode) |
| 110 | goto fail; |
| 111 | inode->i_ino = 1; |
| 112 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; |
| 114 | inode->i_op = &simple_dir_inode_operations; |
| 115 | inode->i_fop = &simple_dir_operations; |
| 116 | inode->i_nlink = 2; |
| 117 | |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 118 | s->s_root = d_alloc_root(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (s->s_root) |
| 120 | return 0; |
| 121 | |
| 122 | printk("capifs: get root dentry failed\n"); |
| 123 | iput(inode); |
| 124 | fail: |
| 125 | return -ENOMEM; |
| 126 | } |
| 127 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 128 | static struct dentry *capifs_mount(struct file_system_type *fs_type, |
| 129 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 131 | return mount_single(fs_type, flags, data, capifs_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static struct file_system_type capifs_fs_type = { |
| 135 | .owner = THIS_MODULE, |
| 136 | .name = "capifs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 137 | .mount = capifs_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .kill_sb = kill_anon_super, |
| 139 | }; |
| 140 | |
Jan Kiszka | e11e7ac | 2010-02-08 10:12:08 +0000 | [diff] [blame] | 141 | static struct dentry *new_ncci(unsigned int number, dev_t device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 143 | struct super_block *s = capifs_mnt->mnt_sb; |
| 144 | struct dentry *root = s->s_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | struct dentry *dentry; |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 146 | struct inode *inode; |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 147 | char name[10]; |
| 148 | int namelen; |
Al Viro | b0c4f32 | 2009-05-08 16:23:30 -0400 | [diff] [blame] | 149 | |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 150 | mutex_lock(&root->d_inode->i_mutex); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 151 | |
| 152 | namelen = sprintf(name, "%d", number); |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 153 | dentry = lookup_one_len(name, root, namelen); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 154 | if (IS_ERR(dentry)) { |
| 155 | dentry = NULL; |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 156 | goto unlock_out; |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 157 | } |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 158 | |
| 159 | if (dentry->d_inode) { |
| 160 | dput(dentry); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 161 | dentry = NULL; |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 162 | goto unlock_out; |
| 163 | } |
| 164 | |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 165 | inode = new_inode(s); |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 166 | if (!inode) { |
| 167 | dput(dentry); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 168 | dentry = NULL; |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 169 | goto unlock_out; |
| 170 | } |
Al Viro | b0c4f32 | 2009-05-08 16:23:30 -0400 | [diff] [blame] | 171 | |
| 172 | /* config contents is protected by root's i_mutex */ |
David Howells | 0e164b6 | 2008-11-14 10:38:42 +1100 | [diff] [blame] | 173 | inode->i_uid = config.setuid ? config.uid : current_fsuid(); |
| 174 | inode->i_gid = config.setgid ? config.gid : current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 176 | inode->i_ino = number + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | init_special_inode(inode, S_IFCHR|config.mode, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 179 | d_instantiate(dentry, inode); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 180 | dget(dentry); |
Jan Kiszka | c947862 | 2010-02-08 10:12:05 +0000 | [diff] [blame] | 181 | |
| 182 | unlock_out: |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 183 | mutex_unlock(&root->d_inode->i_mutex); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 184 | |
| 185 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jan Kiszka | e11e7ac | 2010-02-08 10:12:08 +0000 | [diff] [blame] | 188 | struct dentry *capifs_new_ncci(unsigned int number, dev_t device) |
| 189 | { |
| 190 | struct dentry *dentry; |
| 191 | |
| 192 | if (simple_pin_fs(&capifs_fs_type, &capifs_mnt, &capifs_mnt_count) < 0) |
| 193 | return NULL; |
| 194 | |
| 195 | dentry = new_ncci(number, device); |
| 196 | if (!dentry) |
| 197 | simple_release_fs(&capifs_mnt, &capifs_mnt_count); |
| 198 | |
| 199 | return dentry; |
| 200 | } |
| 201 | |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 202 | void capifs_free_ncci(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 204 | struct dentry *root = capifs_mnt->mnt_sb->s_root; |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 205 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 207 | if (!dentry) |
| 208 | return; |
| 209 | |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 210 | mutex_lock(&root->d_inode->i_mutex); |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 211 | |
| 212 | inode = dentry->d_inode; |
| 213 | if (inode) { |
| 214 | drop_nlink(inode); |
| 215 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | dput(dentry); |
| 217 | } |
Jan Kiszka | 90926f0 | 2010-02-08 10:12:06 +0000 | [diff] [blame] | 218 | dput(dentry); |
| 219 | |
Jan Kiszka | 07ad603 | 2010-02-08 10:12:07 +0000 | [diff] [blame] | 220 | mutex_unlock(&root->d_inode->i_mutex); |
Jan Kiszka | e11e7ac | 2010-02-08 10:12:08 +0000 | [diff] [blame] | 221 | |
| 222 | simple_release_fs(&capifs_mnt, &capifs_mnt_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int __init capifs_init(void) |
| 226 | { |
Jan Kiszka | 88549d6 | 2010-02-08 10:12:09 +0000 | [diff] [blame] | 227 | return register_filesystem(&capifs_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void __exit capifs_exit(void) |
| 231 | { |
| 232 | unregister_filesystem(&capifs_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | EXPORT_SYMBOL(capifs_new_ncci); |
| 236 | EXPORT_SYMBOL(capifs_free_ncci); |
| 237 | |
| 238 | module_init(capifs_init); |
| 239 | module_exit(capifs_exit); |