blob: 00530e82673e9ead2d4798deaff4759e2750731d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070020#include <linux/mutex.h>
21#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/devpts_fs.h>
Domen Puncer7a673c62006-03-23 03:00:59 -080023#include <linux/parser.h>
Florin Malita3972b7f2007-05-08 00:24:18 -070024#include <linux/fsnotify.h>
Miklos Szeredib87a2672008-02-08 04:21:41 -080025#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define DEVPTS_SUPER_MAGIC 0x1cd1
28
Miklos Szeredib87a2672008-02-08 04:21:41 -080029#define DEVPTS_DEFAULT_MODE 0600
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +010030#define PTMX_MINOR 2
Miklos Szeredib87a2672008-02-08 04:21:41 -080031
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070032extern int pty_limit; /* Config limit on Unix98 ptys */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070033static DEFINE_MUTEX(allocated_ptys_lock);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct vfsmount *devpts_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000037struct pts_mount_opts {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 int setuid;
39 int setgid;
40 uid_t uid;
41 gid_t gid;
42 umode_t mode;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000043};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Domen Puncer7a673c62006-03-23 03:00:59 -080045enum {
46 Opt_uid, Opt_gid, Opt_mode,
47 Opt_err
48};
49
Steven Whitehousea447c092008-10-13 10:46:57 +010050static const match_table_t tokens = {
Domen Puncer7a673c62006-03-23 03:00:59 -080051 {Opt_uid, "uid=%u"},
52 {Opt_gid, "gid=%u"},
53 {Opt_mode, "mode=%o"},
54 {Opt_err, NULL}
55};
56
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +000057struct pts_fs_info {
58 struct ida allocated_ptys;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000059 struct pts_mount_opts mount_opts;
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +000060};
61
62static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
63{
64 return sb->s_fs_info;
65}
66
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +000067static inline struct super_block *pts_sb_from_inode(struct inode *inode)
68{
69 if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
70 return inode->i_sb;
71
72 return devpts_mnt->mnt_sb;
73}
74
Sukadev Bhattiprolu53af8ee2009-01-02 13:41:47 +000075static int parse_mount_options(char *data, struct pts_mount_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Domen Puncer7a673c62006-03-23 03:00:59 -080077 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000079 opts->setuid = 0;
80 opts->setgid = 0;
81 opts->uid = 0;
82 opts->gid = 0;
83 opts->mode = DEVPTS_DEFAULT_MODE;
Domen Puncer7a673c62006-03-23 03:00:59 -080084
85 while ((p = strsep(&data, ",")) != NULL) {
86 substring_t args[MAX_OPT_ARGS];
87 int token;
88 int option;
89
90 if (!*p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 continue;
Domen Puncer7a673c62006-03-23 03:00:59 -080092
93 token = match_token(p, tokens, args);
94 switch (token) {
95 case Opt_uid:
96 if (match_int(&args[0], &option))
97 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000098 opts->uid = option;
99 opts->setuid = 1;
Domen Puncer7a673c62006-03-23 03:00:59 -0800100 break;
101 case Opt_gid:
102 if (match_int(&args[0], &option))
103 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000104 opts->gid = option;
105 opts->setgid = 1;
Domen Puncer7a673c62006-03-23 03:00:59 -0800106 break;
107 case Opt_mode:
108 if (match_octal(&args[0], &option))
109 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000110 opts->mode = option & S_IALLUGO;
Domen Puncer7a673c62006-03-23 03:00:59 -0800111 break;
112 default:
113 printk(KERN_ERR "devpts: called with bogus options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EINVAL;
115 }
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return 0;
119}
120
Sukadev Bhattiprolu53af8ee2009-01-02 13:41:47 +0000121static int devpts_remount(struct super_block *sb, int *flags, char *data)
122{
123 struct pts_fs_info *fsi = DEVPTS_SB(sb);
124 struct pts_mount_opts *opts = &fsi->mount_opts;
125
126 return parse_mount_options(data, opts);
127}
128
Miklos Szeredib87a2672008-02-08 04:21:41 -0800129static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs)
130{
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000131 struct pts_fs_info *fsi = DEVPTS_SB(vfs->mnt_sb);
132 struct pts_mount_opts *opts = &fsi->mount_opts;
133
134 if (opts->setuid)
135 seq_printf(seq, ",uid=%u", opts->uid);
136 if (opts->setgid)
137 seq_printf(seq, ",gid=%u", opts->gid);
138 seq_printf(seq, ",mode=%03o", opts->mode);
Miklos Szeredib87a2672008-02-08 04:21:41 -0800139
140 return 0;
141}
142
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800143static const struct super_operations devpts_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .statfs = simple_statfs,
145 .remount_fs = devpts_remount,
Miklos Szeredib87a2672008-02-08 04:21:41 -0800146 .show_options = devpts_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000149static void *new_pts_fs_info(void)
150{
151 struct pts_fs_info *fsi;
152
153 fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
154 if (!fsi)
155 return NULL;
156
157 ida_init(&fsi->allocated_ptys);
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000158 fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000159
160 return fsi;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static int
164devpts_fill_super(struct super_block *s, void *data, int silent)
165{
166 struct inode * inode;
167
168 s->s_blocksize = 1024;
169 s->s_blocksize_bits = 10;
170 s->s_magic = DEVPTS_SUPER_MAGIC;
171 s->s_op = &devpts_sops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 s->s_time_gran = 1;
173
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000174 s->s_fs_info = new_pts_fs_info();
175 if (!s->s_fs_info)
176 goto fail;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 inode = new_inode(s);
179 if (!inode)
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000180 goto free_fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 inode->i_ino = 1;
182 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
183 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 inode->i_uid = inode->i_gid = 0;
185 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
186 inode->i_op = &simple_dir_inode_operations;
187 inode->i_fop = &simple_dir_operations;
188 inode->i_nlink = 2;
189
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000190 s->s_root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (s->s_root)
192 return 0;
193
194 printk("devpts: get root dentry failed\n");
195 iput(inode);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000196
197free_fsi:
198 kfree(s->s_fs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199fail:
200 return -ENOMEM;
201}
202
David Howells454e2392006-06-23 02:02:57 -0700203static int devpts_get_sb(struct file_system_type *fs_type,
204 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Howells454e2392006-06-23 02:02:57 -0700206 return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000209static void devpts_kill_sb(struct super_block *sb)
210{
211 struct pts_fs_info *fsi = DEVPTS_SB(sb);
212
213 kfree(fsi);
214 kill_anon_super(sb);
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static struct file_system_type devpts_fs_type = {
218 .owner = THIS_MODULE,
219 .name = "devpts",
220 .get_sb = devpts_get_sb,
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000221 .kill_sb = devpts_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224/*
225 * The normal naming convention is simply /dev/pts/<number>; this conforms
226 * to the System V naming convention
227 */
228
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100229int devpts_new_index(struct inode *ptmx_inode)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700230{
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000231 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
232 struct pts_fs_info *fsi = DEVPTS_SB(sb);
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700233 int index;
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400234 int ida_ret;
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700235
236retry:
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000237 if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) {
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700238 return -ENOMEM;
239 }
240
241 mutex_lock(&allocated_ptys_lock);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000242 ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400243 if (ida_ret < 0) {
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700244 mutex_unlock(&allocated_ptys_lock);
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400245 if (ida_ret == -EAGAIN)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700246 goto retry;
247 return -EIO;
248 }
249
250 if (index >= pty_limit) {
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000251 ida_remove(&fsi->allocated_ptys, index);
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700252 mutex_unlock(&allocated_ptys_lock);
253 return -EIO;
254 }
255 mutex_unlock(&allocated_ptys_lock);
256 return index;
257}
258
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100259void devpts_kill_index(struct inode *ptmx_inode, int idx)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700260{
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000261 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
262 struct pts_fs_info *fsi = DEVPTS_SB(sb);
263
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700264 mutex_lock(&allocated_ptys_lock);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000265 ida_remove(&fsi->allocated_ptys, idx);
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700266 mutex_unlock(&allocated_ptys_lock);
267}
268
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100269int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700271 int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct tty_driver *driver = tty->driver;
273 dev_t device = MKDEV(driver->major, driver->minor_start+number);
274 struct dentry *dentry;
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000275 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
276 struct inode *inode = new_inode(sb);
277 struct dentry *root = sb->s_root;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000278 struct pts_fs_info *fsi = DEVPTS_SB(sb);
279 struct pts_mount_opts *opts = &fsi->mount_opts;
Sukadev Bhattiprolu89a52e12008-10-13 10:43:18 +0100280 char s[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* We're supposed to be given the slave end of a pty */
283 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
284 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
285
286 if (!inode)
287 return -ENOMEM;
288
289 inode->i_ino = number+2;
David Howellsec4c2aa2008-11-14 10:38:49 +1100290 inode->i_uid = config.setuid ? config.uid : current_fsuid();
291 inode->i_gid = config.setgid ? config.gid : current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000293 init_special_inode(inode, S_IFCHR|opts->mode, device);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700294 inode->i_private = tty;
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100295 tty->driver_data = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Sukadev Bhattiprolu89a52e12008-10-13 10:43:18 +0100297 sprintf(s, "%d", number);
298
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000299 mutex_lock(&root->d_inode->i_mutex);
Sukadev Bhattiprolu89a52e12008-10-13 10:43:18 +0100300
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000301 dentry = d_alloc_name(root, s);
Sukadev Bhattiprolu89a52e12008-10-13 10:43:18 +0100302 if (!IS_ERR(dentry)) {
303 d_add(dentry, inode);
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000304 fsnotify_create(root->d_inode, dentry);
Florin Malita3972b7f2007-05-08 00:24:18 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000307 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return 0;
310}
311
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100312struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +0100314 BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +0100316 if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
317 return (struct tty_struct *)pts_inode->i_private;
318 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100321void devpts_pty_kill(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100323 struct inode *inode = tty->driver_data;
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000324 struct super_block *sb = pts_sb_from_inode(inode);
325 struct dentry *root = sb->s_root;
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100326 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100328 BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
329
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000330 mutex_lock(&root->d_inode->i_mutex);
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100331
332 dentry = d_find_alias(inode);
333 if (dentry && !IS_ERR(dentry)) {
334 inode->i_nlink--;
335 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dput(dentry);
337 }
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100338
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000339 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static int __init init_devpts_fs(void)
343{
344 int err = register_filesystem(&devpts_fs_type);
345 if (!err) {
346 devpts_mnt = kern_mount(&devpts_fs_type);
347 if (IS_ERR(devpts_mnt))
348 err = PTR_ERR(devpts_mnt);
349 }
350 return err;
351}
352
353static void __exit exit_devpts_fs(void)
354{
355 unregister_filesystem(&devpts_fs_type);
356 mntput(devpts_mnt);
357}
358
359module_init(init_devpts_fs)
360module_exit(exit_devpts_fs)
361MODULE_LICENSE("GPL");