]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/devpts/inode.c
Define get_init_pts_sb()
[mirror_ubuntu-zesty-kernel.git] / fs / devpts / inode.c
CommitLineData
1da177e4
LT
1/* -*- 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>
718a9163
SB
20#include <linux/mutex.h>
21#include <linux/idr.h>
1da177e4 22#include <linux/devpts_fs.h>
7a673c6b 23#include <linux/parser.h>
3972b7f6 24#include <linux/fsnotify.h>
b87a267e 25#include <linux/seq_file.h>
1da177e4
LT
26
27#define DEVPTS_SUPER_MAGIC 0x1cd1
28
b87a267e 29#define DEVPTS_DEFAULT_MODE 0600
1f8f1e29
SB
30/*
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
527b3e47 37#define PTMX_MINOR 2
b87a267e 38
718a9163 39extern int pty_limit; /* Config limit on Unix98 ptys */
718a9163
SB
40static DEFINE_MUTEX(allocated_ptys_lock);
41
1da177e4 42static struct vfsmount *devpts_mnt;
1da177e4 43
31af0abb 44struct pts_mount_opts {
1da177e4
LT
45 int setuid;
46 int setgid;
47 uid_t uid;
48 gid_t gid;
49 umode_t mode;
1f8f1e29 50 umode_t ptmxmode;
31af0abb 51};
1da177e4 52
7a673c6b 53enum {
1f8f1e29 54 Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode,
7a673c6b
DP
55 Opt_err
56};
57
a447c093 58static const match_table_t tokens = {
7a673c6b
DP
59 {Opt_uid, "uid=%u"},
60 {Opt_gid, "gid=%u"},
61 {Opt_mode, "mode=%o"},
1f8f1e29
SB
62#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
63 {Opt_ptmxmode, "ptmxmode=%o"},
64#endif
7a673c6b
DP
65 {Opt_err, NULL}
66};
67
e76b7c01
SB
68struct pts_fs_info {
69 struct ida allocated_ptys;
31af0abb 70 struct pts_mount_opts mount_opts;
1f8f1e29 71 struct dentry *ptmx_dentry;
e76b7c01
SB
72};
73
74static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
75{
76 return sb->s_fs_info;
77}
78
59e55e6c
SB
79static inline struct super_block *pts_sb_from_inode(struct inode *inode)
80{
81 if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
82 return inode->i_sb;
83
84 return devpts_mnt->mnt_sb;
85}
86
53af8ee4 87static int parse_mount_options(char *data, struct pts_mount_opts *opts)
1da177e4 88{
7a673c6b
DP
89 char *p;
90
31af0abb
SB
91 opts->setuid = 0;
92 opts->setgid = 0;
93 opts->uid = 0;
94 opts->gid = 0;
95 opts->mode = DEVPTS_DEFAULT_MODE;
1f8f1e29 96 opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
7a673c6b
DP
97
98 while ((p = strsep(&data, ",")) != NULL) {
99 substring_t args[MAX_OPT_ARGS];
100 int token;
101 int option;
102
103 if (!*p)
1da177e4 104 continue;
7a673c6b
DP
105
106 token = match_token(p, tokens, args);
107 switch (token) {
108 case Opt_uid:
109 if (match_int(&args[0], &option))
110 return -EINVAL;
31af0abb
SB
111 opts->uid = option;
112 opts->setuid = 1;
7a673c6b
DP
113 break;
114 case Opt_gid:
115 if (match_int(&args[0], &option))
116 return -EINVAL;
31af0abb
SB
117 opts->gid = option;
118 opts->setgid = 1;
7a673c6b
DP
119 break;
120 case Opt_mode:
121 if (match_octal(&args[0], &option))
122 return -EINVAL;
31af0abb 123 opts->mode = option & S_IALLUGO;
7a673c6b 124 break;
1f8f1e29
SB
125#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
126 case Opt_ptmxmode:
127 if (match_octal(&args[0], &option))
128 return -EINVAL;
129 opts->ptmxmode = option & S_IALLUGO;
130 break;
131#endif
7a673c6b
DP
132 default:
133 printk(KERN_ERR "devpts: called with bogus options\n");
1da177e4
LT
134 return -EINVAL;
135 }
136 }
1da177e4
LT
137
138 return 0;
139}
140
1f8f1e29
SB
141#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
142static int mknod_ptmx(struct super_block *sb)
143{
144 int mode;
145 int rc = -ENOMEM;
146 struct dentry *dentry;
147 struct inode *inode;
148 struct dentry *root = sb->s_root;
149 struct pts_fs_info *fsi = DEVPTS_SB(sb);
150 struct pts_mount_opts *opts = &fsi->mount_opts;
151
152 mutex_lock(&root->d_inode->i_mutex);
153
154 /* If we have already created ptmx node, return */
155 if (fsi->ptmx_dentry) {
156 rc = 0;
157 goto out;
158 }
159
160 dentry = d_alloc_name(root, "ptmx");
161 if (!dentry) {
162 printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
163 goto out;
164 }
165
166 /*
167 * Create a new 'ptmx' node in this mount of devpts.
168 */
169 inode = new_inode(sb);
170 if (!inode) {
171 printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
172 dput(dentry);
173 goto out;
174 }
175
176 inode->i_ino = 2;
177 inode->i_uid = inode->i_gid = 0;
178 inode->i_blocks = 0;
179 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
180
181 mode = S_IFCHR|opts->ptmxmode;
182 init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
183
184 d_add(dentry, inode);
185
186 fsi->ptmx_dentry = dentry;
187 rc = 0;
188
189 printk(KERN_DEBUG "Created ptmx node in devpts ino %lu\n",
190 inode->i_ino);
191out:
192 mutex_unlock(&root->d_inode->i_mutex);
193 return rc;
194}
195
196static void update_ptmx_mode(struct pts_fs_info *fsi)
197{
198 struct inode *inode;
199 if (fsi->ptmx_dentry) {
200 inode = fsi->ptmx_dentry->d_inode;
201 inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
202 }
203}
204#else
205static inline void update_ptmx_mode(struct pts_fs_info *fsi)
206{
207 return;
208}
209#endif
210
53af8ee4
SB
211static int devpts_remount(struct super_block *sb, int *flags, char *data)
212{
1f8f1e29 213 int err;
53af8ee4
SB
214 struct pts_fs_info *fsi = DEVPTS_SB(sb);
215 struct pts_mount_opts *opts = &fsi->mount_opts;
216
1f8f1e29
SB
217 err = parse_mount_options(data, opts);
218
219 /*
220 * parse_mount_options() restores options to default values
221 * before parsing and may have changed ptmxmode. So, update the
222 * mode in the inode too. Bogus options don't fail the remount,
223 * so do this even on error return.
224 */
225 update_ptmx_mode(fsi);
226
227 return err;
53af8ee4
SB
228}
229
b87a267e
MS
230static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs)
231{
31af0abb
SB
232 struct pts_fs_info *fsi = DEVPTS_SB(vfs->mnt_sb);
233 struct pts_mount_opts *opts = &fsi->mount_opts;
234
235 if (opts->setuid)
236 seq_printf(seq, ",uid=%u", opts->uid);
237 if (opts->setgid)
238 seq_printf(seq, ",gid=%u", opts->gid);
239 seq_printf(seq, ",mode=%03o", opts->mode);
1f8f1e29
SB
240#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
241 seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
242#endif
b87a267e
MS
243
244 return 0;
245}
246
ee9b6d61 247static const struct super_operations devpts_sops = {
1da177e4
LT
248 .statfs = simple_statfs,
249 .remount_fs = devpts_remount,
b87a267e 250 .show_options = devpts_show_options,
1da177e4
LT
251};
252
e76b7c01
SB
253static void *new_pts_fs_info(void)
254{
255 struct pts_fs_info *fsi;
256
257 fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
258 if (!fsi)
259 return NULL;
260
261 ida_init(&fsi->allocated_ptys);
31af0abb 262 fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
1f8f1e29 263 fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
e76b7c01
SB
264
265 return fsi;
266}
267
1da177e4
LT
268static int
269devpts_fill_super(struct super_block *s, void *data, int silent)
270{
1f8f1e29 271 struct inode *inode;
1da177e4
LT
272
273 s->s_blocksize = 1024;
274 s->s_blocksize_bits = 10;
275 s->s_magic = DEVPTS_SUPER_MAGIC;
276 s->s_op = &devpts_sops;
1da177e4
LT
277 s->s_time_gran = 1;
278
e76b7c01
SB
279 s->s_fs_info = new_pts_fs_info();
280 if (!s->s_fs_info)
281 goto fail;
282
1da177e4
LT
283 inode = new_inode(s);
284 if (!inode)
e76b7c01 285 goto free_fsi;
1da177e4
LT
286 inode->i_ino = 1;
287 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
288 inode->i_blocks = 0;
1da177e4
LT
289 inode->i_uid = inode->i_gid = 0;
290 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
291 inode->i_op = &simple_dir_inode_operations;
292 inode->i_fop = &simple_dir_operations;
293 inode->i_nlink = 2;
294
59e55e6c 295 s->s_root = d_alloc_root(inode);
1da177e4
LT
296 if (s->s_root)
297 return 0;
1f8f1e29 298
1da177e4
LT
299 printk("devpts: get root dentry failed\n");
300 iput(inode);
e76b7c01
SB
301
302free_fsi:
303 kfree(s->s_fs_info);
1da177e4
LT
304fail:
305 return -ENOMEM;
306}
307
d4076ac5
SB
308static int compare_init_pts_sb(struct super_block *s, void *p)
309{
310 if (devpts_mnt)
311 return devpts_mnt->mnt_sb == s;
312
313 return 0;
314}
315
316/*
317 * get_init_pts_sb()
318 *
319 * This interface is needed to support multiple namespace semantics in
320 * devpts while preserving backward compatibility of the current 'single-
321 * namespace' semantics. i.e all mounts of devpts without the 'newinstance'
322 * mount option should bind to the initial kernel mount, like
323 * get_sb_single().
324 *
325 * Mounts with 'newinstance' option create a new private namespace.
326 *
327 * But for single-mount semantics, devpts cannot use get_sb_single(),
328 * because get_sb_single()/sget() find and use the super-block from
329 * the most recent mount of devpts. But that recent mount may be a
330 * 'newinstance' mount and get_sb_single() would pick the newinstance
331 * super-block instead of the initial super-block.
332 *
333 * This interface is identical to get_sb_single() except that it
334 * consistently selects the 'single-namespace' superblock even in the
335 * presence of the private namespace (i.e 'newinstance') super-blocks.
336 */
337static int get_init_pts_sb(struct file_system_type *fs_type, int flags,
338 void *data, struct vfsmount *mnt)
339{
340 struct super_block *s;
341 int error;
342
343 s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
344 if (IS_ERR(s))
345 return PTR_ERR(s);
346
347 if (!s->s_root) {
348 s->s_flags = flags;
349 error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
350 if (error) {
351 up_write(&s->s_umount);
352 deactivate_super(s);
353 return error;
354 }
355 s->s_flags |= MS_ACTIVE;
356 }
357 do_remount_sb(s, flags, data, 0);
358 return simple_set_mnt(mnt, s);
359}
360
454e2398
DH
361static int devpts_get_sb(struct file_system_type *fs_type,
362 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 363{
d4076ac5 364 return get_init_pts_sb(fs_type, flags, data, mnt);
1da177e4
LT
365}
366
e76b7c01
SB
367static void devpts_kill_sb(struct super_block *sb)
368{
369 struct pts_fs_info *fsi = DEVPTS_SB(sb);
370
371 kfree(fsi);
1f8f1e29 372 kill_litter_super(sb);
e76b7c01
SB
373}
374
1da177e4
LT
375static struct file_system_type devpts_fs_type = {
376 .owner = THIS_MODULE,
377 .name = "devpts",
378 .get_sb = devpts_get_sb,
e76b7c01 379 .kill_sb = devpts_kill_sb,
1da177e4
LT
380};
381
382/*
383 * The normal naming convention is simply /dev/pts/<number>; this conforms
384 * to the System V naming convention
385 */
386
15f1a633 387int devpts_new_index(struct inode *ptmx_inode)
718a9163 388{
e76b7c01
SB
389 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
390 struct pts_fs_info *fsi = DEVPTS_SB(sb);
718a9163 391 int index;
7ee7c12b 392 int ida_ret;
718a9163
SB
393
394retry:
e76b7c01 395 if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) {
718a9163
SB
396 return -ENOMEM;
397 }
398
399 mutex_lock(&allocated_ptys_lock);
e76b7c01 400 ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
7ee7c12b 401 if (ida_ret < 0) {
718a9163 402 mutex_unlock(&allocated_ptys_lock);
7ee7c12b 403 if (ida_ret == -EAGAIN)
718a9163
SB
404 goto retry;
405 return -EIO;
406 }
407
408 if (index >= pty_limit) {
e76b7c01 409 ida_remove(&fsi->allocated_ptys, index);
718a9163
SB
410 mutex_unlock(&allocated_ptys_lock);
411 return -EIO;
412 }
413 mutex_unlock(&allocated_ptys_lock);
414 return index;
415}
416
15f1a633 417void devpts_kill_index(struct inode *ptmx_inode, int idx)
718a9163 418{
e76b7c01
SB
419 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
420 struct pts_fs_info *fsi = DEVPTS_SB(sb);
421
718a9163 422 mutex_lock(&allocated_ptys_lock);
e76b7c01 423 ida_remove(&fsi->allocated_ptys, idx);
718a9163
SB
424 mutex_unlock(&allocated_ptys_lock);
425}
426
15f1a633 427int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
1da177e4 428{
718a9163 429 int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
1da177e4
LT
430 struct tty_driver *driver = tty->driver;
431 dev_t device = MKDEV(driver->major, driver->minor_start+number);
432 struct dentry *dentry;
59e55e6c
SB
433 struct super_block *sb = pts_sb_from_inode(ptmx_inode);
434 struct inode *inode = new_inode(sb);
435 struct dentry *root = sb->s_root;
31af0abb
SB
436 struct pts_fs_info *fsi = DEVPTS_SB(sb);
437 struct pts_mount_opts *opts = &fsi->mount_opts;
89a52e10 438 char s[12];
1da177e4
LT
439
440 /* We're supposed to be given the slave end of a pty */
441 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
442 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
443
444 if (!inode)
445 return -ENOMEM;
446
447 inode->i_ino = number+2;
ec4c2aac
DH
448 inode->i_uid = config.setuid ? config.uid : current_fsuid();
449 inode->i_gid = config.setgid ? config.gid : current_fsgid();
1da177e4 450 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
31af0abb 451 init_special_inode(inode, S_IFCHR|opts->mode, device);
8e18e294 452 inode->i_private = tty;
a6f37daa 453 tty->driver_data = inode;
1da177e4 454
89a52e10
SB
455 sprintf(s, "%d", number);
456
59e55e6c 457 mutex_lock(&root->d_inode->i_mutex);
89a52e10 458
59e55e6c 459 dentry = d_alloc_name(root, s);
89a52e10
SB
460 if (!IS_ERR(dentry)) {
461 d_add(dentry, inode);
59e55e6c 462 fsnotify_create(root->d_inode, dentry);
3972b7f6 463 }
1da177e4 464
59e55e6c 465 mutex_unlock(&root->d_inode->i_mutex);
1da177e4
LT
466
467 return 0;
468}
469
15f1a633 470struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
1da177e4 471{
527b3e47 472 BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
1da177e4 473
527b3e47
SB
474 if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
475 return (struct tty_struct *)pts_inode->i_private;
476 return NULL;
1da177e4
LT
477}
478
15f1a633 479void devpts_pty_kill(struct tty_struct *tty)
1da177e4 480{
a6f37daa 481 struct inode *inode = tty->driver_data;
59e55e6c
SB
482 struct super_block *sb = pts_sb_from_inode(inode);
483 struct dentry *root = sb->s_root;
a6f37daa 484 struct dentry *dentry;
1da177e4 485
a6f37daa
SB
486 BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
487
59e55e6c 488 mutex_lock(&root->d_inode->i_mutex);
a6f37daa
SB
489
490 dentry = d_find_alias(inode);
491 if (dentry && !IS_ERR(dentry)) {
492 inode->i_nlink--;
493 d_delete(dentry);
1da177e4
LT
494 dput(dentry);
495 }
a6f37daa 496
59e55e6c 497 mutex_unlock(&root->d_inode->i_mutex);
1da177e4
LT
498}
499
500static int __init init_devpts_fs(void)
501{
502 int err = register_filesystem(&devpts_fs_type);
503 if (!err) {
504 devpts_mnt = kern_mount(&devpts_fs_type);
505 if (IS_ERR(devpts_mnt))
506 err = PTR_ERR(devpts_mnt);
507 }
508 return err;
509}
510
511static void __exit exit_devpts_fs(void)
512{
513 unregister_filesystem(&devpts_fs_type);
514 mntput(devpts_mnt);
515}
516
517module_init(init_devpts_fs)
518module_exit(exit_devpts_fs)
519MODULE_LICENSE("GPL");