This commit was manufactured by cvs2svn to create tag
[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/vs_base.h>
22 #include <linux/xattr.h>
23
24 #define DEVPTS_SUPER_MAGIC 0x1cd1
25
26 extern struct xattr_handler devpts_xattr_security_handler;
27
28 static struct xattr_handler *devpts_xattr_handlers[] = {
29 #ifdef CONFIG_DEVPTS_FS_SECURITY
30         &devpts_xattr_security_handler,
31 #endif
32         NULL
33 };
34
35 static struct vfsmount *devpts_mnt;
36 static struct dentry *devpts_root;
37
38 static struct {
39         int setuid;
40         int setgid;
41         uid_t   uid;
42         gid_t   gid;
43         umode_t mode;
44 } config = {.mode = 0600};
45
46 static int devpts_remount(struct super_block *sb, 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;
54
55         this_char = NULL;
56         while ((this_char = strsep(&data, ",")) != NULL) {
57                 int n;
58                 char dummy;
59                 if (!*this_char)
60                         continue;
61                 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
62                         setuid = 1;
63                         uid = n;
64                 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
65                         setgid = 1;
66                         gid = n;
67                 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
68                         mode = n & ~S_IFMT;
69                 else {
70                         printk("devpts: called with bogus options\n");
71                         return -EINVAL;
72                 }
73         }
74         config.setuid  = setuid;
75         config.setgid  = setgid;
76         config.uid     = uid;
77         config.gid     = gid;
78         config.mode    = mode;
79
80         return 0;
81 }
82
83 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
84 {
85         struct dentry *dentry = filp->f_dentry;
86         struct dentry *cursor = filp->private_data;
87         struct list_head *p, *q = &cursor->d_child;
88         ino_t ino;
89         int i = filp->f_pos;
90
91         switch (i) {
92                 case 0:
93                         ino = dentry->d_inode->i_ino;
94                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
95                                 break;
96                         filp->f_pos++;
97                         i++;
98                         /* fallthrough */
99                 case 1:
100                         ino = parent_ino(dentry);
101                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
102                                 break;
103                         filp->f_pos++;
104                         i++;
105                         /* fallthrough */
106                 default:
107                         spin_lock(&dcache_lock);
108                         if (filp->f_pos == 2) {
109                                 list_del(q);
110                                 list_add(q, &dentry->d_subdirs);
111                         }
112                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
113                                 struct dentry *next;
114                                 next = list_entry(p, struct dentry, d_child);
115                                 if (d_unhashed(next) || !next->d_inode)
116                                         continue;
117                                 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
118                                         continue;
119
120                                 spin_unlock(&dcache_lock);
121                                 if (filldir(dirent, next->d_name.name,
122                                         next->d_name.len, filp->f_pos,
123                                         next->d_inode->i_ino, DT_CHR) < 0)
124                                         return 0;
125                                 spin_lock(&dcache_lock);
126                                 /* next is still alive */
127                                 list_del(q);
128                                 list_add(q, p);
129                                 p = q;
130                                 filp->f_pos++;
131                         }
132                         spin_unlock(&dcache_lock);
133         }
134         return 0;
135 }
136
137 static struct file_operations devpts_dir_operations = {
138         .open           = dcache_dir_open,
139         .release        = dcache_dir_close,
140         .llseek         = dcache_dir_lseek,
141         .read           = generic_read_dir,
142         .readdir        = devpts_readdir,
143 };
144
145 static struct super_operations devpts_sops = {
146         .statfs         = simple_statfs,
147         .remount_fs     = devpts_remount,
148 };
149
150 static int
151 devpts_fill_super(struct super_block *s, void *data, int silent)
152 {
153         struct inode * inode;
154
155         s->s_blocksize = 1024;
156         s->s_blocksize_bits = 10;
157         s->s_magic = DEVPTS_SUPER_MAGIC;
158         s->s_op = &devpts_sops;
159         s->s_xattr = devpts_xattr_handlers;
160
161         inode = new_inode(s);
162         if (!inode)
163                 goto fail;
164         inode->i_ino = 1;
165         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
166         inode->i_blocks = 0;
167         inode->i_blksize = 1024;
168         inode->i_uid = inode->i_gid = 0;
169         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
170         inode->i_op = &simple_dir_inode_operations;
171         inode->i_fop = &devpts_dir_operations;
172         inode->i_nlink = 2;
173         inode->i_xid = vx_current_xid();
174
175         devpts_root = s->s_root = d_alloc_root(inode);
176         if (s->s_root)
177                 return 0;
178         
179         printk("devpts: get root dentry failed\n");
180         iput(inode);
181 fail:
182         return -ENOMEM;
183 }
184
185 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
186         int flags, const char *dev_name, void *data)
187 {
188         return get_sb_single(fs_type, flags, data, devpts_fill_super);
189 }
190
191 static struct file_system_type devpts_fs_type = {
192         .owner          = THIS_MODULE,
193         .name           = "devpts",
194         .get_sb         = devpts_get_sb,
195         .kill_sb        = kill_anon_super,
196 };
197
198 /*
199  * The normal naming convention is simply /dev/pts/<number>; this conforms
200  * to the System V naming convention
201  */
202
203 static struct dentry *get_node(int num)
204 {
205         char s[12];
206         struct dentry *root = devpts_root;
207         down(&root->d_inode->i_sem);
208         return lookup_one_len(s, root, sprintf(s, "%d", num));
209 }
210
211 #ifdef CONFIG_DEVPTS_FS_XATTR
212 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
213 {
214         int ret = -EACCES;
215
216         if (vx_check(inode->i_xid, VX_IDENT))
217                 ret = generic_permission(inode, mask, NULL);
218         return ret;
219 }
220 #endif
221
222 struct inode_operations devpts_file_inode_operations = {
223 #ifdef CONFIG_DEVPTS_FS_XATTR
224         .setxattr       = generic_setxattr,
225         .getxattr       = generic_getxattr,
226         .listxattr      = generic_listxattr,
227         .removexattr    = generic_removexattr,
228         .permission     = devpts_permission,
229 #endif
230 };
231
232 int devpts_pty_new(struct tty_struct *tty)
233 {
234         int number = tty->index;
235         struct tty_driver *driver = tty->driver;
236         dev_t device = MKDEV(driver->major, driver->minor_start+number);
237         struct dentry *dentry;
238         struct inode *inode = new_inode(devpts_mnt->mnt_sb);
239
240         /* We're supposed to be given the slave end of a pty */
241         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
242         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
243
244         if (!inode)
245                 return -ENOMEM;
246
247         inode->i_ino = number+2;
248         inode->i_blksize = 1024;
249         inode->i_uid = config.setuid ? config.uid : current->fsuid;
250         inode->i_gid = config.setgid ? config.gid : current->fsgid;
251         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
252         init_special_inode(inode, S_IFCHR|config.mode, device);
253         inode->i_xid = vx_current_xid();
254         inode->i_op = &devpts_file_inode_operations;
255         inode->u.generic_ip = tty;
256
257         dentry = get_node(number);
258         if (!IS_ERR(dentry) && !dentry->d_inode)
259                 d_instantiate(dentry, inode);
260
261         up(&devpts_root->d_inode->i_sem);
262
263         return 0;
264 }
265
266 struct tty_struct *devpts_get_tty(int number)
267 {
268         struct dentry *dentry = get_node(number);
269         struct tty_struct *tty;
270
271         tty = NULL;
272         if (!IS_ERR(dentry)) {
273                 if (dentry->d_inode)
274                         tty = dentry->d_inode->u.generic_ip;
275                 dput(dentry);
276         }
277
278         up(&devpts_root->d_inode->i_sem);
279
280         return tty;
281 }
282
283 void devpts_pty_kill(int number)
284 {
285         struct dentry *dentry = get_node(number);
286
287         if (!IS_ERR(dentry)) {
288                 struct inode *inode = dentry->d_inode;
289                 if (inode) {
290                         inode->i_nlink--;
291                         d_delete(dentry);
292                         dput(dentry);
293                 }
294                 dput(dentry);
295         }
296         up(&devpts_root->d_inode->i_sem);
297 }
298
299 static int __init init_devpts_fs(void)
300 {
301         int err = register_filesystem(&devpts_fs_type);
302         if (!err) {
303                 devpts_mnt = kern_mount(&devpts_fs_type);
304                 if (IS_ERR(devpts_mnt))
305                         err = PTR_ERR(devpts_mnt);
306         }
307         return err;
308 }
309
310 static void __exit exit_devpts_fs(void)
311 {
312         unregister_filesystem(&devpts_fs_type);
313         mntput(devpts_mnt);
314 }
315
316 module_init(init_devpts_fs)
317 module_exit(exit_devpts_fs)
318 MODULE_LICENSE("GPL");