1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/devpts/inode.c
5 * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
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.
11 * ------------------------------------------------------------------------- */
13 #include <linux/module.h>
14 #include <linux/init.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>
29 #define DEVPTS_DEFAULT_MODE 0600
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
36 #define DEVPTS_DEFAULT_PTMX_MODE 0000
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.
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;
48 static struct ctl_table pty_table[] = {
51 .maxlen = sizeof(int),
54 .proc_handler = proc_dointvec_minmax,
55 .extra1 = &pty_limit_min,
56 .extra2 = &pty_limit_max,
59 .maxlen = sizeof(int),
62 .proc_handler = proc_dointvec,
67 static struct ctl_table pty_kern_table[] = {
76 static struct ctl_table pty_root_table[] = {
80 .child = pty_kern_table,
85 static DEFINE_MUTEX(allocated_ptys_lock);
87 static struct vfsmount *devpts_mnt;
89 struct pts_mount_opts {
100 Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance,
104 static const match_table_t tokens = {
107 {Opt_mode, "mode=%o"},
108 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
109 {Opt_ptmxmode, "ptmxmode=%o"},
110 {Opt_newinstance, "newinstance"},
116 struct ida allocated_ptys;
117 struct pts_mount_opts mount_opts;
118 struct dentry *ptmx_dentry;
121 static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
123 return sb->s_fs_info;
126 static inline struct super_block *pts_sb_from_inode(struct inode *inode)
128 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
129 if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
132 return devpts_mnt->mnt_sb;
135 #define PARSE_MOUNT 0
136 #define PARSE_REMOUNT 1
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).
145 * Note: @data may be NULL (in which case all options are set to default).
147 static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
155 opts->mode = DEVPTS_DEFAULT_MODE;
156 opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
158 /* newinstance makes sense only on initial mount */
159 if (op == PARSE_MOUNT)
160 opts->newinstance = 0;
162 while ((p = strsep(&data, ",")) != NULL) {
163 substring_t args[MAX_OPT_ARGS];
170 token = match_token(p, tokens, args);
173 if (match_int(&args[0], &option))
179 if (match_int(&args[0], &option))
185 if (match_octal(&args[0], &option))
187 opts->mode = option & S_IALLUGO;
189 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
191 if (match_octal(&args[0], &option))
193 opts->ptmxmode = option & S_IALLUGO;
195 case Opt_newinstance:
196 /* newinstance makes sense only on initial mount */
197 if (op == PARSE_MOUNT)
198 opts->newinstance = 1;
202 printk(KERN_ERR "devpts: called with bogus options\n");
210 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
211 static int mknod_ptmx(struct super_block *sb)
215 struct dentry *dentry;
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;
221 mutex_lock(&root->d_inode->i_mutex);
223 /* If we have already created ptmx node, return */
224 if (fsi->ptmx_dentry) {
229 dentry = d_alloc_name(root, "ptmx");
231 printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
236 * Create a new 'ptmx' node in this mount of devpts.
238 inode = new_inode(sb);
240 printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
246 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
248 mode = S_IFCHR|opts->ptmxmode;
249 init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
251 d_add(dentry, inode);
253 fsi->ptmx_dentry = dentry;
256 mutex_unlock(&root->d_inode->i_mutex);
260 static void update_ptmx_mode(struct pts_fs_info *fsi)
263 if (fsi->ptmx_dentry) {
264 inode = fsi->ptmx_dentry->d_inode;
265 inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
269 static inline void update_ptmx_mode(struct pts_fs_info *fsi)
275 static int devpts_remount(struct super_block *sb, int *flags, char *data)
278 struct pts_fs_info *fsi = DEVPTS_SB(sb);
279 struct pts_mount_opts *opts = &fsi->mount_opts;
281 err = parse_mount_options(data, PARSE_REMOUNT, opts);
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.
289 update_ptmx_mode(fsi);
294 static int devpts_show_options(struct seq_file *seq, struct dentry *root)
296 struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
297 struct pts_mount_opts *opts = &fsi->mount_opts;
300 seq_printf(seq, ",uid=%u", opts->uid);
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);
311 static const struct super_operations devpts_sops = {
312 .statfs = simple_statfs,
313 .remount_fs = devpts_remount,
314 .show_options = devpts_show_options,
317 static void *new_pts_fs_info(void)
319 struct pts_fs_info *fsi;
321 fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
325 ida_init(&fsi->allocated_ptys);
326 fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
327 fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
333 devpts_fill_super(struct super_block *s, void *data, int silent)
337 s->s_blocksize = 1024;
338 s->s_blocksize_bits = 10;
339 s->s_magic = DEVPTS_SUPER_MAGIC;
340 s->s_op = &devpts_sops;
343 s->s_fs_info = new_pts_fs_info();
347 inode = new_inode(s);
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;
357 s->s_root = d_alloc_root(inode);
361 printk(KERN_ERR "devpts: get root dentry failed\n");
368 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
369 static int compare_init_pts_sb(struct super_block *s, void *p)
372 return devpts_mnt->mnt_sb == s;
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.
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.
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().
393 * Mounts with 'newinstance' option create a new, private namespace.
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.
403 static struct dentry *devpts_mount(struct file_system_type *fs_type,
404 int flags, const char *dev_name, void *data)
407 struct pts_mount_opts opts;
408 struct super_block *s;
410 error = parse_mount_options(data, PARSE_MOUNT, &opts);
412 return ERR_PTR(error);
414 if (opts.newinstance)
415 s = sget(fs_type, NULL, set_anon_super, NULL);
417 s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
424 error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
427 s->s_flags |= MS_ACTIVE;
430 memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
432 error = mknod_ptmx(s);
436 return dget(s->s_root);
439 deactivate_locked_super(s);
440 return ERR_PTR(error);
445 * This supports only the legacy single-instance semantics (no
446 * multiple-instance semantics)
448 static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
449 const char *dev_name, void *data)
451 return mount_single(fs_type, flags, data, devpts_fill_super);
455 static void devpts_kill_sb(struct super_block *sb)
457 struct pts_fs_info *fsi = DEVPTS_SB(sb);
460 kill_litter_super(sb);
463 static struct file_system_type devpts_fs_type = {
465 .mount = devpts_mount,
466 .kill_sb = devpts_kill_sb,
470 * The normal naming convention is simply /dev/pts/<number>; this conforms
471 * to the System V naming convention
474 int devpts_new_index(struct inode *ptmx_inode)
476 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
477 struct pts_fs_info *fsi = DEVPTS_SB(sb);
482 if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
485 mutex_lock(&allocated_ptys_lock);
486 ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
488 mutex_unlock(&allocated_ptys_lock);
489 if (ida_ret == -EAGAIN)
494 if (index >= pty_limit) {
495 ida_remove(&fsi->allocated_ptys, index);
496 mutex_unlock(&allocated_ptys_lock);
500 mutex_unlock(&allocated_ptys_lock);
504 void devpts_kill_index(struct inode *ptmx_inode, int idx)
506 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
507 struct pts_fs_info *fsi = DEVPTS_SB(sb);
509 mutex_lock(&allocated_ptys_lock);
510 ida_remove(&fsi->allocated_ptys, idx);
512 mutex_unlock(&allocated_ptys_lock);
515 int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
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;
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);
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;
545 sprintf(s, "%d", number);
547 mutex_lock(&root->d_inode->i_mutex);
549 dentry = d_alloc_name(root, s);
551 d_add(dentry, inode);
552 fsnotify_create(root->d_inode, dentry);
558 mutex_unlock(&root->d_inode->i_mutex);
563 struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
565 struct dentry *dentry;
566 struct tty_struct *tty;
568 BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
570 /* Ensure dentry has not been deleted by devpts_pty_kill() */
571 dentry = d_find_alias(pts_inode);
576 if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
577 tty = (struct tty_struct *)pts_inode->i_private;
584 void devpts_pty_kill(struct tty_struct *tty)
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;
591 BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
593 mutex_lock(&root->d_inode->i_mutex);
595 dentry = d_find_alias(inode);
599 dput(dentry); /* d_alloc_name() in devpts_pty_new() */
600 dput(dentry); /* d_find_alias above */
602 mutex_unlock(&root->d_inode->i_mutex);
605 static int __init init_devpts_fs(void)
607 int err = register_filesystem(&devpts_fs_type);
608 struct ctl_table_header *table;
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);
621 module_init(init_devpts_fs)