blob: 8596bd1a4d26536ebf340aa43ef3ef56b31c01fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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 Schmielau4e57b682005-10-30 15:03:48 -080018#include <linux/sched.h> /* current */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Adrian Bunk6e21bd92006-01-08 01:05:15 -080020#include "capifs.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022MODULE_DESCRIPTION("CAPI4Linux: /dev/capi/ filesystem");
23MODULE_AUTHOR("Carsten Paeth");
24MODULE_LICENSE("GPL");
25
26/* ------------------------------------------------------------------ */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define CAPIFS_SUPER_MAGIC (('C'<<8)|'N')
29
30static struct vfsmount *capifs_mnt;
Jan Kiszkae11e7ac2010-02-08 10:12:08 +000031static int capifs_mnt_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static struct {
34 int setuid;
35 int setgid;
36 uid_t uid;
37 gid_t gid;
38 umode_t mode;
39} config = {.mode = 0600};
40
41/* ------------------------------------------------------------------ */
42
43static int capifs_remount(struct super_block *s, int *flags, char *data)
44{
45 int setuid = 0;
46 int setgid = 0;
47 uid_t uid = 0;
48 gid_t gid = 0;
49 umode_t mode = 0600;
50 char *this_char;
Miklos Szeredie55e2122008-02-08 04:21:40 -080051 char *new_opt = kstrdup(data, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 this_char = NULL;
54 while ((this_char = strsep(&data, ",")) != NULL) {
55 int n;
56 char dummy;
57 if (!*this_char)
58 continue;
59 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
60 setuid = 1;
61 uid = n;
62 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
63 setgid = 1;
64 gid = n;
65 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
66 mode = n & ~S_IFMT;
67 else {
Cyrill Gorcunovc24e9b32008-04-28 02:14:41 -070068 kfree(new_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 printk("capifs: called with bogus options\n");
70 return -EINVAL;
71 }
72 }
Miklos Szeredie55e2122008-02-08 04:21:40 -080073
Al Virob0c4f322009-05-08 16:23:30 -040074 mutex_lock(&s->s_root->d_inode->i_mutex);
Miklos Szeredie55e2122008-02-08 04:21:40 -080075
Al Virob0c4f322009-05-08 16:23:30 -040076 replace_mount_options(s, new_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 config.setuid = setuid;
78 config.setgid = setgid;
79 config.uid = uid;
80 config.gid = gid;
81 config.mode = mode;
Miklos Szeredie55e2122008-02-08 04:21:40 -080082
Al Virob0c4f322009-05-08 16:23:30 -040083 mutex_unlock(&s->s_root->d_inode->i_mutex);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070088static const struct super_operations capifs_sops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 .statfs = simple_statfs,
91 .remount_fs = capifs_remount,
Miklos Szeredie55e2122008-02-08 04:21:40 -080092 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95
96static int
97capifs_fill_super(struct super_block *s, void *data, int silent)
98{
99 struct inode * inode;
100
101 s->s_blocksize = 1024;
102 s->s_blocksize_bits = 10;
103 s->s_magic = CAPIFS_SUPER_MAGIC;
104 s->s_op = &capifs_sops;
105 s->s_time_gran = 1;
106
107 inode = new_inode(s);
108 if (!inode)
109 goto fail;
110 inode->i_ino = 1;
111 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
113 inode->i_op = &simple_dir_inode_operations;
114 inode->i_fop = &simple_dir_operations;
115 inode->i_nlink = 2;
116
Jan Kiszka07ad6032010-02-08 10:12:07 +0000117 s->s_root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (s->s_root)
119 return 0;
120
121 printk("capifs: get root dentry failed\n");
122 iput(inode);
123fail:
124 return -ENOMEM;
125}
126
David Howells454e2392006-06-23 02:02:57 -0700127static int capifs_get_sb(struct file_system_type *fs_type,
128 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
David Howells454e2392006-06-23 02:02:57 -0700130 return get_sb_single(fs_type, flags, data, capifs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static struct file_system_type capifs_fs_type = {
134 .owner = THIS_MODULE,
135 .name = "capifs",
136 .get_sb = capifs_get_sb,
137 .kill_sb = kill_anon_super,
138};
139
Jan Kiszkae11e7ac2010-02-08 10:12:08 +0000140static struct dentry *new_ncci(unsigned int number, dev_t device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jan Kiszka07ad6032010-02-08 10:12:07 +0000142 struct super_block *s = capifs_mnt->mnt_sb;
143 struct dentry *root = s->s_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct dentry *dentry;
Jan Kiszkac9478622010-02-08 10:12:05 +0000145 struct inode *inode;
Jan Kiszka90926f02010-02-08 10:12:06 +0000146 char name[10];
147 int namelen;
Al Virob0c4f322009-05-08 16:23:30 -0400148
Jan Kiszka07ad6032010-02-08 10:12:07 +0000149 mutex_lock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000150
151 namelen = sprintf(name, "%d", number);
Jan Kiszka07ad6032010-02-08 10:12:07 +0000152 dentry = lookup_one_len(name, root, namelen);
Jan Kiszka90926f02010-02-08 10:12:06 +0000153 if (IS_ERR(dentry)) {
154 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000155 goto unlock_out;
Jan Kiszka90926f02010-02-08 10:12:06 +0000156 }
Jan Kiszkac9478622010-02-08 10:12:05 +0000157
158 if (dentry->d_inode) {
159 dput(dentry);
Jan Kiszka90926f02010-02-08 10:12:06 +0000160 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000161 goto unlock_out;
162 }
163
Jan Kiszka07ad6032010-02-08 10:12:07 +0000164 inode = new_inode(s);
Jan Kiszkac9478622010-02-08 10:12:05 +0000165 if (!inode) {
166 dput(dentry);
Jan Kiszka90926f02010-02-08 10:12:06 +0000167 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000168 goto unlock_out;
169 }
Al Virob0c4f322009-05-08 16:23:30 -0400170
171 /* config contents is protected by root's i_mutex */
David Howells0e164b62008-11-14 10:38:42 +1100172 inode->i_uid = config.setuid ? config.uid : current_fsuid();
173 inode->i_gid = config.setgid ? config.gid : current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Jan Kiszkac9478622010-02-08 10:12:05 +0000175 inode->i_ino = number + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 init_special_inode(inode, S_IFCHR|config.mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jan Kiszkac9478622010-02-08 10:12:05 +0000178 d_instantiate(dentry, inode);
Jan Kiszka90926f02010-02-08 10:12:06 +0000179 dget(dentry);
Jan Kiszkac9478622010-02-08 10:12:05 +0000180
181unlock_out:
Jan Kiszka07ad6032010-02-08 10:12:07 +0000182 mutex_unlock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000183
184 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Jan Kiszkae11e7ac2010-02-08 10:12:08 +0000187struct dentry *capifs_new_ncci(unsigned int number, dev_t device)
188{
189 struct dentry *dentry;
190
191 if (simple_pin_fs(&capifs_fs_type, &capifs_mnt, &capifs_mnt_count) < 0)
192 return NULL;
193
194 dentry = new_ncci(number, device);
195 if (!dentry)
196 simple_release_fs(&capifs_mnt, &capifs_mnt_count);
197
198 return dentry;
199}
200
Jan Kiszka90926f02010-02-08 10:12:06 +0000201void capifs_free_ncci(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Jan Kiszka07ad6032010-02-08 10:12:07 +0000203 struct dentry *root = capifs_mnt->mnt_sb->s_root;
Jan Kiszka90926f02010-02-08 10:12:06 +0000204 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jan Kiszka90926f02010-02-08 10:12:06 +0000206 if (!dentry)
207 return;
208
Jan Kiszka07ad6032010-02-08 10:12:07 +0000209 mutex_lock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000210
211 inode = dentry->d_inode;
212 if (inode) {
213 drop_nlink(inode);
214 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dput(dentry);
216 }
Jan Kiszka90926f02010-02-08 10:12:06 +0000217 dput(dentry);
218
Jan Kiszka07ad6032010-02-08 10:12:07 +0000219 mutex_unlock(&root->d_inode->i_mutex);
Jan Kiszkae11e7ac2010-02-08 10:12:08 +0000220
221 simple_release_fs(&capifs_mnt, &capifs_mnt_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static int __init capifs_init(void)
225{
Jan Kiszka88549d62010-02-08 10:12:09 +0000226 return register_filesystem(&capifs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static void __exit capifs_exit(void)
230{
231 unregister_filesystem(&capifs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234EXPORT_SYMBOL(capifs_new_ncci);
235EXPORT_SYMBOL(capifs_free_ncci);
236
237module_init(capifs_init);
238module_exit(capifs_exit);