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