backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[linux-2.6.git] / fs / devpts / inode.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2  *
3  * linux/fs/devpts/inode.c
4  *
5  *  Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
6  *
7  * This file is part of the Linux kernel and is made available under
8  * the terms of the GNU General Public License, version 2, or at your
9  * option, any later version, incorporated herein by reference.
10  *
11  * ------------------------------------------------------------------------- */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/sched.h>
17 #include <linux/namei.h>
18 #include <linux/mount.h>
19 #include <linux/tty.h>
20 #include <linux/devpts_fs.h>
21 #include <linux/parser.h>
22 #include <linux/vs_base.h>
23
24
25 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
26 {
27         int ret = -EACCES;
28
29         if (vx_check(inode->i_xid, VX_IDENT))
30                 ret = generic_permission(inode, mask, NULL);
31         return ret;
32 }
33
34 static struct inode_operations devpts_file_inode_operations = {
35         .permission     = devpts_permission,
36 };
37
38 static struct vfsmount *devpts_mnt;
39 static struct dentry *devpts_root;
40
41 static struct {
42         int setuid;
43         int setgid;
44         uid_t   uid;
45         gid_t   gid;
46         umode_t mode;
47 } config = {.mode = 0600};
48
49 enum {
50         Opt_uid, Opt_gid, Opt_mode,
51         Opt_err
52 };
53
54 static match_table_t tokens = {
55         {Opt_uid, "uid=%u"},
56         {Opt_gid, "gid=%u"},
57         {Opt_mode, "mode=%o"},
58         {Opt_err, NULL}
59 };
60
61 static int devpts_remount(struct super_block *sb, int *flags, char *data)
62 {
63         char *p;
64
65         config.setuid  = 0;
66         config.setgid  = 0;
67         config.uid     = 0;
68         config.gid     = 0;
69         config.mode    = 0600;
70
71         while ((p = strsep(&data, ",")) != NULL) {
72                 substring_t args[MAX_OPT_ARGS];
73                 int token;
74                 int option;
75
76                 if (!*p)
77                         continue;
78
79                 token = match_token(p, tokens, args);
80                 switch (token) {
81                 case Opt_uid:
82                         if (match_int(&args[0], &option))
83                                 return -EINVAL;
84                         config.uid = option;
85                         config.setuid = 1;
86                         break;
87                 case Opt_gid:
88                         if (match_int(&args[0], &option))
89                                 return -EINVAL;
90                         config.gid = option;
91                         config.setgid = 1;
92                         break;
93                 case Opt_mode:
94                         if (match_octal(&args[0], &option))
95                                 return -EINVAL;
96                         config.mode = option & ~S_IFMT;
97                         break;
98                 default:
99                         printk(KERN_ERR "devpts: called with bogus options\n");
100                         return -EINVAL;
101                 }
102         }
103
104         return 0;
105 }
106
107 static int devpts_filter(struct dentry *de)
108 {
109         return vx_check(de->d_inode->i_xid, VX_IDENT);
110 }
111
112 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
113 {
114         return dcache_readdir_filter(filp, dirent, filldir, devpts_filter);
115 }
116
117 static struct file_operations devpts_dir_operations = {
118         .open           = dcache_dir_open,
119         .release        = dcache_dir_close,
120         .llseek         = dcache_dir_lseek,
121         .read           = generic_read_dir,
122         .readdir        = devpts_readdir,
123 };
124
125 static struct super_operations devpts_sops = {
126         .statfs         = simple_statfs,
127         .remount_fs     = devpts_remount,
128 };
129
130 static int
131 devpts_fill_super(struct super_block *s, void *data, int silent)
132 {
133         struct inode * inode;
134
135         s->s_blocksize = 1024;
136         s->s_blocksize_bits = 10;
137         s->s_magic = DEVPTS_SUPER_MAGIC;
138         s->s_op = &devpts_sops;
139         s->s_time_gran = 1;
140
141         inode = new_inode(s);
142         if (!inode)
143                 goto fail;
144         inode->i_ino = 1;
145         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
146         inode->i_blocks = 0;
147         inode->i_uid = inode->i_gid = 0;
148         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
149         inode->i_op = &simple_dir_inode_operations;
150         inode->i_fop = &devpts_dir_operations;
151         inode->i_nlink = 2;
152         inode->i_xid = vx_current_xid();
153
154         devpts_root = s->s_root = d_alloc_root(inode);
155         if (s->s_root)
156                 return 0;
157         
158         printk("devpts: get root dentry failed\n");
159         iput(inode);
160 fail:
161         return -ENOMEM;
162 }
163
164 static int devpts_get_sb(struct file_system_type *fs_type,
165         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
166 {
167         return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
168 }
169
170 static struct file_system_type devpts_fs_type = {
171         .owner          = THIS_MODULE,
172         .name           = "devpts",
173         .get_sb         = devpts_get_sb,
174         .kill_sb        = kill_anon_super,
175 };
176
177 /*
178  * The normal naming convention is simply /dev/pts/<number>; this conforms
179  * to the System V naming convention
180  */
181
182 static struct dentry *get_node(int num)
183 {
184         char s[12];
185         struct dentry *root = devpts_root;
186         mutex_lock(&root->d_inode->i_mutex);
187         return lookup_one_len(s, root, sprintf(s, "%d", num));
188 }
189
190 int devpts_pty_new(struct tty_struct *tty)
191 {
192         int number = tty->index;
193         struct tty_driver *driver = tty->driver;
194         dev_t device = MKDEV(driver->major, driver->minor_start+number);
195         struct dentry *dentry;
196         struct inode *inode = new_inode(devpts_mnt->mnt_sb);
197
198         /* We're supposed to be given the slave end of a pty */
199         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
200         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
201
202         if (!inode)
203                 return -ENOMEM;
204
205         inode->i_ino = number+2;
206         inode->i_uid = config.setuid ? config.uid : current->fsuid;
207         inode->i_gid = config.setgid ? config.gid : current->fsgid;
208         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
209         init_special_inode(inode, S_IFCHR|config.mode, device);
210         inode->i_xid = vx_current_xid();
211         inode->i_op = &devpts_file_inode_operations;
212         inode->i_private = tty;
213
214         dentry = get_node(number);
215         if (!IS_ERR(dentry) && !dentry->d_inode)
216                 d_instantiate(dentry, inode);
217
218         mutex_unlock(&devpts_root->d_inode->i_mutex);
219
220         return 0;
221 }
222
223 struct tty_struct *devpts_get_tty(int number)
224 {
225         struct dentry *dentry = get_node(number);
226         struct tty_struct *tty;
227
228         tty = NULL;
229         if (!IS_ERR(dentry)) {
230                 if (dentry->d_inode)
231                         tty = dentry->d_inode->i_private;
232                 dput(dentry);
233         }
234
235         mutex_unlock(&devpts_root->d_inode->i_mutex);
236
237         return tty;
238 }
239
240 void devpts_pty_kill(int number)
241 {
242         struct dentry *dentry = get_node(number);
243
244         if (!IS_ERR(dentry)) {
245                 struct inode *inode = dentry->d_inode;
246                 if (inode) {
247                         inode->i_nlink--;
248                         d_delete(dentry);
249                         dput(dentry);
250                 }
251                 dput(dentry);
252         }
253         mutex_unlock(&devpts_root->d_inode->i_mutex);
254 }
255
256 static int __init init_devpts_fs(void)
257 {
258         int err = register_filesystem(&devpts_fs_type);
259         if (!err) {
260                 devpts_mnt = kern_mount(&devpts_fs_type);
261                 if (IS_ERR(devpts_mnt))
262                         err = PTR_ERR(devpts_mnt);
263         }
264         return err;
265 }
266
267 static void __exit exit_devpts_fs(void)
268 {
269         unregister_filesystem(&devpts_fs_type);
270         mntput(devpts_mnt);
271 }
272
273 module_init(init_devpts_fs)
274 module_exit(exit_devpts_fs)
275 MODULE_LICENSE("GPL");