Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> |
| 20 | #include <linux/tty.h> |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Nick Black | 1fd7317 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 22 | #include <linux/magic.h> |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 23 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/devpts_fs.h> |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 25 | #include <linux/parser.h> |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 26 | #include <linux/fsnotify.h> |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 29 | #define DEVPTS_DEFAULT_MODE 0600 |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 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 |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 37 | #define PTMX_MINOR 2 |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 38 | |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 39 | /* |
| 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 | */ |
| 43 | static int pty_limit = NR_UNIX98_PTY_DEFAULT; |
| 44 | static int pty_limit_min; |
| 45 | static int pty_limit_max = NR_UNIX98_PTY_MAX; |
| 46 | static int pty_count; |
| 47 | |
| 48 | static 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 | |
| 67 | static struct ctl_table pty_kern_table[] = { |
| 68 | { |
| 69 | .procname = "pty", |
| 70 | .mode = 0555, |
| 71 | .child = pty_table, |
| 72 | }, |
| 73 | {} |
| 74 | }; |
| 75 | |
| 76 | static struct ctl_table pty_root_table[] = { |
| 77 | { |
| 78 | .procname = "kernel", |
| 79 | .mode = 0555, |
| 80 | .child = pty_kern_table, |
| 81 | }, |
| 82 | {} |
| 83 | }; |
| 84 | |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 85 | static DEFINE_MUTEX(allocated_ptys_lock); |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static struct vfsmount *devpts_mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 89 | struct pts_mount_opts { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int setuid; |
| 91 | int setgid; |
| 92 | uid_t uid; |
| 93 | gid_t gid; |
| 94 | umode_t mode; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 95 | umode_t ptmxmode; |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 96 | int newinstance; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 97 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 99 | enum { |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 100 | Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance, |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 101 | Opt_err |
| 102 | }; |
| 103 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 104 | static const match_table_t tokens = { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 105 | {Opt_uid, "uid=%u"}, |
| 106 | {Opt_gid, "gid=%u"}, |
| 107 | {Opt_mode, "mode=%o"}, |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 108 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 109 | {Opt_ptmxmode, "ptmxmode=%o"}, |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 110 | {Opt_newinstance, "newinstance"}, |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 111 | #endif |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 112 | {Opt_err, NULL} |
| 113 | }; |
| 114 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 115 | struct pts_fs_info { |
| 116 | struct ida allocated_ptys; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 117 | struct pts_mount_opts mount_opts; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 118 | struct dentry *ptmx_dentry; |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb) |
| 122 | { |
| 123 | return sb->s_fs_info; |
| 124 | } |
| 125 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 126 | static inline struct super_block *pts_sb_from_inode(struct inode *inode) |
| 127 | { |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 129 | if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
| 130 | return inode->i_sb; |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 131 | #endif |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 132 | return devpts_mnt->mnt_sb; |
| 133 | } |
| 134 | |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 135 | #define PARSE_MOUNT 0 |
| 136 | #define PARSE_REMOUNT 1 |
| 137 | |
Sukadev Bhattiprolu | 1f71ebe | 2009-05-14 19:38:24 -0700 | [diff] [blame] | 138 | /* |
| 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 Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 147 | static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 149 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 151 | opts->setuid = 0; |
| 152 | opts->setgid = 0; |
| 153 | opts->uid = 0; |
| 154 | opts->gid = 0; |
| 155 | opts->mode = DEVPTS_DEFAULT_MODE; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 156 | opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 157 | |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 158 | /* newinstance makes sense only on initial mount */ |
| 159 | if (op == PARSE_MOUNT) |
| 160 | opts->newinstance = 0; |
| 161 | |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 162 | while ((p = strsep(&data, ",")) != NULL) { |
| 163 | substring_t args[MAX_OPT_ARGS]; |
| 164 | int token; |
| 165 | int option; |
| 166 | |
| 167 | if (!*p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | continue; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 169 | |
| 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 Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 175 | opts->uid = option; |
| 176 | opts->setuid = 1; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 177 | break; |
| 178 | case Opt_gid: |
| 179 | if (match_int(&args[0], &option)) |
| 180 | return -EINVAL; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 181 | opts->gid = option; |
| 182 | opts->setgid = 1; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 183 | break; |
| 184 | case Opt_mode: |
| 185 | if (match_octal(&args[0], &option)) |
| 186 | return -EINVAL; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 187 | opts->mode = option & S_IALLUGO; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 188 | break; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 189 | #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 Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 195 | case Opt_newinstance: |
| 196 | /* newinstance makes sense only on initial mount */ |
| 197 | if (op == PARSE_MOUNT) |
| 198 | opts->newinstance = 1; |
| 199 | break; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 200 | #endif |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 201 | default: |
| 202 | printk(KERN_ERR "devpts: called with bogus options\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | } |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 210 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 211 | static int mknod_ptmx(struct super_block *sb) |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 212 | { |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 213 | int mode; |
| 214 | int rc = -ENOMEM; |
| 215 | struct dentry *dentry; |
| 216 | struct inode *inode; |
| 217 | struct dentry *root = sb->s_root; |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 218 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 219 | struct pts_mount_opts *opts = &fsi->mount_opts; |
| 220 | |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 221 | 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 Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 246 | 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 Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 255 | out: |
| 256 | mutex_unlock(&root->d_inode->i_mutex); |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | static 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 |
| 269 | static inline void update_ptmx_mode(struct pts_fs_info *fsi) |
| 270 | { |
| 271 | return; |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | static 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 Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 281 | err = parse_mount_options(data, PARSE_REMOUNT, opts); |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 282 | |
| 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 Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 294 | static int devpts_show_options(struct seq_file *seq, struct dentry *root) |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 295 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 296 | struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb); |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 297 | 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 Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 304 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 305 | seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode); |
| 306 | #endif |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 311 | static const struct super_operations devpts_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | .statfs = simple_statfs, |
| 313 | .remount_fs = devpts_remount, |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 314 | .show_options = devpts_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 317 | static 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 Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 326 | fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 327 | fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 328 | |
| 329 | return fsi; |
| 330 | } |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | static int |
| 333 | devpts_fill_super(struct super_block *s, void *data, int silent) |
| 334 | { |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 335 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | s->s_time_gran = 1; |
| 342 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 343 | s->s_fs_info = new_pts_fs_info(); |
| 344 | if (!s->s_fs_info) |
| 345 | goto fail; |
| 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | inode = new_inode(s); |
| 348 | if (!inode) |
Al Viro | 3850aba | 2012-01-08 19:40:27 -0500 | [diff] [blame] | 349 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | inode->i_ino = 1; |
| 351 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | 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 Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 355 | set_nlink(inode, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 357 | s->s_root = d_alloc_root(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (s->s_root) |
| 359 | return 0; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 360 | |
Alan Cox | 835aa44 | 2009-01-02 13:42:48 +0000 | [diff] [blame] | 361 | printk(KERN_ERR "devpts: get root dentry failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | iput(inode); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | fail: |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | |
Andrew Morton | 8c056e5 | 2009-01-02 13:44:12 +0000 | [diff] [blame] | 368 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
Sukadev Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 369 | static int compare_init_pts_sb(struct super_block *s, void *p) |
| 370 | { |
| 371 | if (devpts_mnt) |
| 372 | return devpts_mnt->mnt_sb == s; |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 376 | /* |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 377 | * devpts_mount() |
Sukadev Bhattiprolu | 289f00e | 2009-03-07 10:12:06 -0800 | [diff] [blame] | 378 | * |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 379 | * 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 Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 382 | * |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 383 | * 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 Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 386 | * |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 387 | * 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 Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 391 | * kernel mount, like mount_single(). |
Sukadev Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 392 | * |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 393 | * Mounts with 'newinstance' option create a new, private namespace. |
| 394 | * |
| 395 | * NOTE: |
| 396 | * |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 397 | * For single-mount semantics, devpts cannot use mount_single(), |
| 398 | * because mount_single()/sget() find and use the super-block from |
Sukadev Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 399 | * the most recent mount of devpts. But that recent mount may be a |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 400 | * 'newinstance' mount and mount_single() would pick the newinstance |
Sukadev Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 401 | * super-block instead of the initial super-block. |
Sukadev Bhattiprolu | d4076ac | 2009-01-02 13:42:19 +0000 | [diff] [blame] | 402 | */ |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 403 | static struct dentry *devpts_mount(struct file_system_type *fs_type, |
| 404 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Sukadev Bhattiprolu | 482984f | 2009-03-07 10:14:41 -0800 | [diff] [blame] | 406 | int error; |
| 407 | struct pts_mount_opts opts; |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 408 | struct super_block *s; |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 409 | |
Sukadev Bhattiprolu | 1f71ebe | 2009-05-14 19:38:24 -0700 | [diff] [blame] | 410 | error = parse_mount_options(data, PARSE_MOUNT, &opts); |
| 411 | if (error) |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 412 | return ERR_PTR(error); |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 413 | |
Sukadev Bhattiprolu | 482984f | 2009-03-07 10:14:41 -0800 | [diff] [blame] | 414 | if (opts.newinstance) |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 415 | s = sget(fs_type, NULL, set_anon_super, NULL); |
Sukadev Bhattiprolu | 482984f | 2009-03-07 10:14:41 -0800 | [diff] [blame] | 416 | else |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 417 | s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL); |
Sukadev Bhattiprolu | 945cf2c | 2009-03-07 10:11:41 -0800 | [diff] [blame] | 418 | |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 419 | if (IS_ERR(s)) |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 420 | return ERR_CAST(s); |
Sukadev Bhattiprolu | 945cf2c | 2009-03-07 10:11:41 -0800 | [diff] [blame] | 421 | |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 422 | 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 Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 430 | memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts)); |
| 431 | |
| 432 | error = mknod_ptmx(s); |
Sukadev Bhattiprolu | 945cf2c | 2009-03-07 10:11:41 -0800 | [diff] [blame] | 433 | if (error) |
Al Viro | 8946807 | 2010-03-20 21:57:43 -0400 | [diff] [blame] | 434 | goto out_undo_sget; |
| 435 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 436 | return dget(s->s_root); |
Sukadev Bhattiprolu | 945cf2c | 2009-03-07 10:11:41 -0800 | [diff] [blame] | 437 | |
Sukadev Bhattiprolu | 1bd7903 | 2009-03-07 10:12:32 -0800 | [diff] [blame] | 438 | out_undo_sget: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 439 | deactivate_locked_super(s); |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 440 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
Sukadev Bhattiprolu | 482984f | 2009-03-07 10:14:41 -0800 | [diff] [blame] | 442 | |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 443 | #else |
| 444 | /* |
| 445 | * This supports only the legacy single-instance semantics (no |
| 446 | * multiple-instance semantics) |
| 447 | */ |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 448 | static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags, |
| 449 | const char *dev_name, void *data) |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 450 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 451 | return mount_single(fs_type, flags, data, devpts_fill_super); |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 452 | } |
| 453 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 455 | static void devpts_kill_sb(struct super_block *sb) |
| 456 | { |
| 457 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 458 | |
| 459 | kfree(fsi); |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame] | 460 | kill_litter_super(sb); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | static struct file_system_type devpts_fs_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | .name = "devpts", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 465 | .mount = devpts_mount, |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 466 | .kill_sb = devpts_kill_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | /* |
| 470 | * The normal naming convention is simply /dev/pts/<number>; this conforms |
| 471 | * to the System V naming convention |
| 472 | */ |
| 473 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 474 | int devpts_new_index(struct inode *ptmx_inode) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 475 | { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 476 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
| 477 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 478 | int index; |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 479 | int ida_ret; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 480 | |
| 481 | retry: |
Alan Cox | 835aa44 | 2009-01-02 13:42:48 +0000 | [diff] [blame] | 482 | if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 483 | return -ENOMEM; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 484 | |
| 485 | mutex_lock(&allocated_ptys_lock); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 486 | ida_ret = ida_get_new(&fsi->allocated_ptys, &index); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 487 | if (ida_ret < 0) { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 488 | mutex_unlock(&allocated_ptys_lock); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 489 | if (ida_ret == -EAGAIN) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 490 | goto retry; |
| 491 | return -EIO; |
| 492 | } |
| 493 | |
| 494 | if (index >= pty_limit) { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 495 | ida_remove(&fsi->allocated_ptys, index); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 496 | mutex_unlock(&allocated_ptys_lock); |
| 497 | return -EIO; |
| 498 | } |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 499 | pty_count++; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 500 | mutex_unlock(&allocated_ptys_lock); |
| 501 | return index; |
| 502 | } |
| 503 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 504 | void devpts_kill_index(struct inode *ptmx_inode, int idx) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 505 | { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 506 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
| 507 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 508 | |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 509 | mutex_lock(&allocated_ptys_lock); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 510 | ida_remove(&fsi->allocated_ptys, idx); |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 511 | pty_count--; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 512 | mutex_unlock(&allocated_ptys_lock); |
| 513 | } |
| 514 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 515 | int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Alan Cox | 835aa44 | 2009-01-02 13:42:48 +0000 | [diff] [blame] | 517 | /* tty layer puts index from devpts_new_index() in here */ |
| 518 | int number = tty->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | struct tty_driver *driver = tty->driver; |
| 520 | dev_t device = MKDEV(driver->major, driver->minor_start+number); |
| 521 | struct dentry *dentry; |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 522 | 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 Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 525 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 526 | struct pts_mount_opts *opts = &fsi->mount_opts; |
Andrey Vagin | aa597bc | 2011-02-08 00:14:52 +0300 | [diff] [blame] | 527 | int ret = 0; |
Sukadev Bhattiprolu | 89a52e1 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 528 | char s[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 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 Howells | d0eafc7 | 2009-01-02 13:44:49 +0000 | [diff] [blame] | 537 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 541 | init_special_inode(inode, S_IFCHR|opts->mode, device); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 542 | inode->i_private = tty; |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 543 | tty->driver_data = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Sukadev Bhattiprolu | 89a52e1 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 545 | sprintf(s, "%d", number); |
| 546 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 547 | mutex_lock(&root->d_inode->i_mutex); |
Sukadev Bhattiprolu | 89a52e1 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 548 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 549 | dentry = d_alloc_name(root, s); |
Andrey Vagin | b12d125 | 2011-03-22 16:35:11 -0700 | [diff] [blame] | 550 | if (dentry) { |
Sukadev Bhattiprolu | 89a52e1 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 551 | d_add(dentry, inode); |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 552 | fsnotify_create(root->d_inode, dentry); |
Andrey Vagin | aa597bc | 2011-02-08 00:14:52 +0300 | [diff] [blame] | 553 | } else { |
| 554 | iput(inode); |
| 555 | ret = -ENOMEM; |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 558 | mutex_unlock(&root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Andrey Vagin | aa597bc | 2011-02-08 00:14:52 +0300 | [diff] [blame] | 560 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 563 | struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Sukadev Bhattiprolu | edfacdd | 2009-11-17 18:35:43 -0800 | [diff] [blame] | 565 | struct dentry *dentry; |
| 566 | struct tty_struct *tty; |
| 567 | |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 568 | BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Sukadev Bhattiprolu | edfacdd | 2009-11-17 18:35:43 -0800 | [diff] [blame] | 570 | /* 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 Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 576 | if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
Sukadev Bhattiprolu | edfacdd | 2009-11-17 18:35:43 -0800 | [diff] [blame] | 577 | tty = (struct tty_struct *)pts_inode->i_private; |
| 578 | |
| 579 | dput(dentry); |
| 580 | |
| 581 | return tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 584 | void devpts_pty_kill(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 586 | struct inode *inode = tty->driver_data; |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 587 | struct super_block *sb = pts_sb_from_inode(inode); |
| 588 | struct dentry *root = sb->s_root; |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 589 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 591 | BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
| 592 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 593 | mutex_lock(&root->d_inode->i_mutex); |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 594 | |
| 595 | dentry = d_find_alias(inode); |
Sukadev Bhattiprolu | 2a1b2dc | 2009-01-02 13:42:27 +0000 | [diff] [blame] | 596 | |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 597 | drop_nlink(inode); |
Andrey Vagin | aa597bc | 2011-02-08 00:14:52 +0300 | [diff] [blame] | 598 | d_delete(dentry); |
| 599 | dput(dentry); /* d_alloc_name() in devpts_pty_new() */ |
Alan Cox | 835aa44 | 2009-01-02 13:42:48 +0000 | [diff] [blame] | 600 | dput(dentry); /* d_find_alias above */ |
Andrey Vagin | aa597bc | 2011-02-08 00:14:52 +0300 | [diff] [blame] | 601 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 602 | mutex_unlock(&root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static int __init init_devpts_fs(void) |
| 606 | { |
| 607 | int err = register_filesystem(&devpts_fs_type); |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 608 | struct ctl_table_header *table; |
| 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (!err) { |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 611 | table = register_sysctl_table(pty_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | devpts_mnt = kern_mount(&devpts_fs_type); |
Alan Cox | 93d5581 | 2009-06-11 14:03:55 +0100 | [diff] [blame] | 613 | if (IS_ERR(devpts_mnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | err = PTR_ERR(devpts_mnt); |
Alan Cox | 93d5581 | 2009-06-11 14:03:55 +0100 | [diff] [blame] | 615 | unregister_filesystem(&devpts_fs_type); |
Konstantin Khlebnikov | a4834c1 | 2012-01-05 13:06:02 +0400 | [diff] [blame^] | 616 | unregister_sysctl_table(table); |
Alan Cox | 93d5581 | 2009-06-11 14:03:55 +0100 | [diff] [blame] | 617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | return err; |
| 620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | module_init(init_devpts_fs) |