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/mount.h>
19 #include <linux/tty.h>
20 #include <linux/mutex.h>
21 #include <linux/magic.h>
22 #include <linux/idr.h>
23 #include <linux/devpts_fs.h>
24 #include <linux/parser.h>
25 #include <linux/fsnotify.h>
26 #include <linux/seq_file.h>
28 #define DEVPTS_DEFAULT_MODE 0600
30 * ptmx is a new node in /dev/pts and will be unused in legacy (single-
31 * instance) mode. To prevent surprises in user space, set permissions of
32 * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
35 #define DEVPTS_DEFAULT_PTMX_MODE 0000
38 extern int pty_limit
; /* Config limit on Unix98 ptys */
39 static DEFINE_MUTEX(allocated_ptys_lock
);
41 static struct vfsmount
*devpts_mnt
;
43 struct pts_mount_opts
{
54 Opt_uid
, Opt_gid
, Opt_mode
, Opt_ptmxmode
, Opt_newinstance
,
58 static const match_table_t tokens
= {
61 {Opt_mode
, "mode=%o"},
62 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
63 {Opt_ptmxmode
, "ptmxmode=%o"},
64 {Opt_newinstance
, "newinstance"},
70 struct ida allocated_ptys
;
71 struct pts_mount_opts mount_opts
;
72 struct dentry
*ptmx_dentry
;
75 static inline struct pts_fs_info
*DEVPTS_SB(struct super_block
*sb
)
80 static inline struct super_block
*pts_sb_from_inode(struct inode
*inode
)
82 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
83 if (inode
->i_sb
->s_magic
== DEVPTS_SUPER_MAGIC
)
86 return devpts_mnt
->mnt_sb
;
90 #define PARSE_REMOUNT 1
93 * parse_mount_options():
94 * Set @opts to mount options specified in @data. If an option is not
95 * specified in @data, set it to its default value. The exception is
96 * 'newinstance' option which can only be set/cleared on a mount (i.e.
97 * cannot be changed during remount).
99 * Note: @data may be NULL (in which case all options are set to default).
101 static int parse_mount_options(char *data
, int op
, struct pts_mount_opts
*opts
)
109 opts
->mode
= DEVPTS_DEFAULT_MODE
;
110 opts
->ptmxmode
= DEVPTS_DEFAULT_PTMX_MODE
;
112 /* newinstance makes sense only on initial mount */
113 if (op
== PARSE_MOUNT
)
114 opts
->newinstance
= 0;
116 while ((p
= strsep(&data
, ",")) != NULL
) {
117 substring_t args
[MAX_OPT_ARGS
];
124 token
= match_token(p
, tokens
, args
);
127 if (match_int(&args
[0], &option
))
133 if (match_int(&args
[0], &option
))
139 if (match_octal(&args
[0], &option
))
141 opts
->mode
= option
& S_IALLUGO
;
143 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
145 if (match_octal(&args
[0], &option
))
147 opts
->ptmxmode
= option
& S_IALLUGO
;
149 case Opt_newinstance
:
150 /* newinstance makes sense only on initial mount */
151 if (op
== PARSE_MOUNT
)
152 opts
->newinstance
= 1;
156 printk(KERN_ERR
"devpts: called with bogus options\n");
164 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
165 static int mknod_ptmx(struct super_block
*sb
)
169 struct dentry
*dentry
;
171 struct dentry
*root
= sb
->s_root
;
172 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
173 struct pts_mount_opts
*opts
= &fsi
->mount_opts
;
175 mutex_lock(&root
->d_inode
->i_mutex
);
177 /* If we have already created ptmx node, return */
178 if (fsi
->ptmx_dentry
) {
183 dentry
= d_alloc_name(root
, "ptmx");
185 printk(KERN_NOTICE
"Unable to alloc dentry for ptmx node\n");
190 * Create a new 'ptmx' node in this mount of devpts.
192 inode
= new_inode(sb
);
194 printk(KERN_ERR
"Unable to alloc inode for ptmx node\n");
200 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
202 mode
= S_IFCHR
|opts
->ptmxmode
;
203 init_special_inode(inode
, mode
, MKDEV(TTYAUX_MAJOR
, 2));
205 d_add(dentry
, inode
);
207 fsi
->ptmx_dentry
= dentry
;
210 mutex_unlock(&root
->d_inode
->i_mutex
);
214 static void update_ptmx_mode(struct pts_fs_info
*fsi
)
217 if (fsi
->ptmx_dentry
) {
218 inode
= fsi
->ptmx_dentry
->d_inode
;
219 inode
->i_mode
= S_IFCHR
|fsi
->mount_opts
.ptmxmode
;
223 static inline void update_ptmx_mode(struct pts_fs_info
*fsi
)
229 static int devpts_remount(struct super_block
*sb
, int *flags
, char *data
)
232 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
233 struct pts_mount_opts
*opts
= &fsi
->mount_opts
;
235 err
= parse_mount_options(data
, PARSE_REMOUNT
, opts
);
238 * parse_mount_options() restores options to default values
239 * before parsing and may have changed ptmxmode. So, update the
240 * mode in the inode too. Bogus options don't fail the remount,
241 * so do this even on error return.
243 update_ptmx_mode(fsi
);
248 static int devpts_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
250 struct pts_fs_info
*fsi
= DEVPTS_SB(vfs
->mnt_sb
);
251 struct pts_mount_opts
*opts
= &fsi
->mount_opts
;
254 seq_printf(seq
, ",uid=%u", opts
->uid
);
256 seq_printf(seq
, ",gid=%u", opts
->gid
);
257 seq_printf(seq
, ",mode=%03o", opts
->mode
);
258 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
259 seq_printf(seq
, ",ptmxmode=%03o", opts
->ptmxmode
);
265 static const struct super_operations devpts_sops
= {
266 .statfs
= simple_statfs
,
267 .remount_fs
= devpts_remount
,
268 .show_options
= devpts_show_options
,
271 static void *new_pts_fs_info(void)
273 struct pts_fs_info
*fsi
;
275 fsi
= kzalloc(sizeof(struct pts_fs_info
), GFP_KERNEL
);
279 ida_init(&fsi
->allocated_ptys
);
280 fsi
->mount_opts
.mode
= DEVPTS_DEFAULT_MODE
;
281 fsi
->mount_opts
.ptmxmode
= DEVPTS_DEFAULT_PTMX_MODE
;
287 devpts_fill_super(struct super_block
*s
, void *data
, int silent
)
291 s
->s_blocksize
= 1024;
292 s
->s_blocksize_bits
= 10;
293 s
->s_magic
= DEVPTS_SUPER_MAGIC
;
294 s
->s_op
= &devpts_sops
;
297 s
->s_fs_info
= new_pts_fs_info();
301 inode
= new_inode(s
);
305 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
306 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
;
307 inode
->i_op
= &simple_dir_inode_operations
;
308 inode
->i_fop
= &simple_dir_operations
;
311 s
->s_root
= d_alloc_root(inode
);
315 printk(KERN_ERR
"devpts: get root dentry failed\n");
324 #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
325 static int compare_init_pts_sb(struct super_block
*s
, void *p
)
328 return devpts_mnt
->mnt_sb
== s
;
335 * If the '-o newinstance' mount option was specified, mount a new
336 * (private) instance of devpts. PTYs created in this instance are
337 * independent of the PTYs in other devpts instances.
339 * If the '-o newinstance' option was not specified, mount/remount the
340 * initial kernel mount of devpts. This type of mount gives the
341 * legacy, single-instance semantics.
343 * The 'newinstance' option is needed to support multiple namespace
344 * semantics in devpts while preserving backward compatibility of the
345 * current 'single-namespace' semantics. i.e all mounts of devpts
346 * without the 'newinstance' mount option should bind to the initial
347 * kernel mount, like get_sb_single().
349 * Mounts with 'newinstance' option create a new, private namespace.
353 * For single-mount semantics, devpts cannot use get_sb_single(),
354 * because get_sb_single()/sget() find and use the super-block from
355 * the most recent mount of devpts. But that recent mount may be a
356 * 'newinstance' mount and get_sb_single() would pick the newinstance
357 * super-block instead of the initial super-block.
359 static int devpts_get_sb(struct file_system_type
*fs_type
,
360 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
363 struct pts_mount_opts opts
;
364 struct super_block
*s
;
366 error
= parse_mount_options(data
, PARSE_MOUNT
, &opts
);
370 if (opts
.newinstance
)
371 s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
373 s
= sget(fs_type
, compare_init_pts_sb
, set_anon_super
, NULL
);
380 error
= devpts_fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
383 s
->s_flags
|= MS_ACTIVE
;
386 simple_set_mnt(mnt
, s
);
388 memcpy(&(DEVPTS_SB(s
))->mount_opts
, &opts
, sizeof(opts
));
390 error
= mknod_ptmx(s
);
397 dput(s
->s_root
); /* undo dget() in simple_set_mnt() */
400 deactivate_locked_super(s
);
406 * This supports only the legacy single-instance semantics (no
407 * multiple-instance semantics)
409 static int devpts_get_sb(struct file_system_type
*fs_type
, int flags
,
410 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
412 return get_sb_single(fs_type
, flags
, data
, devpts_fill_super
, mnt
);
416 static void devpts_kill_sb(struct super_block
*sb
)
418 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
421 kill_litter_super(sb
);
424 static struct file_system_type devpts_fs_type
= {
426 .get_sb
= devpts_get_sb
,
427 .kill_sb
= devpts_kill_sb
,
431 * The normal naming convention is simply /dev/pts/<number>; this conforms
432 * to the System V naming convention
435 int devpts_new_index(struct inode
*ptmx_inode
)
437 struct super_block
*sb
= pts_sb_from_inode(ptmx_inode
);
438 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
443 if (!ida_pre_get(&fsi
->allocated_ptys
, GFP_KERNEL
))
446 mutex_lock(&allocated_ptys_lock
);
447 ida_ret
= ida_get_new(&fsi
->allocated_ptys
, &index
);
449 mutex_unlock(&allocated_ptys_lock
);
450 if (ida_ret
== -EAGAIN
)
455 if (index
>= pty_limit
) {
456 ida_remove(&fsi
->allocated_ptys
, index
);
457 mutex_unlock(&allocated_ptys_lock
);
460 mutex_unlock(&allocated_ptys_lock
);
464 void devpts_kill_index(struct inode
*ptmx_inode
, int idx
)
466 struct super_block
*sb
= pts_sb_from_inode(ptmx_inode
);
467 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
469 mutex_lock(&allocated_ptys_lock
);
470 ida_remove(&fsi
->allocated_ptys
, idx
);
471 mutex_unlock(&allocated_ptys_lock
);
474 int devpts_pty_new(struct inode
*ptmx_inode
, struct tty_struct
*tty
)
476 /* tty layer puts index from devpts_new_index() in here */
477 int number
= tty
->index
;
478 struct tty_driver
*driver
= tty
->driver
;
479 dev_t device
= MKDEV(driver
->major
, driver
->minor_start
+number
);
480 struct dentry
*dentry
;
481 struct super_block
*sb
= pts_sb_from_inode(ptmx_inode
);
482 struct inode
*inode
= new_inode(sb
);
483 struct dentry
*root
= sb
->s_root
;
484 struct pts_fs_info
*fsi
= DEVPTS_SB(sb
);
485 struct pts_mount_opts
*opts
= &fsi
->mount_opts
;
488 /* We're supposed to be given the slave end of a pty */
489 BUG_ON(driver
->type
!= TTY_DRIVER_TYPE_PTY
);
490 BUG_ON(driver
->subtype
!= PTY_TYPE_SLAVE
);
495 inode
->i_ino
= number
+ 3;
496 inode
->i_uid
= opts
->setuid
? opts
->uid
: current_fsuid();
497 inode
->i_gid
= opts
->setgid
? opts
->gid
: current_fsgid();
498 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
499 init_special_inode(inode
, S_IFCHR
|opts
->mode
, device
);
500 inode
->i_private
= tty
;
501 tty
->driver_data
= inode
;
503 sprintf(s
, "%d", number
);
505 mutex_lock(&root
->d_inode
->i_mutex
);
507 dentry
= d_alloc_name(root
, s
);
508 if (!IS_ERR(dentry
)) {
509 d_add(dentry
, inode
);
510 fsnotify_create(root
->d_inode
, dentry
);
513 mutex_unlock(&root
->d_inode
->i_mutex
);
518 struct tty_struct
*devpts_get_tty(struct inode
*pts_inode
, int number
)
520 struct dentry
*dentry
;
521 struct tty_struct
*tty
;
523 BUG_ON(pts_inode
->i_rdev
== MKDEV(TTYAUX_MAJOR
, PTMX_MINOR
));
525 /* Ensure dentry has not been deleted by devpts_pty_kill() */
526 dentry
= d_find_alias(pts_inode
);
531 if (pts_inode
->i_sb
->s_magic
== DEVPTS_SUPER_MAGIC
)
532 tty
= (struct tty_struct
*)pts_inode
->i_private
;
539 void devpts_pty_kill(struct tty_struct
*tty
)
541 struct inode
*inode
= tty
->driver_data
;
542 struct super_block
*sb
= pts_sb_from_inode(inode
);
543 struct dentry
*root
= sb
->s_root
;
544 struct dentry
*dentry
;
546 BUG_ON(inode
->i_rdev
== MKDEV(TTYAUX_MAJOR
, PTMX_MINOR
));
548 mutex_lock(&root
->d_inode
->i_mutex
);
550 dentry
= d_find_alias(inode
);
557 dput(dentry
); /* d_alloc_name() in devpts_pty_new() */
560 dput(dentry
); /* d_find_alias above */
562 mutex_unlock(&root
->d_inode
->i_mutex
);
565 static int __init
init_devpts_fs(void)
567 int err
= register_filesystem(&devpts_fs_type
);
569 devpts_mnt
= kern_mount(&devpts_fs_type
);
570 if (IS_ERR(devpts_mnt
)) {
571 err
= PTR_ERR(devpts_mnt
);
572 unregister_filesystem(&devpts_fs_type
);
577 module_init(init_devpts_fs
)