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