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> |
| 18 | #include <linux/mount.h> |
| 19 | #include <linux/tty.h> |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 20 | #include <linux/mutex.h> |
| 21 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/devpts_fs.h> |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 23 | #include <linux/parser.h> |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define DEVPTS_SUPER_MAGIC 0x1cd1 |
| 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 | |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 39 | extern int pty_limit; /* Config limit on Unix98 ptys */ |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 40 | static DEFINE_MUTEX(allocated_ptys_lock); |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static struct vfsmount *devpts_mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 44 | struct pts_mount_opts { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | int setuid; |
| 46 | int setgid; |
| 47 | uid_t uid; |
| 48 | gid_t gid; |
| 49 | umode_t mode; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 50 | umode_t ptmxmode; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 51 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 53 | enum { |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 54 | Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 55 | Opt_err |
| 56 | }; |
| 57 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 58 | static const match_table_t tokens = { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 59 | {Opt_uid, "uid=%u"}, |
| 60 | {Opt_gid, "gid=%u"}, |
| 61 | {Opt_mode, "mode=%o"}, |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 62 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 63 | {Opt_ptmxmode, "ptmxmode=%o"}, |
| 64 | #endif |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 65 | {Opt_err, NULL} |
| 66 | }; |
| 67 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 68 | struct pts_fs_info { |
| 69 | struct ida allocated_ptys; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 70 | struct pts_mount_opts mount_opts; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 71 | struct dentry *ptmx_dentry; |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb) |
| 75 | { |
| 76 | return sb->s_fs_info; |
| 77 | } |
| 78 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 79 | static 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 | |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 87 | static int parse_mount_options(char *data, struct pts_mount_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 89 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 91 | opts->setuid = 0; |
| 92 | opts->setgid = 0; |
| 93 | opts->uid = 0; |
| 94 | opts->gid = 0; |
| 95 | opts->mode = DEVPTS_DEFAULT_MODE; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 96 | opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 97 | |
| 98 | while ((p = strsep(&data, ",")) != NULL) { |
| 99 | substring_t args[MAX_OPT_ARGS]; |
| 100 | int token; |
| 101 | int option; |
| 102 | |
| 103 | if (!*p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | continue; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 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; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 111 | opts->uid = option; |
| 112 | opts->setuid = 1; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 113 | break; |
| 114 | case Opt_gid: |
| 115 | if (match_int(&args[0], &option)) |
| 116 | return -EINVAL; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 117 | opts->gid = option; |
| 118 | opts->setgid = 1; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 119 | break; |
| 120 | case Opt_mode: |
| 121 | if (match_octal(&args[0], &option)) |
| 122 | return -EINVAL; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 123 | opts->mode = option & S_IALLUGO; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 124 | break; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 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 |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 132 | default: |
| 133 | printk(KERN_ERR "devpts: called with bogus options\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | } |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 141 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 142 | static int mknod_ptmx(struct super_block *sb) |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 143 | { |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 144 | int mode; |
| 145 | int rc = -ENOMEM; |
| 146 | struct dentry *dentry; |
| 147 | struct inode *inode; |
| 148 | struct dentry *root = sb->s_root; |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 149 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 150 | struct pts_mount_opts *opts = &fsi->mount_opts; |
| 151 | |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 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); |
| 191 | out: |
| 192 | mutex_unlock(&root->d_inode->i_mutex); |
| 193 | return rc; |
| 194 | } |
| 195 | |
| 196 | static 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 |
| 205 | static inline void update_ptmx_mode(struct pts_fs_info *fsi) |
| 206 | { |
| 207 | return; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | static int devpts_remount(struct super_block *sb, int *flags, char *data) |
| 212 | { |
| 213 | int err; |
| 214 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 215 | struct pts_mount_opts *opts = &fsi->mount_opts; |
| 216 | |
| 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; |
Sukadev Bhattiprolu | 53af8ee | 2009-01-02 13:41:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 230 | static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 231 | { |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 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); |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 240 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
| 241 | seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode); |
| 242 | #endif |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 247 | static const struct super_operations devpts_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | .statfs = simple_statfs, |
| 249 | .remount_fs = devpts_remount, |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 250 | .show_options = devpts_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 253 | static 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); |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 262 | fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 263 | fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 264 | |
| 265 | return fsi; |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | static int |
| 269 | devpts_fill_super(struct super_block *s, void *data, int silent) |
| 270 | { |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 271 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | s->s_time_gran = 1; |
| 278 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 279 | s->s_fs_info = new_pts_fs_info(); |
| 280 | if (!s->s_fs_info) |
| 281 | goto fail; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | inode = new_inode(s); |
| 284 | if (!inode) |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 285 | goto free_fsi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | inode->i_ino = 1; |
| 287 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 288 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 295 | s->s_root = d_alloc_root(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (s->s_root) |
| 297 | return 0; |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | printk("devpts: get root dentry failed\n"); |
| 300 | iput(inode); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 301 | |
| 302 | free_fsi: |
| 303 | kfree(s->s_fs_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | fail: |
| 305 | return -ENOMEM; |
| 306 | } |
| 307 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 308 | static int devpts_get_sb(struct file_system_type *fs_type, |
| 309 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 311 | return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 314 | static void devpts_kill_sb(struct super_block *sb) |
| 315 | { |
| 316 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 317 | |
| 318 | kfree(fsi); |
Sukadev Bhattiprolu | 1f8f1e2 | 2009-01-02 13:42:02 +0000 | [diff] [blame^] | 319 | kill_litter_super(sb); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | static struct file_system_type devpts_fs_type = { |
| 323 | .owner = THIS_MODULE, |
| 324 | .name = "devpts", |
| 325 | .get_sb = devpts_get_sb, |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 326 | .kill_sb = devpts_kill_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* |
| 330 | * The normal naming convention is simply /dev/pts/<number>; this conforms |
| 331 | * to the System V naming convention |
| 332 | */ |
| 333 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 334 | int devpts_new_index(struct inode *ptmx_inode) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 335 | { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 336 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
| 337 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 338 | int index; |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 339 | int ida_ret; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 340 | |
| 341 | retry: |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 342 | if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 343 | return -ENOMEM; |
| 344 | } |
| 345 | |
| 346 | mutex_lock(&allocated_ptys_lock); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 347 | ida_ret = ida_get_new(&fsi->allocated_ptys, &index); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 348 | if (ida_ret < 0) { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 349 | mutex_unlock(&allocated_ptys_lock); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 350 | if (ida_ret == -EAGAIN) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 351 | goto retry; |
| 352 | return -EIO; |
| 353 | } |
| 354 | |
| 355 | if (index >= pty_limit) { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 356 | ida_remove(&fsi->allocated_ptys, index); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 357 | mutex_unlock(&allocated_ptys_lock); |
| 358 | return -EIO; |
| 359 | } |
| 360 | mutex_unlock(&allocated_ptys_lock); |
| 361 | return index; |
| 362 | } |
| 363 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 364 | void devpts_kill_index(struct inode *ptmx_inode, int idx) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 365 | { |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 366 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
| 367 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 368 | |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 369 | mutex_lock(&allocated_ptys_lock); |
Sukadev Bhattiprolu | e76b7c0 | 2009-01-02 13:41:21 +0000 | [diff] [blame] | 370 | ida_remove(&fsi->allocated_ptys, idx); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 371 | mutex_unlock(&allocated_ptys_lock); |
| 372 | } |
| 373 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 374 | int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 376 | int number = tty->index; /* tty layer puts index from devpts_new_index() in here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct tty_driver *driver = tty->driver; |
| 378 | dev_t device = MKDEV(driver->major, driver->minor_start+number); |
| 379 | struct dentry *dentry; |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 380 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
| 381 | struct inode *inode = new_inode(sb); |
| 382 | struct dentry *root = sb->s_root; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 383 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
| 384 | struct pts_mount_opts *opts = &fsi->mount_opts; |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 385 | char s[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | /* We're supposed to be given the slave end of a pty */ |
| 388 | BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY); |
| 389 | BUG_ON(driver->subtype != PTY_TYPE_SLAVE); |
| 390 | |
| 391 | if (!inode) |
| 392 | return -ENOMEM; |
| 393 | |
| 394 | inode->i_ino = number+2; |
David Howells | ec4c2aa | 2008-11-14 10:38:49 +1100 | [diff] [blame] | 395 | inode->i_uid = config.setuid ? config.uid : current_fsuid(); |
| 396 | inode->i_gid = config.setgid ? config.gid : current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Sukadev Bhattiprolu | 31af0ab | 2009-01-02 13:41:33 +0000 | [diff] [blame] | 398 | init_special_inode(inode, S_IFCHR|opts->mode, device); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 399 | inode->i_private = tty; |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 400 | tty->driver_data = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 402 | sprintf(s, "%d", number); |
| 403 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 404 | mutex_lock(&root->d_inode->i_mutex); |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 405 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 406 | dentry = d_alloc_name(root, s); |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 407 | if (!IS_ERR(dentry)) { |
| 408 | d_add(dentry, inode); |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 409 | fsnotify_create(root->d_inode, dentry); |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 412 | mutex_unlock(&root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 417 | struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 419 | BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 421 | if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
| 422 | return (struct tty_struct *)pts_inode->i_private; |
| 423 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 426 | void devpts_pty_kill(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 428 | struct inode *inode = tty->driver_data; |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 429 | struct super_block *sb = pts_sb_from_inode(inode); |
| 430 | struct dentry *root = sb->s_root; |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 431 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 433 | BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
| 434 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 435 | mutex_lock(&root->d_inode->i_mutex); |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 436 | |
| 437 | dentry = d_find_alias(inode); |
| 438 | if (dentry && !IS_ERR(dentry)) { |
| 439 | inode->i_nlink--; |
| 440 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | dput(dentry); |
| 442 | } |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 443 | |
Sukadev Bhattiprolu | 59e55e6 | 2009-01-02 13:41:11 +0000 | [diff] [blame] | 444 | mutex_unlock(&root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static int __init init_devpts_fs(void) |
| 448 | { |
| 449 | int err = register_filesystem(&devpts_fs_type); |
| 450 | if (!err) { |
| 451 | devpts_mnt = kern_mount(&devpts_fs_type); |
| 452 | if (IS_ERR(devpts_mnt)) |
| 453 | err = PTR_ERR(devpts_mnt); |
| 454 | } |
| 455 | return err; |
| 456 | } |
| 457 | |
| 458 | static void __exit exit_devpts_fs(void) |
| 459 | { |
| 460 | unregister_filesystem(&devpts_fs_type); |
| 461 | mntput(devpts_mnt); |
| 462 | } |
| 463 | |
| 464 | module_init(init_devpts_fs) |
| 465 | module_exit(exit_devpts_fs) |
| 466 | MODULE_LICENSE("GPL"); |