tty: move pty count limiting into devpts
[pandora-kernel.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/slab.h>
19 #include <linux/mount.h>
20 #include <linux/tty.h>
21 #include <linux/mutex.h>
22 #include <linux/magic.h>
23 #include <linux/idr.h>
24 #include <linux/devpts_fs.h>
25 #include <linux/parser.h>
26 #include <linux/fsnotify.h>
27 #include <linux/seq_file.h>
28
29 #define DEVPTS_DEFAULT_MODE 0600
30 /*
31  * ptmx is a new node in /dev/pts and will be unused in legacy (single-
32  * instance) mode. To prevent surprises in user space, set permissions of
33  * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
34  * permissions.
35  */
36 #define DEVPTS_DEFAULT_PTMX_MODE 0000
37 #define PTMX_MINOR      2
38
39 /*
40  * sysctl support for setting limits on the number of Unix98 ptys allocated.
41  * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
42  */
43 static int pty_limit = NR_UNIX98_PTY_DEFAULT;
44 static int pty_limit_min;
45 static int pty_limit_max = NR_UNIX98_PTY_MAX;
46 static int pty_count;
47
48 static struct ctl_table pty_table[] = {
49         {
50                 .procname       = "max",
51                 .maxlen         = sizeof(int),
52                 .mode           = 0644,
53                 .data           = &pty_limit,
54                 .proc_handler   = proc_dointvec_minmax,
55                 .extra1         = &pty_limit_min,
56                 .extra2         = &pty_limit_max,
57         }, {
58                 .procname       = "nr",
59                 .maxlen         = sizeof(int),
60                 .mode           = 0444,
61                 .data           = &pty_count,
62                 .proc_handler   = proc_dointvec,
63         },
64         {}
65 };
66
67 static struct ctl_table pty_kern_table[] = {
68         {
69                 .procname       = "pty",
70                 .mode           = 0555,
71                 .child          = pty_table,
72         },
73         {}
74 };
75
76 static struct ctl_table pty_root_table[] = {
77         {
78                 .procname       = "kernel",
79                 .mode           = 0555,
80                 .child          = pty_kern_table,
81         },
82         {}
83 };
84
85 static DEFINE_MUTEX(allocated_ptys_lock);
86
87 static struct vfsmount *devpts_mnt;
88
89 struct pts_mount_opts {
90         int setuid;
91         int setgid;
92         uid_t   uid;
93         gid_t   gid;
94         umode_t mode;
95         umode_t ptmxmode;
96         int newinstance;
97 };
98
99 enum {
100         Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance,
101         Opt_err
102 };
103
104 static const match_table_t tokens = {
105         {Opt_uid, "uid=%u"},
106         {Opt_gid, "gid=%u"},
107         {Opt_mode, "mode=%o"},
108 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
109         {Opt_ptmxmode, "ptmxmode=%o"},
110         {Opt_newinstance, "newinstance"},
111 #endif
112         {Opt_err, NULL}
113 };
114
115 struct pts_fs_info {
116         struct ida allocated_ptys;
117         struct pts_mount_opts mount_opts;
118         struct dentry *ptmx_dentry;
119 };
120
121 static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
122 {
123         return sb->s_fs_info;
124 }
125
126 static inline struct super_block *pts_sb_from_inode(struct inode *inode)
127 {
128 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
129         if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
130                 return inode->i_sb;
131 #endif
132         return devpts_mnt->mnt_sb;
133 }
134
135 #define PARSE_MOUNT     0
136 #define PARSE_REMOUNT   1
137
138 /*
139  * parse_mount_options():
140  *      Set @opts to mount options specified in @data. If an option is not
141  *      specified in @data, set it to its default value. The exception is
142  *      'newinstance' option which can only be set/cleared on a mount (i.e.
143  *      cannot be changed during remount).
144  *
145  * Note: @data may be NULL (in which case all options are set to default).
146  */
147 static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
148 {
149         char *p;
150
151         opts->setuid  = 0;
152         opts->setgid  = 0;
153         opts->uid     = 0;
154         opts->gid     = 0;
155         opts->mode    = DEVPTS_DEFAULT_MODE;
156         opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
157
158         /* newinstance makes sense only on initial mount */
159         if (op == PARSE_MOUNT)
160                 opts->newinstance = 0;
161
162         while ((p = strsep(&data, ",")) != NULL) {
163                 substring_t args[MAX_OPT_ARGS];
164                 int token;
165                 int option;
166
167                 if (!*p)
168                         continue;
169
170                 token = match_token(p, tokens, args);
171                 switch (token) {
172                 case Opt_uid:
173                         if (match_int(&args[0], &option))
174                                 return -EINVAL;
175                         opts->uid = option;
176                         opts->setuid = 1;
177                         break;
178                 case Opt_gid:
179                         if (match_int(&args[0], &option))
180                                 return -EINVAL;
181                         opts->gid = option;
182                         opts->setgid = 1;
183                         break;
184                 case Opt_mode:
185                         if (match_octal(&args[0], &option))
186                                 return -EINVAL;
187                         opts->mode = option & S_IALLUGO;
188                         break;
189 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
190                 case Opt_ptmxmode:
191                         if (match_octal(&args[0], &option))
192                                 return -EINVAL;
193                         opts->ptmxmode = option & S_IALLUGO;
194                         break;
195                 case Opt_newinstance:
196                         /* newinstance makes sense only on initial mount */
197                         if (op == PARSE_MOUNT)
198                                 opts->newinstance = 1;
199                         break;
200 #endif
201                 default:
202                         printk(KERN_ERR "devpts: called with bogus options\n");
203                         return -EINVAL;
204                 }
205         }
206
207         return 0;
208 }
209
210 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
211 static int mknod_ptmx(struct super_block *sb)
212 {
213         int mode;
214         int rc = -ENOMEM;
215         struct dentry *dentry;
216         struct inode *inode;
217         struct dentry *root = sb->s_root;
218         struct pts_fs_info *fsi = DEVPTS_SB(sb);
219         struct pts_mount_opts *opts = &fsi->mount_opts;
220
221         mutex_lock(&root->d_inode->i_mutex);
222
223         /* If we have already created ptmx node, return */
224         if (fsi->ptmx_dentry) {
225                 rc = 0;
226                 goto out;
227         }
228
229         dentry = d_alloc_name(root, "ptmx");
230         if (!dentry) {
231                 printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
232                 goto out;
233         }
234
235         /*
236          * Create a new 'ptmx' node in this mount of devpts.
237          */
238         inode = new_inode(sb);
239         if (!inode) {
240                 printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
241                 dput(dentry);
242                 goto out;
243         }
244
245         inode->i_ino = 2;
246         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
247
248         mode = S_IFCHR|opts->ptmxmode;
249         init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
250
251         d_add(dentry, inode);
252
253         fsi->ptmx_dentry = dentry;
254         rc = 0;
255 out:
256         mutex_unlock(&root->d_inode->i_mutex);
257         return rc;
258 }
259
260 static void update_ptmx_mode(struct pts_fs_info *fsi)
261 {
262         struct inode *inode;
263         if (fsi->ptmx_dentry) {
264                 inode = fsi->ptmx_dentry->d_inode;
265                 inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
266         }
267 }
268 #else
269 static inline void update_ptmx_mode(struct pts_fs_info *fsi)
270 {
271        return;
272 }
273 #endif
274
275 static int devpts_remount(struct super_block *sb, int *flags, char *data)
276 {
277         int err;
278         struct pts_fs_info *fsi = DEVPTS_SB(sb);
279         struct pts_mount_opts *opts = &fsi->mount_opts;
280
281         err = parse_mount_options(data, PARSE_REMOUNT, opts);
282
283         /*
284          * parse_mount_options() restores options to default values
285          * before parsing and may have changed ptmxmode. So, update the
286          * mode in the inode too. Bogus options don't fail the remount,
287          * so do this even on error return.
288          */
289         update_ptmx_mode(fsi);
290
291         return err;
292 }
293
294 static int devpts_show_options(struct seq_file *seq, struct dentry *root)
295 {
296         struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
297         struct pts_mount_opts *opts = &fsi->mount_opts;
298
299         if (opts->setuid)
300                 seq_printf(seq, ",uid=%u", opts->uid);
301         if (opts->setgid)
302                 seq_printf(seq, ",gid=%u", opts->gid);
303         seq_printf(seq, ",mode=%03o", opts->mode);
304 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
305         seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
306 #endif
307
308         return 0;
309 }
310
311 static const struct super_operations devpts_sops = {
312         .statfs         = simple_statfs,
313         .remount_fs     = devpts_remount,
314         .show_options   = devpts_show_options,
315 };
316
317 static void *new_pts_fs_info(void)
318 {
319         struct pts_fs_info *fsi;
320
321         fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
322         if (!fsi)
323                 return NULL;
324
325         ida_init(&fsi->allocated_ptys);
326         fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
327         fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
328
329         return fsi;
330 }
331
332 static int
333 devpts_fill_super(struct super_block *s, void *data, int silent)
334 {
335         struct inode *inode;
336
337         s->s_blocksize = 1024;
338         s->s_blocksize_bits = 10;
339         s->s_magic = DEVPTS_SUPER_MAGIC;
340         s->s_op = &devpts_sops;
341         s->s_time_gran = 1;
342
343         s->s_fs_info = new_pts_fs_info();
344         if (!s->s_fs_info)
345                 goto fail;
346
347         inode = new_inode(s);
348         if (!inode)
349                 goto fail;
350         inode->i_ino = 1;
351         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
352         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
353         inode->i_op = &simple_dir_inode_operations;
354         inode->i_fop = &simple_dir_operations;
355         set_nlink(inode, 2);
356
357         s->s_root = d_alloc_root(inode);
358         if (s->s_root)
359                 return 0;
360
361         printk(KERN_ERR "devpts: get root dentry failed\n");
362         iput(inode);
363
364 fail:
365         return -ENOMEM;
366 }
367
368 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
369 static int compare_init_pts_sb(struct super_block *s, void *p)
370 {
371         if (devpts_mnt)
372                 return devpts_mnt->mnt_sb == s;
373         return 0;
374 }
375
376 /*
377  * devpts_mount()
378  *
379  *     If the '-o newinstance' mount option was specified, mount a new
380  *     (private) instance of devpts.  PTYs created in this instance are
381  *     independent of the PTYs in other devpts instances.
382  *
383  *     If the '-o newinstance' option was not specified, mount/remount the
384  *     initial kernel mount of devpts.  This type of mount gives the
385  *     legacy, single-instance semantics.
386  *
387  *     The 'newinstance' option is needed to support multiple namespace
388  *     semantics in devpts while preserving backward compatibility of the
389  *     current 'single-namespace' semantics. i.e all mounts of devpts
390  *     without the 'newinstance' mount option should bind to the initial
391  *     kernel mount, like mount_single().
392  *
393  *     Mounts with 'newinstance' option create a new, private namespace.
394  *
395  *     NOTE:
396  *
397  *     For single-mount semantics, devpts cannot use mount_single(),
398  *     because mount_single()/sget() find and use the super-block from
399  *     the most recent mount of devpts. But that recent mount may be a
400  *     'newinstance' mount and mount_single() would pick the newinstance
401  *     super-block instead of the initial super-block.
402  */
403 static struct dentry *devpts_mount(struct file_system_type *fs_type,
404         int flags, const char *dev_name, void *data)
405 {
406         int error;
407         struct pts_mount_opts opts;
408         struct super_block *s;
409
410         error = parse_mount_options(data, PARSE_MOUNT, &opts);
411         if (error)
412                 return ERR_PTR(error);
413
414         if (opts.newinstance)
415                 s = sget(fs_type, NULL, set_anon_super, NULL);
416         else
417                 s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
418
419         if (IS_ERR(s))
420                 return ERR_CAST(s);
421
422         if (!s->s_root) {
423                 s->s_flags = flags;
424                 error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
425                 if (error)
426                         goto out_undo_sget;
427                 s->s_flags |= MS_ACTIVE;
428         }
429
430         memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
431
432         error = mknod_ptmx(s);
433         if (error)
434                 goto out_undo_sget;
435
436         return dget(s->s_root);
437
438 out_undo_sget:
439         deactivate_locked_super(s);
440         return ERR_PTR(error);
441 }
442
443 #else
444 /*
445  * This supports only the legacy single-instance semantics (no
446  * multiple-instance semantics)
447  */
448 static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
449                 const char *dev_name, void *data)
450 {
451         return mount_single(fs_type, flags, data, devpts_fill_super);
452 }
453 #endif
454
455 static void devpts_kill_sb(struct super_block *sb)
456 {
457         struct pts_fs_info *fsi = DEVPTS_SB(sb);
458
459         kfree(fsi);
460         kill_litter_super(sb);
461 }
462
463 static struct file_system_type devpts_fs_type = {
464         .name           = "devpts",
465         .mount          = devpts_mount,
466         .kill_sb        = devpts_kill_sb,
467 };
468
469 /*
470  * The normal naming convention is simply /dev/pts/<number>; this conforms
471  * to the System V naming convention
472  */
473
474 int devpts_new_index(struct inode *ptmx_inode)
475 {
476         struct super_block *sb = pts_sb_from_inode(ptmx_inode);
477         struct pts_fs_info *fsi = DEVPTS_SB(sb);
478         int index;
479         int ida_ret;
480
481 retry:
482         if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
483                 return -ENOMEM;
484
485         mutex_lock(&allocated_ptys_lock);
486         ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
487         if (ida_ret < 0) {
488                 mutex_unlock(&allocated_ptys_lock);
489                 if (ida_ret == -EAGAIN)
490                         goto retry;
491                 return -EIO;
492         }
493
494         if (index >= pty_limit) {
495                 ida_remove(&fsi->allocated_ptys, index);
496                 mutex_unlock(&allocated_ptys_lock);
497                 return -EIO;
498         }
499         pty_count++;
500         mutex_unlock(&allocated_ptys_lock);
501         return index;
502 }
503
504 void devpts_kill_index(struct inode *ptmx_inode, int idx)
505 {
506         struct super_block *sb = pts_sb_from_inode(ptmx_inode);
507         struct pts_fs_info *fsi = DEVPTS_SB(sb);
508
509         mutex_lock(&allocated_ptys_lock);
510         ida_remove(&fsi->allocated_ptys, idx);
511         pty_count--;
512         mutex_unlock(&allocated_ptys_lock);
513 }
514
515 int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
516 {
517         /* tty layer puts index from devpts_new_index() in here */
518         int number = tty->index;
519         struct tty_driver *driver = tty->driver;
520         dev_t device = MKDEV(driver->major, driver->minor_start+number);
521         struct dentry *dentry;
522         struct super_block *sb = pts_sb_from_inode(ptmx_inode);
523         struct inode *inode = new_inode(sb);
524         struct dentry *root = sb->s_root;
525         struct pts_fs_info *fsi = DEVPTS_SB(sb);
526         struct pts_mount_opts *opts = &fsi->mount_opts;
527         int ret = 0;
528         char s[12];
529
530         /* We're supposed to be given the slave end of a pty */
531         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
532         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
533
534         if (!inode)
535                 return -ENOMEM;
536
537         inode->i_ino = number + 3;
538         inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
539         inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
540         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
541         init_special_inode(inode, S_IFCHR|opts->mode, device);
542         inode->i_private = tty;
543         tty->driver_data = inode;
544
545         sprintf(s, "%d", number);
546
547         mutex_lock(&root->d_inode->i_mutex);
548
549         dentry = d_alloc_name(root, s);
550         if (dentry) {
551                 d_add(dentry, inode);
552                 fsnotify_create(root->d_inode, dentry);
553         } else {
554                 iput(inode);
555                 ret = -ENOMEM;
556         }
557
558         mutex_unlock(&root->d_inode->i_mutex);
559
560         return ret;
561 }
562
563 struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
564 {
565         struct dentry *dentry;
566         struct tty_struct *tty;
567
568         BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
569
570         /* Ensure dentry has not been deleted by devpts_pty_kill() */
571         dentry = d_find_alias(pts_inode);
572         if (!dentry)
573                 return NULL;
574
575         tty = NULL;
576         if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
577                 tty = (struct tty_struct *)pts_inode->i_private;
578
579         dput(dentry);
580
581         return tty;
582 }
583
584 void devpts_pty_kill(struct tty_struct *tty)
585 {
586         struct inode *inode = tty->driver_data;
587         struct super_block *sb = pts_sb_from_inode(inode);
588         struct dentry *root = sb->s_root;
589         struct dentry *dentry;
590
591         BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
592
593         mutex_lock(&root->d_inode->i_mutex);
594
595         dentry = d_find_alias(inode);
596
597         drop_nlink(inode);
598         d_delete(dentry);
599         dput(dentry);   /* d_alloc_name() in devpts_pty_new() */
600         dput(dentry);           /* d_find_alias above */
601
602         mutex_unlock(&root->d_inode->i_mutex);
603 }
604
605 static int __init init_devpts_fs(void)
606 {
607         int err = register_filesystem(&devpts_fs_type);
608         struct ctl_table_header *table;
609
610         if (!err) {
611                 table = register_sysctl_table(pty_root_table);
612                 devpts_mnt = kern_mount(&devpts_fs_type);
613                 if (IS_ERR(devpts_mnt)) {
614                         err = PTR_ERR(devpts_mnt);
615                         unregister_filesystem(&devpts_fs_type);
616                         unregister_sysctl_table(table);
617                 }
618         }
619         return err;
620 }
621 module_init(init_devpts_fs)