blob: 550e80f390a63f87da84f84e9ec0290f75ca7151 [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
28static char *revision = "$Revision: 1.1.2.3 $";
29
30/* ------------------------------------------------------------------ */
31
32#define CAPIFS_SUPER_MAGIC (('C'<<8)|'N')
33
34static struct vfsmount *capifs_mnt;
35static struct dentry *capifs_root;
36
37static 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
47static 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 Szeredie55e2122008-02-08 04:21:40 -080055 char *new_opt = kstrdup(data, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 Gorcunovc24e9b32008-04-28 02:14:41 -070072 kfree(new_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 printk("capifs: called with bogus options\n");
74 return -EINVAL;
75 }
76 }
Miklos Szeredie55e2122008-02-08 04:21:40 -080077
78 kfree(s->s_options);
79 s->s_options = new_opt;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 config.setuid = setuid;
82 config.setgid = setgid;
83 config.uid = uid;
84 config.gid = gid;
85 config.mode = mode;
Miklos Szeredie55e2122008-02-08 04:21:40 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
90static struct super_operations capifs_sops =
91{
92 .statfs = simple_statfs,
93 .remount_fs = capifs_remount,
Miklos Szeredie55e2122008-02-08 04:21:40 -080094 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97
98static int
99capifs_fill_super(struct super_block *s, void *data, int silent)
100{
101 struct inode * inode;
102
103 s->s_blocksize = 1024;
104 s->s_blocksize_bits = 10;
105 s->s_magic = CAPIFS_SUPER_MAGIC;
106 s->s_op = &capifs_sops;
107 s->s_time_gran = 1;
108
109 inode = new_inode(s);
110 if (!inode)
111 goto fail;
112 inode->i_ino = 1;
113 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
114 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 inode->i_uid = inode->i_gid = 0;
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);
127fail:
128 return -ENOMEM;
129}
130
David Howells454e2392006-06-23 02:02:57 -0700131static int capifs_get_sb(struct file_system_type *fs_type,
132 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
David Howells454e2392006-06-23 02:02:57 -0700134 return get_sb_single(fs_type, flags, data, capifs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137static 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
144static struct dentry *get_node(int num)
145{
146 char s[10];
147 struct dentry *root = capifs_root;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800148 mutex_lock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return lookup_one_len(s, root, sprintf(s, "%d", num));
150}
151
152void capifs_new_ncci(unsigned int number, dev_t device)
153{
154 struct dentry *dentry;
155 struct inode *inode = new_inode(capifs_mnt->mnt_sb);
156 if (!inode)
157 return;
158 inode->i_ino = number+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 inode->i_uid = config.setuid ? config.uid : current->fsuid;
160 inode->i_gid = config.setgid ? config.gid : current->fsgid;
161 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
162 init_special_inode(inode, S_IFCHR|config.mode, device);
163 //inode->i_op = &capifs_file_inode_operations;
164
165 dentry = get_node(number);
166 if (!IS_ERR(dentry) && !dentry->d_inode)
167 d_instantiate(dentry, inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800168 mutex_unlock(&capifs_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171void capifs_free_ncci(unsigned int number)
172{
173 struct dentry *dentry = get_node(number);
174
175 if (!IS_ERR(dentry)) {
176 struct inode *inode = dentry->d_inode;
177 if (inode) {
178 inode->i_nlink--;
179 d_delete(dentry);
180 dput(dentry);
181 }
182 dput(dentry);
183 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800184 mutex_unlock(&capifs_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static int __init capifs_init(void)
188{
189 char rev[32];
190 char *p;
191 int err;
192
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700193 if ((p = strchr(revision, ':')) != NULL && p[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 strlcpy(rev, p + 2, sizeof(rev));
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700195 if ((p = strchr(rev, '$')) != NULL && p > rev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *(p-1) = 0;
197 } else
198 strcpy(rev, "1.0");
199
200 err = register_filesystem(&capifs_fs_type);
201 if (!err) {
202 capifs_mnt = kern_mount(&capifs_fs_type);
James Morris820d2202005-08-27 13:47:06 +0200203 if (IS_ERR(capifs_mnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err = PTR_ERR(capifs_mnt);
James Morris820d2202005-08-27 13:47:06 +0200205 unregister_filesystem(&capifs_fs_type);
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 if (!err)
209 printk(KERN_NOTICE "capifs: Rev %s\n", rev);
210 return err;
211}
212
213static void __exit capifs_exit(void)
214{
215 unregister_filesystem(&capifs_fs_type);
216 mntput(capifs_mnt);
217}
218
219EXPORT_SYMBOL(capifs_new_ncci);
220EXPORT_SYMBOL(capifs_free_ncci);
221
222module_init(capifs_init);
223module_exit(capifs_exit);