blob: c2c7317d56872efcdc71fe9588b2598c07d2fc01 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
20#include <linux/tty.h>
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070021#include <linux/mutex.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070022#include <linux/magic.h>
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070023#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/devpts_fs.h>
Domen Puncer7a673c62006-03-23 03:00:59 -080025#include <linux/parser.h>
Florin Malita3972b7f2007-05-08 00:24:18 -070026#include <linux/fsnotify.h>
Miklos Szeredib87a2672008-02-08 04:21:41 -080027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Miklos Szeredib87a2672008-02-08 04:21:41 -080029#define DEVPTS_DEFAULT_MODE 0600
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +000030/*
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
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +010037#define PTMX_MINOR 2
Miklos Szeredib87a2672008-02-08 04:21:41 -080038
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +040039/*
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 */
43static int pty_limit = NR_UNIX98_PTY_DEFAULT;
44static int pty_limit_min;
45static int pty_limit_max = NR_UNIX98_PTY_MAX;
46static int pty_count;
47
48static 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
67static struct ctl_table pty_kern_table[] = {
68 {
69 .procname = "pty",
70 .mode = 0555,
71 .child = pty_table,
72 },
73 {}
74};
75
76static struct ctl_table pty_root_table[] = {
77 {
78 .procname = "kernel",
79 .mode = 0555,
80 .child = pty_kern_table,
81 },
82 {}
83};
84
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -070085static DEFINE_MUTEX(allocated_ptys_lock);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct vfsmount *devpts_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000089struct pts_mount_opts {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int setuid;
91 int setgid;
92 uid_t uid;
93 gid_t gid;
94 umode_t mode;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +000095 umode_t ptmxmode;
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +000096 int newinstance;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +000097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Domen Puncer7a673c62006-03-23 03:00:59 -080099enum {
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000100 Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance,
Domen Puncer7a673c62006-03-23 03:00:59 -0800101 Opt_err
102};
103
Steven Whitehousea447c092008-10-13 10:46:57 +0100104static const match_table_t tokens = {
Domen Puncer7a673c62006-03-23 03:00:59 -0800105 {Opt_uid, "uid=%u"},
106 {Opt_gid, "gid=%u"},
107 {Opt_mode, "mode=%o"},
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000108#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
109 {Opt_ptmxmode, "ptmxmode=%o"},
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000110 {Opt_newinstance, "newinstance"},
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000111#endif
Domen Puncer7a673c62006-03-23 03:00:59 -0800112 {Opt_err, NULL}
113};
114
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000115struct pts_fs_info {
116 struct ida allocated_ptys;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000117 struct pts_mount_opts mount_opts;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000118 struct dentry *ptmx_dentry;
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000119};
120
121static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
122{
123 return sb->s_fs_info;
124}
125
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000126static inline struct super_block *pts_sb_from_inode(struct inode *inode)
127{
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000128#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000129 if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
130 return inode->i_sb;
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000131#endif
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000132 return devpts_mnt->mnt_sb;
133}
134
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000135#define PARSE_MOUNT 0
136#define PARSE_REMOUNT 1
137
Sukadev Bhattiprolu1f71ebe2009-05-14 19:38:24 -0700138/*
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 */
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000147static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Domen Puncer7a673c62006-03-23 03:00:59 -0800149 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000151 opts->setuid = 0;
152 opts->setgid = 0;
153 opts->uid = 0;
154 opts->gid = 0;
155 opts->mode = DEVPTS_DEFAULT_MODE;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000156 opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
Domen Puncer7a673c62006-03-23 03:00:59 -0800157
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000158 /* newinstance makes sense only on initial mount */
159 if (op == PARSE_MOUNT)
160 opts->newinstance = 0;
161
Domen Puncer7a673c62006-03-23 03:00:59 -0800162 while ((p = strsep(&data, ",")) != NULL) {
163 substring_t args[MAX_OPT_ARGS];
164 int token;
165 int option;
166
167 if (!*p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 continue;
Domen Puncer7a673c62006-03-23 03:00:59 -0800169
170 token = match_token(p, tokens, args);
171 switch (token) {
172 case Opt_uid:
173 if (match_int(&args[0], &option))
174 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000175 opts->uid = option;
176 opts->setuid = 1;
Domen Puncer7a673c62006-03-23 03:00:59 -0800177 break;
178 case Opt_gid:
179 if (match_int(&args[0], &option))
180 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000181 opts->gid = option;
182 opts->setgid = 1;
Domen Puncer7a673c62006-03-23 03:00:59 -0800183 break;
184 case Opt_mode:
185 if (match_octal(&args[0], &option))
186 return -EINVAL;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000187 opts->mode = option & S_IALLUGO;
Domen Puncer7a673c62006-03-23 03:00:59 -0800188 break;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000189#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;
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000195 case Opt_newinstance:
196 /* newinstance makes sense only on initial mount */
197 if (op == PARSE_MOUNT)
198 opts->newinstance = 1;
199 break;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000200#endif
Domen Puncer7a673c62006-03-23 03:00:59 -0800201 default:
202 printk(KERN_ERR "devpts: called with bogus options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -EINVAL;
204 }
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 return 0;
208}
209
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000210#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
211static int mknod_ptmx(struct super_block *sb)
Sukadev Bhattiprolu53af8ee2009-01-02 13:41:47 +0000212{
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000213 int mode;
214 int rc = -ENOMEM;
215 struct dentry *dentry;
216 struct inode *inode;
217 struct dentry *root = sb->s_root;
Sukadev Bhattiprolu53af8ee2009-01-02 13:41:47 +0000218 struct pts_fs_info *fsi = DEVPTS_SB(sb);
219 struct pts_mount_opts *opts = &fsi->mount_opts;
220
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000221 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;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000246 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;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000255out:
256 mutex_unlock(&root->d_inode->i_mutex);
257 return rc;
258}
259
260static 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
269static inline void update_ptmx_mode(struct pts_fs_info *fsi)
270{
271 return;
272}
273#endif
274
275static 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
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000281 err = parse_mount_options(data, PARSE_REMOUNT, opts);
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000282
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;
Sukadev Bhattiprolu53af8ee2009-01-02 13:41:47 +0000292}
293
Al Viro34c80b12011-12-08 21:32:45 -0500294static int devpts_show_options(struct seq_file *seq, struct dentry *root)
Miklos Szeredib87a2672008-02-08 04:21:41 -0800295{
Al Viro34c80b12011-12-08 21:32:45 -0500296 struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000297 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);
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000304#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
305 seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
306#endif
Miklos Szeredib87a2672008-02-08 04:21:41 -0800307
308 return 0;
309}
310
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800311static const struct super_operations devpts_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .statfs = simple_statfs,
313 .remount_fs = devpts_remount,
Miklos Szeredib87a2672008-02-08 04:21:41 -0800314 .show_options = devpts_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000317static 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);
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000326 fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000327 fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000328
329 return fsi;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int
333devpts_fill_super(struct super_block *s, void *data, int silent)
334{
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000335 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 s->s_blocksize = 1024;
338 s->s_blocksize_bits = 10;
339 s->s_magic = DEVPTS_SUPER_MAGIC;
340 s->s_op = &devpts_sops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 s->s_time_gran = 1;
342
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000343 s->s_fs_info = new_pts_fs_info();
344 if (!s->s_fs_info)
345 goto fail;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 inode = new_inode(s);
348 if (!inode)
Al Viro3850aba2012-01-08 19:40:27 -0500349 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 inode->i_ino = 1;
351 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200355 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000357 s->s_root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (s->s_root)
359 return 0;
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000360
Alan Cox835aa442009-01-02 13:42:48 +0000361 printk(KERN_ERR "devpts: get root dentry failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 iput(inode);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364fail:
365 return -ENOMEM;
366}
367
Andrew Morton8c056e52009-01-02 13:44:12 +0000368#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000369static int compare_init_pts_sb(struct super_block *s, void *p)
370{
371 if (devpts_mnt)
372 return devpts_mnt->mnt_sb == s;
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000373 return 0;
374}
375
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000376/*
Al Virofc14f2f2010-07-25 01:48:30 +0400377 * devpts_mount()
Sukadev Bhattiprolu289f00e2009-03-07 10:12:06 -0800378 *
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800379 * 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.
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000382 *
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800383 * 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.
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000386 *
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800387 * 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
Al Virofc14f2f2010-07-25 01:48:30 +0400391 * kernel mount, like mount_single().
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000392 *
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800393 * Mounts with 'newinstance' option create a new, private namespace.
394 *
395 * NOTE:
396 *
Al Virofc14f2f2010-07-25 01:48:30 +0400397 * For single-mount semantics, devpts cannot use mount_single(),
398 * because mount_single()/sget() find and use the super-block from
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000399 * the most recent mount of devpts. But that recent mount may be a
Al Virofc14f2f2010-07-25 01:48:30 +0400400 * 'newinstance' mount and mount_single() would pick the newinstance
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000401 * super-block instead of the initial super-block.
Sukadev Bhattiprolud4076ac2009-01-02 13:42:19 +0000402 */
Al Virofc14f2f2010-07-25 01:48:30 +0400403static struct dentry *devpts_mount(struct file_system_type *fs_type,
404 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Sukadev Bhattiprolu482984f2009-03-07 10:14:41 -0800406 int error;
407 struct pts_mount_opts opts;
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800408 struct super_block *s;
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000409
Sukadev Bhattiprolu1f71ebe2009-05-14 19:38:24 -0700410 error = parse_mount_options(data, PARSE_MOUNT, &opts);
411 if (error)
Al Virofc14f2f2010-07-25 01:48:30 +0400412 return ERR_PTR(error);
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000413
Sukadev Bhattiprolu482984f2009-03-07 10:14:41 -0800414 if (opts.newinstance)
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800415 s = sget(fs_type, NULL, set_anon_super, NULL);
Sukadev Bhattiprolu482984f2009-03-07 10:14:41 -0800416 else
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800417 s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
Sukadev Bhattiprolu945cf2c2009-03-07 10:11:41 -0800418
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800419 if (IS_ERR(s))
Al Virofc14f2f2010-07-25 01:48:30 +0400420 return ERR_CAST(s);
Sukadev Bhattiprolu945cf2c2009-03-07 10:11:41 -0800421
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800422 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
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800430 memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
431
432 error = mknod_ptmx(s);
Sukadev Bhattiprolu945cf2c2009-03-07 10:11:41 -0800433 if (error)
Al Viro89468072010-03-20 21:57:43 -0400434 goto out_undo_sget;
435
Al Virofc14f2f2010-07-25 01:48:30 +0400436 return dget(s->s_root);
Sukadev Bhattiprolu945cf2c2009-03-07 10:11:41 -0800437
Sukadev Bhattiprolu1bd79032009-03-07 10:12:32 -0800438out_undo_sget:
Al Viro6f5bbff2009-05-06 01:34:22 -0400439 deactivate_locked_super(s);
Al Virofc14f2f2010-07-25 01:48:30 +0400440 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Sukadev Bhattiprolu482984f2009-03-07 10:14:41 -0800442
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000443#else
444/*
445 * This supports only the legacy single-instance semantics (no
446 * multiple-instance semantics)
447 */
Al Virofc14f2f2010-07-25 01:48:30 +0400448static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
449 const char *dev_name, void *data)
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000450{
Al Virofc14f2f2010-07-25 01:48:30 +0400451 return mount_single(fs_type, flags, data, devpts_fill_super);
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000452}
453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000455static void devpts_kill_sb(struct super_block *sb)
456{
457 struct pts_fs_info *fsi = DEVPTS_SB(sb);
458
459 kfree(fsi);
Sukadev Bhattiprolu1f8f1e22009-01-02 13:42:02 +0000460 kill_litter_super(sb);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct file_system_type devpts_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .name = "devpts",
Al Virofc14f2f2010-07-25 01:48:30 +0400465 .mount = devpts_mount,
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000466 .kill_sb = devpts_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467};
468
469/*
470 * The normal naming convention is simply /dev/pts/<number>; this conforms
471 * to the System V naming convention
472 */
473
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100474int devpts_new_index(struct inode *ptmx_inode)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700475{
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000476 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
477 struct pts_fs_info *fsi = DEVPTS_SB(sb);
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700478 int index;
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400479 int ida_ret;
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700480
481retry:
Alan Cox835aa442009-01-02 13:42:48 +0000482 if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700483 return -ENOMEM;
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700484
485 mutex_lock(&allocated_ptys_lock);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000486 ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400487 if (ida_ret < 0) {
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700488 mutex_unlock(&allocated_ptys_lock);
Alexey Dobriyan7ee7c122008-07-26 11:42:16 +0400489 if (ida_ret == -EAGAIN)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700490 goto retry;
491 return -EIO;
492 }
493
494 if (index >= pty_limit) {
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000495 ida_remove(&fsi->allocated_ptys, index);
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700496 mutex_unlock(&allocated_ptys_lock);
497 return -EIO;
498 }
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +0400499 pty_count++;
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700500 mutex_unlock(&allocated_ptys_lock);
501 return index;
502}
503
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100504void devpts_kill_index(struct inode *ptmx_inode, int idx)
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700505{
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000506 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
507 struct pts_fs_info *fsi = DEVPTS_SB(sb);
508
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700509 mutex_lock(&allocated_ptys_lock);
Sukadev Bhattiprolue76b7c02009-01-02 13:41:21 +0000510 ida_remove(&fsi->allocated_ptys, idx);
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +0400511 pty_count--;
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -0700512 mutex_unlock(&allocated_ptys_lock);
513}
514
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100515int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Alan Cox835aa442009-01-02 13:42:48 +0000517 /* tty layer puts index from devpts_new_index() in here */
518 int number = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct tty_driver *driver = tty->driver;
520 dev_t device = MKDEV(driver->major, driver->minor_start+number);
521 struct dentry *dentry;
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000522 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
523 struct inode *inode = new_inode(sb);
524 struct dentry *root = sb->s_root;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000525 struct pts_fs_info *fsi = DEVPTS_SB(sb);
526 struct pts_mount_opts *opts = &fsi->mount_opts;
Andrey Vaginaa597bc2011-02-08 00:14:52 +0300527 int ret = 0;
Sukadev Bhattiprolu89a52e102008-10-13 10:43:18 +0100528 char s[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
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
David Howellsd0eafc72009-01-02 13:44:49 +0000537 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Sukadev Bhattiprolu31af0ab2009-01-02 13:41:33 +0000541 init_special_inode(inode, S_IFCHR|opts->mode, device);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700542 inode->i_private = tty;
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100543 tty->driver_data = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Sukadev Bhattiprolu89a52e102008-10-13 10:43:18 +0100545 sprintf(s, "%d", number);
546
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000547 mutex_lock(&root->d_inode->i_mutex);
Sukadev Bhattiprolu89a52e102008-10-13 10:43:18 +0100548
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000549 dentry = d_alloc_name(root, s);
Andrey Vaginb12d1252011-03-22 16:35:11 -0700550 if (dentry) {
Sukadev Bhattiprolu89a52e102008-10-13 10:43:18 +0100551 d_add(dentry, inode);
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000552 fsnotify_create(root->d_inode, dentry);
Andrey Vaginaa597bc2011-02-08 00:14:52 +0300553 } else {
554 iput(inode);
555 ret = -ENOMEM;
Florin Malita3972b7f2007-05-08 00:24:18 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000558 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Andrey Vaginaa597bc2011-02-08 00:14:52 +0300560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100563struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Sukadev Bhattiproluedfacdd2009-11-17 18:35:43 -0800565 struct dentry *dentry;
566 struct tty_struct *tty;
567
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +0100568 BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Sukadev Bhattiproluedfacdd2009-11-17 18:35:43 -0800570 /* 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;
Sukadev Bhattiprolu527b3e42008-10-13 10:43:08 +0100576 if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
Sukadev Bhattiproluedfacdd2009-11-17 18:35:43 -0800577 tty = (struct tty_struct *)pts_inode->i_private;
578
579 dput(dentry);
580
581 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100584void devpts_pty_kill(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100586 struct inode *inode = tty->driver_data;
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000587 struct super_block *sb = pts_sb_from_inode(inode);
588 struct dentry *root = sb->s_root;
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100589 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100591 BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
592
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000593 mutex_lock(&root->d_inode->i_mutex);
Sukadev Bhattiprolua6f37da2008-10-13 10:43:27 +0100594
595 dentry = d_find_alias(inode);
Sukadev Bhattiprolu2a1b2dc2009-01-02 13:42:27 +0000596
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200597 drop_nlink(inode);
Andrey Vaginaa597bc2011-02-08 00:14:52 +0300598 d_delete(dentry);
599 dput(dentry); /* d_alloc_name() in devpts_pty_new() */
Alan Cox835aa442009-01-02 13:42:48 +0000600 dput(dentry); /* d_find_alias above */
Andrey Vaginaa597bc2011-02-08 00:14:52 +0300601
Sukadev Bhattiprolu59e55e62009-01-02 13:41:11 +0000602 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static int __init init_devpts_fs(void)
606{
607 int err = register_filesystem(&devpts_fs_type);
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +0400608 struct ctl_table_header *table;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!err) {
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +0400611 table = register_sysctl_table(pty_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 devpts_mnt = kern_mount(&devpts_fs_type);
Alan Cox93d55812009-06-11 14:03:55 +0100613 if (IS_ERR(devpts_mnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 err = PTR_ERR(devpts_mnt);
Alan Cox93d55812009-06-11 14:03:55 +0100615 unregister_filesystem(&devpts_fs_type);
Konstantin Khlebnikova4834c12012-01-05 13:06:02 +0400616 unregister_sysctl_table(table);
Alan Cox93d55812009-06-11 14:03:55 +0100617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 return err;
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621module_init(init_devpts_fs)