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