blob: 6ae0edf29c85d95dcf109ddf8282a34b69abd312 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static struct {
37 int setuid;
38 int setgid;
39 uid_t uid;
40 gid_t gid;
41 umode_t mode;
42} config = {.mode = 0600};
43
44/* ------------------------------------------------------------------ */
45
46static int capifs_remount(struct super_block *s, int *flags, char *data)
47{
48 int setuid = 0;
49 int setgid = 0;
50 uid_t uid = 0;
51 gid_t gid = 0;
52 umode_t mode = 0600;
53 char *this_char;
Miklos Szeredie55e2122008-02-08 04:21:40 -080054 char *new_opt = kstrdup(data, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 this_char = NULL;
57 while ((this_char = strsep(&data, ",")) != NULL) {
58 int n;
59 char dummy;
60 if (!*this_char)
61 continue;
62 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
63 setuid = 1;
64 uid = n;
65 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
66 setgid = 1;
67 gid = n;
68 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
69 mode = n & ~S_IFMT;
70 else {
Cyrill Gorcunovc24e9b32008-04-28 02:14:41 -070071 kfree(new_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 printk("capifs: called with bogus options\n");
73 return -EINVAL;
74 }
75 }
Miklos Szeredie55e2122008-02-08 04:21:40 -080076
Al Virob0c4f322009-05-08 16:23:30 -040077 mutex_lock(&s->s_root->d_inode->i_mutex);
Miklos Szeredie55e2122008-02-08 04:21:40 -080078
Al Virob0c4f322009-05-08 16:23:30 -040079 replace_mount_options(s, new_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 config.setuid = setuid;
81 config.setgid = setgid;
82 config.uid = uid;
83 config.gid = gid;
84 config.mode = mode;
Miklos Szeredie55e2122008-02-08 04:21:40 -080085
Al Virob0c4f322009-05-08 16:23:30 -040086 mutex_unlock(&s->s_root->d_inode->i_mutex);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89}
90
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070091static const struct super_operations capifs_sops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 .statfs = simple_statfs,
94 .remount_fs = capifs_remount,
Miklos Szeredie55e2122008-02-08 04:21:40 -080095 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98
99static int
100capifs_fill_super(struct super_block *s, void *data, int silent)
101{
102 struct inode * inode;
103
104 s->s_blocksize = 1024;
105 s->s_blocksize_bits = 10;
106 s->s_magic = CAPIFS_SUPER_MAGIC;
107 s->s_op = &capifs_sops;
108 s->s_time_gran = 1;
109
110 inode = new_inode(s);
111 if (!inode)
112 goto fail;
113 inode->i_ino = 1;
114 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
116 inode->i_op = &simple_dir_inode_operations;
117 inode->i_fop = &simple_dir_operations;
118 inode->i_nlink = 2;
119
Jan Kiszka07ad6032010-02-08 10:12:07 +0000120 s->s_root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (s->s_root)
122 return 0;
123
124 printk("capifs: get root dentry failed\n");
125 iput(inode);
126fail:
127 return -ENOMEM;
128}
129
David Howells454e2392006-06-23 02:02:57 -0700130static int capifs_get_sb(struct file_system_type *fs_type,
131 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
David Howells454e2392006-06-23 02:02:57 -0700133 return get_sb_single(fs_type, flags, data, capifs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static struct file_system_type capifs_fs_type = {
137 .owner = THIS_MODULE,
138 .name = "capifs",
139 .get_sb = capifs_get_sb,
140 .kill_sb = kill_anon_super,
141};
142
Jan Kiszka90926f02010-02-08 10:12:06 +0000143struct dentry *capifs_new_ncci(unsigned int number, dev_t device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jan Kiszka07ad6032010-02-08 10:12:07 +0000145 struct super_block *s = capifs_mnt->mnt_sb;
146 struct dentry *root = s->s_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct dentry *dentry;
Jan Kiszkac9478622010-02-08 10:12:05 +0000148 struct inode *inode;
Jan Kiszka90926f02010-02-08 10:12:06 +0000149 char name[10];
150 int namelen;
Al Virob0c4f322009-05-08 16:23:30 -0400151
Jan Kiszka07ad6032010-02-08 10:12:07 +0000152 mutex_lock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000153
154 namelen = sprintf(name, "%d", number);
Jan Kiszka07ad6032010-02-08 10:12:07 +0000155 dentry = lookup_one_len(name, root, namelen);
Jan Kiszka90926f02010-02-08 10:12:06 +0000156 if (IS_ERR(dentry)) {
157 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000158 goto unlock_out;
Jan Kiszka90926f02010-02-08 10:12:06 +0000159 }
Jan Kiszkac9478622010-02-08 10:12:05 +0000160
161 if (dentry->d_inode) {
162 dput(dentry);
Jan Kiszka90926f02010-02-08 10:12:06 +0000163 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000164 goto unlock_out;
165 }
166
Jan Kiszka07ad6032010-02-08 10:12:07 +0000167 inode = new_inode(s);
Jan Kiszkac9478622010-02-08 10:12:05 +0000168 if (!inode) {
169 dput(dentry);
Jan Kiszka90926f02010-02-08 10:12:06 +0000170 dentry = NULL;
Jan Kiszkac9478622010-02-08 10:12:05 +0000171 goto unlock_out;
172 }
Al Virob0c4f322009-05-08 16:23:30 -0400173
174 /* config contents is protected by root's i_mutex */
David Howells0e164b62008-11-14 10:38:42 +1100175 inode->i_uid = config.setuid ? config.uid : current_fsuid();
176 inode->i_gid = config.setgid ? config.gid : current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Jan Kiszkac9478622010-02-08 10:12:05 +0000178 inode->i_ino = number + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 init_special_inode(inode, S_IFCHR|config.mode, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jan Kiszkac9478622010-02-08 10:12:05 +0000181 d_instantiate(dentry, inode);
Jan Kiszka90926f02010-02-08 10:12:06 +0000182 dget(dentry);
Jan Kiszkac9478622010-02-08 10:12:05 +0000183
184unlock_out:
Jan Kiszka07ad6032010-02-08 10:12:07 +0000185 mutex_unlock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000186
187 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Jan Kiszka90926f02010-02-08 10:12:06 +0000190void capifs_free_ncci(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Jan Kiszka07ad6032010-02-08 10:12:07 +0000192 struct dentry *root = capifs_mnt->mnt_sb->s_root;
Jan Kiszka90926f02010-02-08 10:12:06 +0000193 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jan Kiszka90926f02010-02-08 10:12:06 +0000195 if (!dentry)
196 return;
197
Jan Kiszka07ad6032010-02-08 10:12:07 +0000198 mutex_lock(&root->d_inode->i_mutex);
Jan Kiszka90926f02010-02-08 10:12:06 +0000199
200 inode = dentry->d_inode;
201 if (inode) {
202 drop_nlink(inode);
203 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 dput(dentry);
205 }
Jan Kiszka90926f02010-02-08 10:12:06 +0000206 dput(dentry);
207
Jan Kiszka07ad6032010-02-08 10:12:07 +0000208 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static int __init capifs_init(void)
212{
213 char rev[32];
214 char *p;
215 int err;
216
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700217 if ((p = strchr(revision, ':')) != NULL && p[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 strlcpy(rev, p + 2, sizeof(rev));
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700219 if ((p = strchr(rev, '$')) != NULL && p > rev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *(p-1) = 0;
221 } else
222 strcpy(rev, "1.0");
223
224 err = register_filesystem(&capifs_fs_type);
225 if (!err) {
226 capifs_mnt = kern_mount(&capifs_fs_type);
James Morris820d2202005-08-27 13:47:06 +0200227 if (IS_ERR(capifs_mnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err = PTR_ERR(capifs_mnt);
James Morris820d2202005-08-27 13:47:06 +0200229 unregister_filesystem(&capifs_fs_type);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232 if (!err)
233 printk(KERN_NOTICE "capifs: Rev %s\n", rev);
234 return err;
235}
236
237static void __exit capifs_exit(void)
238{
239 unregister_filesystem(&capifs_fs_type);
240 mntput(capifs_mnt);
241}
242
243EXPORT_SYMBOL(capifs_new_ncci);
244EXPORT_SYMBOL(capifs_free_ncci);
245
246module_init(capifs_init);
247module_exit(capifs_exit);