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/idr.h>
22 #include <linux/devpts_fs.h>
23 #include <linux/parser.h>
24 #include <linux/fsnotify.h>
25 #include <linux/seq_file.h>
27 #define DEVPTS_SUPER_MAGIC 0x1cd1
29 #define DEVPTS_DEFAULT_MODE 0600
32 extern int pty_limit
; /* Config limit on Unix98 ptys */
33 static DEFINE_IDA(allocated_ptys
);
34 static DEFINE_MUTEX(allocated_ptys_lock
);
36 static struct vfsmount
*devpts_mnt
;
37 static struct dentry
*devpts_root
;
45 } config
= {.mode
= DEVPTS_DEFAULT_MODE
};
48 Opt_uid
, Opt_gid
, Opt_mode
,
52 static match_table_t tokens
= {
55 {Opt_mode
, "mode=%o"},
59 static int devpts_remount(struct super_block
*sb
, int *flags
, char *data
)
67 config
.mode
= DEVPTS_DEFAULT_MODE
;
69 while ((p
= strsep(&data
, ",")) != NULL
) {
70 substring_t args
[MAX_OPT_ARGS
];
77 token
= match_token(p
, tokens
, args
);
80 if (match_int(&args
[0], &option
))
86 if (match_int(&args
[0], &option
))
92 if (match_octal(&args
[0], &option
))
94 config
.mode
= option
& S_IALLUGO
;
97 printk(KERN_ERR
"devpts: called with bogus options\n");
105 static int devpts_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
108 seq_printf(seq
, ",uid=%u", config
.uid
);
110 seq_printf(seq
, ",gid=%u", config
.gid
);
111 seq_printf(seq
, ",mode=%03o", config
.mode
);
116 static const struct super_operations devpts_sops
= {
117 .statfs
= simple_statfs
,
118 .remount_fs
= devpts_remount
,
119 .show_options
= devpts_show_options
,
123 devpts_fill_super(struct super_block
*s
, void *data
, int silent
)
125 struct inode
* inode
;
127 s
->s_blocksize
= 1024;
128 s
->s_blocksize_bits
= 10;
129 s
->s_magic
= DEVPTS_SUPER_MAGIC
;
130 s
->s_op
= &devpts_sops
;
133 inode
= new_inode(s
);
137 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
139 inode
->i_uid
= inode
->i_gid
= 0;
140 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
;
141 inode
->i_op
= &simple_dir_inode_operations
;
142 inode
->i_fop
= &simple_dir_operations
;
145 devpts_root
= s
->s_root
= d_alloc_root(inode
);
149 printk("devpts: get root dentry failed\n");
155 static int devpts_get_sb(struct file_system_type
*fs_type
,
156 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
158 return get_sb_single(fs_type
, flags
, data
, devpts_fill_super
, mnt
);
161 static struct file_system_type devpts_fs_type
= {
162 .owner
= THIS_MODULE
,
164 .get_sb
= devpts_get_sb
,
165 .kill_sb
= kill_anon_super
,
169 * The normal naming convention is simply /dev/pts/<number>; this conforms
170 * to the System V naming convention
173 int devpts_new_index(struct inode
*ptmx_inode
)
179 if (!ida_pre_get(&allocated_ptys
, GFP_KERNEL
)) {
183 mutex_lock(&allocated_ptys_lock
);
184 ida_ret
= ida_get_new(&allocated_ptys
, &index
);
186 mutex_unlock(&allocated_ptys_lock
);
187 if (ida_ret
== -EAGAIN
)
192 if (index
>= pty_limit
) {
193 ida_remove(&allocated_ptys
, index
);
194 mutex_unlock(&allocated_ptys_lock
);
197 mutex_unlock(&allocated_ptys_lock
);
201 void devpts_kill_index(struct inode
*ptmx_inode
, int idx
)
203 mutex_lock(&allocated_ptys_lock
);
204 ida_remove(&allocated_ptys
, idx
);
205 mutex_unlock(&allocated_ptys_lock
);
208 int devpts_pty_new(struct inode
*ptmx_inode
, struct tty_struct
*tty
)
210 int number
= tty
->index
; /* tty layer puts index from devpts_new_index() in here */
211 struct tty_driver
*driver
= tty
->driver
;
212 dev_t device
= MKDEV(driver
->major
, driver
->minor_start
+number
);
213 struct dentry
*dentry
;
214 struct inode
*inode
= new_inode(devpts_mnt
->mnt_sb
);
217 /* We're supposed to be given the slave end of a pty */
218 BUG_ON(driver
->type
!= TTY_DRIVER_TYPE_PTY
);
219 BUG_ON(driver
->subtype
!= PTY_TYPE_SLAVE
);
224 inode
->i_ino
= number
+2;
225 inode
->i_uid
= config
.setuid
? config
.uid
: current
->fsuid
;
226 inode
->i_gid
= config
.setgid
? config
.gid
: current
->fsgid
;
227 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
228 init_special_inode(inode
, S_IFCHR
|config
.mode
, device
);
229 inode
->i_private
= tty
;
230 tty
->driver_data
= inode
;
232 sprintf(s
, "%d", number
);
234 mutex_lock(&devpts_root
->d_inode
->i_mutex
);
236 dentry
= d_alloc_name(devpts_root
, s
);
237 if (!IS_ERR(dentry
)) {
238 d_add(dentry
, inode
);
239 fsnotify_create(devpts_root
->d_inode
, dentry
);
242 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
247 struct tty_struct
*devpts_get_tty(struct inode
*pts_inode
, int number
)
249 BUG_ON(pts_inode
->i_rdev
== MKDEV(TTYAUX_MAJOR
, PTMX_MINOR
));
251 if (pts_inode
->i_sb
->s_magic
== DEVPTS_SUPER_MAGIC
)
252 return (struct tty_struct
*)pts_inode
->i_private
;
256 void devpts_pty_kill(struct tty_struct
*tty
)
258 struct inode
*inode
= tty
->driver_data
;
259 struct dentry
*dentry
;
261 BUG_ON(inode
->i_rdev
== MKDEV(TTYAUX_MAJOR
, PTMX_MINOR
));
263 mutex_lock(&devpts_root
->d_inode
->i_mutex
);
265 dentry
= d_find_alias(inode
);
266 if (dentry
&& !IS_ERR(dentry
)) {
272 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
275 static int __init
init_devpts_fs(void)
277 int err
= register_filesystem(&devpts_fs_type
);
279 devpts_mnt
= kern_mount(&devpts_fs_type
);
280 if (IS_ERR(devpts_mnt
))
281 err
= PTR_ERR(devpts_mnt
);
286 static void __exit
exit_devpts_fs(void)
288 unregister_filesystem(&devpts_fs_type
);
292 module_init(init_devpts_fs
)
293 module_exit(exit_devpts_fs
)
294 MODULE_LICENSE("GPL");