Commit | Line | Data |
---|---|---|
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> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
1da177e4 LT |
19 | #include <linux/mount.h> |
20 | #include <linux/tty.h> | |
718a9163 | 21 | #include <linux/mutex.h> |
1fd7317d | 22 | #include <linux/magic.h> |
718a9163 | 23 | #include <linux/idr.h> |
1da177e4 | 24 | #include <linux/devpts_fs.h> |
7a673c6b | 25 | #include <linux/parser.h> |
3972b7f6 | 26 | #include <linux/fsnotify.h> |
b87a267e | 27 | #include <linux/seq_file.h> |
1da177e4 | 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 | |
a4834c10 KK |
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; | |
e9aba515 | 44 | static int pty_reserve = NR_UNIX98_PTY_RESERVE; |
a4834c10 | 45 | static int pty_limit_min; |
e9aba515 | 46 | static int pty_limit_max = INT_MAX; |
a4834c10 KK |
47 | static int pty_count; |
48 | ||
49 | static struct ctl_table pty_table[] = { | |
50 | { | |
51 | .procname = "max", | |
52 | .maxlen = sizeof(int), | |
53 | .mode = 0644, | |
54 | .data = &pty_limit, | |
55 | .proc_handler = proc_dointvec_minmax, | |
56 | .extra1 = &pty_limit_min, | |
57 | .extra2 = &pty_limit_max, | |
e9aba515 KK |
58 | }, { |
59 | .procname = "reserve", | |
60 | .maxlen = sizeof(int), | |
61 | .mode = 0644, | |
62 | .data = &pty_reserve, | |
63 | .proc_handler = proc_dointvec_minmax, | |
64 | .extra1 = &pty_limit_min, | |
65 | .extra2 = &pty_limit_max, | |
a4834c10 KK |
66 | }, { |
67 | .procname = "nr", | |
68 | .maxlen = sizeof(int), | |
69 | .mode = 0444, | |
70 | .data = &pty_count, | |
71 | .proc_handler = proc_dointvec, | |
72 | }, | |
73 | {} | |
74 | }; | |
75 | ||
76 | static struct ctl_table pty_kern_table[] = { | |
77 | { | |
78 | .procname = "pty", | |
79 | .mode = 0555, | |
80 | .child = pty_table, | |
81 | }, | |
82 | {} | |
83 | }; | |
84 | ||
85 | static struct ctl_table pty_root_table[] = { | |
86 | { | |
87 | .procname = "kernel", | |
88 | .mode = 0555, | |
89 | .child = pty_kern_table, | |
90 | }, | |
91 | {} | |
92 | }; | |
93 | ||
718a9163 SB |
94 | static DEFINE_MUTEX(allocated_ptys_lock); |
95 | ||
1da177e4 | 96 | static struct vfsmount *devpts_mnt; |
1da177e4 | 97 | |
31af0abb | 98 | struct pts_mount_opts { |
1da177e4 LT |
99 | int setuid; |
100 | int setgid; | |
101 | uid_t uid; | |
102 | gid_t gid; | |
103 | umode_t mode; | |
1f8f1e29 | 104 | umode_t ptmxmode; |
2a1b2dc0 | 105 | int newinstance; |
e9aba515 | 106 | int max; |
31af0abb | 107 | }; |
1da177e4 | 108 | |
7a673c6b | 109 | enum { |
e9aba515 | 110 | Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance, Opt_max, |
7a673c6b DP |
111 | Opt_err |
112 | }; | |
113 | ||
a447c093 | 114 | static const match_table_t tokens = { |
7a673c6b DP |
115 | {Opt_uid, "uid=%u"}, |
116 | {Opt_gid, "gid=%u"}, | |
117 | {Opt_mode, "mode=%o"}, | |
1f8f1e29 SB |
118 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
119 | {Opt_ptmxmode, "ptmxmode=%o"}, | |
2a1b2dc0 | 120 | {Opt_newinstance, "newinstance"}, |
e9aba515 | 121 | {Opt_max, "max=%d"}, |
1f8f1e29 | 122 | #endif |
7a673c6b DP |
123 | {Opt_err, NULL} |
124 | }; | |
125 | ||
e76b7c01 SB |
126 | struct pts_fs_info { |
127 | struct ida allocated_ptys; | |
31af0abb | 128 | struct pts_mount_opts mount_opts; |
1f8f1e29 | 129 | struct dentry *ptmx_dentry; |
e76b7c01 SB |
130 | }; |
131 | ||
132 | static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb) | |
133 | { | |
134 | return sb->s_fs_info; | |
135 | } | |
136 | ||
59e55e6c SB |
137 | static inline struct super_block *pts_sb_from_inode(struct inode *inode) |
138 | { | |
2a1b2dc0 | 139 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
59e55e6c SB |
140 | if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
141 | return inode->i_sb; | |
2a1b2dc0 | 142 | #endif |
59e55e6c SB |
143 | return devpts_mnt->mnt_sb; |
144 | } | |
145 | ||
2a1b2dc0 SB |
146 | #define PARSE_MOUNT 0 |
147 | #define PARSE_REMOUNT 1 | |
148 | ||
1f71ebed SB |
149 | /* |
150 | * parse_mount_options(): | |
151 | * Set @opts to mount options specified in @data. If an option is not | |
152 | * specified in @data, set it to its default value. The exception is | |
153 | * 'newinstance' option which can only be set/cleared on a mount (i.e. | |
154 | * cannot be changed during remount). | |
155 | * | |
156 | * Note: @data may be NULL (in which case all options are set to default). | |
157 | */ | |
2a1b2dc0 | 158 | static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts) |
1da177e4 | 159 | { |
7a673c6b DP |
160 | char *p; |
161 | ||
31af0abb SB |
162 | opts->setuid = 0; |
163 | opts->setgid = 0; | |
164 | opts->uid = 0; | |
165 | opts->gid = 0; | |
166 | opts->mode = DEVPTS_DEFAULT_MODE; | |
1f8f1e29 | 167 | opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
e9aba515 | 168 | opts->max = NR_UNIX98_PTY_MAX; |
7a673c6b | 169 | |
2a1b2dc0 SB |
170 | /* newinstance makes sense only on initial mount */ |
171 | if (op == PARSE_MOUNT) | |
172 | opts->newinstance = 0; | |
173 | ||
7a673c6b DP |
174 | while ((p = strsep(&data, ",")) != NULL) { |
175 | substring_t args[MAX_OPT_ARGS]; | |
176 | int token; | |
177 | int option; | |
178 | ||
179 | if (!*p) | |
1da177e4 | 180 | continue; |
7a673c6b DP |
181 | |
182 | token = match_token(p, tokens, args); | |
183 | switch (token) { | |
184 | case Opt_uid: | |
185 | if (match_int(&args[0], &option)) | |
186 | return -EINVAL; | |
31af0abb SB |
187 | opts->uid = option; |
188 | opts->setuid = 1; | |
7a673c6b DP |
189 | break; |
190 | case Opt_gid: | |
191 | if (match_int(&args[0], &option)) | |
192 | return -EINVAL; | |
31af0abb SB |
193 | opts->gid = option; |
194 | opts->setgid = 1; | |
7a673c6b DP |
195 | break; |
196 | case Opt_mode: | |
197 | if (match_octal(&args[0], &option)) | |
198 | return -EINVAL; | |
31af0abb | 199 | opts->mode = option & S_IALLUGO; |
7a673c6b | 200 | break; |
1f8f1e29 SB |
201 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
202 | case Opt_ptmxmode: | |
203 | if (match_octal(&args[0], &option)) | |
204 | return -EINVAL; | |
205 | opts->ptmxmode = option & S_IALLUGO; | |
206 | break; | |
2a1b2dc0 SB |
207 | case Opt_newinstance: |
208 | /* newinstance makes sense only on initial mount */ | |
209 | if (op == PARSE_MOUNT) | |
210 | opts->newinstance = 1; | |
211 | break; | |
e9aba515 KK |
212 | case Opt_max: |
213 | if (match_int(&args[0], &option) || | |
214 | option < 0 || option > NR_UNIX98_PTY_MAX) | |
215 | return -EINVAL; | |
216 | opts->max = option; | |
217 | break; | |
1f8f1e29 | 218 | #endif |
7a673c6b DP |
219 | default: |
220 | printk(KERN_ERR "devpts: called with bogus options\n"); | |
1da177e4 LT |
221 | return -EINVAL; |
222 | } | |
223 | } | |
1da177e4 LT |
224 | |
225 | return 0; | |
226 | } | |
227 | ||
1f8f1e29 SB |
228 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
229 | static int mknod_ptmx(struct super_block *sb) | |
230 | { | |
231 | int mode; | |
232 | int rc = -ENOMEM; | |
233 | struct dentry *dentry; | |
234 | struct inode *inode; | |
235 | struct dentry *root = sb->s_root; | |
236 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
237 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
238 | ||
239 | mutex_lock(&root->d_inode->i_mutex); | |
240 | ||
241 | /* If we have already created ptmx node, return */ | |
242 | if (fsi->ptmx_dentry) { | |
243 | rc = 0; | |
244 | goto out; | |
245 | } | |
246 | ||
247 | dentry = d_alloc_name(root, "ptmx"); | |
248 | if (!dentry) { | |
249 | printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n"); | |
250 | goto out; | |
251 | } | |
252 | ||
253 | /* | |
254 | * Create a new 'ptmx' node in this mount of devpts. | |
255 | */ | |
256 | inode = new_inode(sb); | |
257 | if (!inode) { | |
258 | printk(KERN_ERR "Unable to alloc inode for ptmx node\n"); | |
259 | dput(dentry); | |
260 | goto out; | |
261 | } | |
262 | ||
263 | inode->i_ino = 2; | |
1f8f1e29 SB |
264 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
265 | ||
266 | mode = S_IFCHR|opts->ptmxmode; | |
267 | init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2)); | |
268 | ||
269 | d_add(dentry, inode); | |
270 | ||
271 | fsi->ptmx_dentry = dentry; | |
272 | rc = 0; | |
1f8f1e29 SB |
273 | out: |
274 | mutex_unlock(&root->d_inode->i_mutex); | |
275 | return rc; | |
276 | } | |
277 | ||
278 | static void update_ptmx_mode(struct pts_fs_info *fsi) | |
279 | { | |
280 | struct inode *inode; | |
281 | if (fsi->ptmx_dentry) { | |
282 | inode = fsi->ptmx_dentry->d_inode; | |
283 | inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode; | |
284 | } | |
285 | } | |
286 | #else | |
287 | static inline void update_ptmx_mode(struct pts_fs_info *fsi) | |
288 | { | |
289 | return; | |
290 | } | |
291 | #endif | |
292 | ||
53af8ee4 SB |
293 | static int devpts_remount(struct super_block *sb, int *flags, char *data) |
294 | { | |
1f8f1e29 | 295 | int err; |
53af8ee4 SB |
296 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
297 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
298 | ||
2a1b2dc0 | 299 | err = parse_mount_options(data, PARSE_REMOUNT, opts); |
1f8f1e29 SB |
300 | |
301 | /* | |
302 | * parse_mount_options() restores options to default values | |
303 | * before parsing and may have changed ptmxmode. So, update the | |
304 | * mode in the inode too. Bogus options don't fail the remount, | |
305 | * so do this even on error return. | |
306 | */ | |
307 | update_ptmx_mode(fsi); | |
308 | ||
309 | return err; | |
53af8ee4 SB |
310 | } |
311 | ||
34c80b1d | 312 | static int devpts_show_options(struct seq_file *seq, struct dentry *root) |
b87a267e | 313 | { |
34c80b1d | 314 | struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb); |
31af0abb SB |
315 | struct pts_mount_opts *opts = &fsi->mount_opts; |
316 | ||
317 | if (opts->setuid) | |
318 | seq_printf(seq, ",uid=%u", opts->uid); | |
319 | if (opts->setgid) | |
320 | seq_printf(seq, ",gid=%u", opts->gid); | |
321 | seq_printf(seq, ",mode=%03o", opts->mode); | |
1f8f1e29 SB |
322 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
323 | seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode); | |
e9aba515 KK |
324 | if (opts->max < NR_UNIX98_PTY_MAX) |
325 | seq_printf(seq, ",max=%d", opts->max); | |
1f8f1e29 | 326 | #endif |
b87a267e MS |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
ee9b6d61 | 331 | static const struct super_operations devpts_sops = { |
1da177e4 LT |
332 | .statfs = simple_statfs, |
333 | .remount_fs = devpts_remount, | |
b87a267e | 334 | .show_options = devpts_show_options, |
1da177e4 LT |
335 | }; |
336 | ||
e76b7c01 SB |
337 | static void *new_pts_fs_info(void) |
338 | { | |
339 | struct pts_fs_info *fsi; | |
340 | ||
341 | fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL); | |
342 | if (!fsi) | |
343 | return NULL; | |
344 | ||
345 | ida_init(&fsi->allocated_ptys); | |
31af0abb | 346 | fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE; |
1f8f1e29 | 347 | fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE; |
e76b7c01 SB |
348 | |
349 | return fsi; | |
350 | } | |
351 | ||
1da177e4 LT |
352 | static int |
353 | devpts_fill_super(struct super_block *s, void *data, int silent) | |
354 | { | |
1f8f1e29 | 355 | struct inode *inode; |
1da177e4 LT |
356 | |
357 | s->s_blocksize = 1024; | |
358 | s->s_blocksize_bits = 10; | |
359 | s->s_magic = DEVPTS_SUPER_MAGIC; | |
360 | s->s_op = &devpts_sops; | |
1da177e4 LT |
361 | s->s_time_gran = 1; |
362 | ||
e76b7c01 SB |
363 | s->s_fs_info = new_pts_fs_info(); |
364 | if (!s->s_fs_info) | |
365 | goto fail; | |
366 | ||
1da177e4 LT |
367 | inode = new_inode(s); |
368 | if (!inode) | |
3850aba7 | 369 | goto fail; |
1da177e4 LT |
370 | inode->i_ino = 1; |
371 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
1da177e4 LT |
372 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; |
373 | inode->i_op = &simple_dir_inode_operations; | |
374 | inode->i_fop = &simple_dir_operations; | |
bfe86848 | 375 | set_nlink(inode, 2); |
1da177e4 | 376 | |
48fde701 | 377 | s->s_root = d_make_root(inode); |
1da177e4 LT |
378 | if (s->s_root) |
379 | return 0; | |
1f8f1e29 | 380 | |
835aa440 | 381 | printk(KERN_ERR "devpts: get root dentry failed\n"); |
e76b7c01 | 382 | |
1da177e4 LT |
383 | fail: |
384 | return -ENOMEM; | |
385 | } | |
386 | ||
8c056e5b | 387 | #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES |
d4076ac5 SB |
388 | static int compare_init_pts_sb(struct super_block *s, void *p) |
389 | { | |
390 | if (devpts_mnt) | |
391 | return devpts_mnt->mnt_sb == s; | |
2a1b2dc0 SB |
392 | return 0; |
393 | } | |
394 | ||
2a1b2dc0 | 395 | /* |
fc14f2fe | 396 | * devpts_mount() |
1bd79035 SB |
397 | * |
398 | * If the '-o newinstance' mount option was specified, mount a new | |
399 | * (private) instance of devpts. PTYs created in this instance are | |
400 | * independent of the PTYs in other devpts instances. | |
401 | * | |
402 | * If the '-o newinstance' option was not specified, mount/remount the | |
403 | * initial kernel mount of devpts. This type of mount gives the | |
404 | * legacy, single-instance semantics. | |
289f00e2 | 405 | * |
1bd79035 SB |
406 | * The 'newinstance' option is needed to support multiple namespace |
407 | * semantics in devpts while preserving backward compatibility of the | |
408 | * current 'single-namespace' semantics. i.e all mounts of devpts | |
409 | * without the 'newinstance' mount option should bind to the initial | |
fc14f2fe | 410 | * kernel mount, like mount_single(). |
d4076ac5 | 411 | * |
1bd79035 | 412 | * Mounts with 'newinstance' option create a new, private namespace. |
d4076ac5 | 413 | * |
1bd79035 | 414 | * NOTE: |
d4076ac5 | 415 | * |
fc14f2fe AV |
416 | * For single-mount semantics, devpts cannot use mount_single(), |
417 | * because mount_single()/sget() find and use the super-block from | |
d4076ac5 | 418 | * the most recent mount of devpts. But that recent mount may be a |
fc14f2fe | 419 | * 'newinstance' mount and mount_single() would pick the newinstance |
d4076ac5 | 420 | * super-block instead of the initial super-block. |
d4076ac5 | 421 | */ |
fc14f2fe AV |
422 | static struct dentry *devpts_mount(struct file_system_type *fs_type, |
423 | int flags, const char *dev_name, void *data) | |
1da177e4 | 424 | { |
482984f0 SB |
425 | int error; |
426 | struct pts_mount_opts opts; | |
1bd79035 | 427 | struct super_block *s; |
2a1b2dc0 | 428 | |
1f71ebed SB |
429 | error = parse_mount_options(data, PARSE_MOUNT, &opts); |
430 | if (error) | |
fc14f2fe | 431 | return ERR_PTR(error); |
2a1b2dc0 | 432 | |
482984f0 | 433 | if (opts.newinstance) |
1bd79035 | 434 | s = sget(fs_type, NULL, set_anon_super, NULL); |
482984f0 | 435 | else |
1bd79035 | 436 | s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL); |
945cf2c7 | 437 | |
1bd79035 | 438 | if (IS_ERR(s)) |
fc14f2fe | 439 | return ERR_CAST(s); |
1bd79035 SB |
440 | |
441 | if (!s->s_root) { | |
442 | s->s_flags = flags; | |
443 | error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0); | |
444 | if (error) | |
445 | goto out_undo_sget; | |
446 | s->s_flags |= MS_ACTIVE; | |
447 | } | |
448 | ||
1bd79035 | 449 | memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts)); |
945cf2c7 | 450 | |
1bd79035 | 451 | error = mknod_ptmx(s); |
945cf2c7 | 452 | if (error) |
89468071 | 453 | goto out_undo_sget; |
945cf2c7 | 454 | |
fc14f2fe | 455 | return dget(s->s_root); |
1bd79035 SB |
456 | |
457 | out_undo_sget: | |
6f5bbff9 | 458 | deactivate_locked_super(s); |
fc14f2fe | 459 | return ERR_PTR(error); |
1da177e4 | 460 | } |
482984f0 | 461 | |
2a1b2dc0 SB |
462 | #else |
463 | /* | |
464 | * This supports only the legacy single-instance semantics (no | |
465 | * multiple-instance semantics) | |
466 | */ | |
fc14f2fe AV |
467 | static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags, |
468 | const char *dev_name, void *data) | |
2a1b2dc0 | 469 | { |
fc14f2fe | 470 | return mount_single(fs_type, flags, data, devpts_fill_super); |
2a1b2dc0 SB |
471 | } |
472 | #endif | |
1da177e4 | 473 | |
e76b7c01 SB |
474 | static void devpts_kill_sb(struct super_block *sb) |
475 | { | |
476 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
477 | ||
478 | kfree(fsi); | |
1f8f1e29 | 479 | kill_litter_super(sb); |
e76b7c01 SB |
480 | } |
481 | ||
1da177e4 | 482 | static struct file_system_type devpts_fs_type = { |
1da177e4 | 483 | .name = "devpts", |
fc14f2fe | 484 | .mount = devpts_mount, |
e76b7c01 | 485 | .kill_sb = devpts_kill_sb, |
1da177e4 LT |
486 | }; |
487 | ||
488 | /* | |
489 | * The normal naming convention is simply /dev/pts/<number>; this conforms | |
490 | * to the System V naming convention | |
491 | */ | |
492 | ||
15f1a633 | 493 | int devpts_new_index(struct inode *ptmx_inode) |
718a9163 | 494 | { |
e76b7c01 SB |
495 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
496 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
718a9163 | 497 | int index; |
7ee7c12b | 498 | int ida_ret; |
718a9163 SB |
499 | |
500 | retry: | |
835aa440 | 501 | if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) |
718a9163 | 502 | return -ENOMEM; |
718a9163 SB |
503 | |
504 | mutex_lock(&allocated_ptys_lock); | |
e9aba515 KK |
505 | if (pty_count >= pty_limit - |
506 | (fsi->mount_opts.newinstance ? pty_reserve : 0)) { | |
507 | mutex_unlock(&allocated_ptys_lock); | |
508 | return -ENOSPC; | |
509 | } | |
510 | ||
e76b7c01 | 511 | ida_ret = ida_get_new(&fsi->allocated_ptys, &index); |
7ee7c12b | 512 | if (ida_ret < 0) { |
718a9163 | 513 | mutex_unlock(&allocated_ptys_lock); |
7ee7c12b | 514 | if (ida_ret == -EAGAIN) |
718a9163 SB |
515 | goto retry; |
516 | return -EIO; | |
517 | } | |
518 | ||
e9aba515 | 519 | if (index >= fsi->mount_opts.max) { |
e76b7c01 | 520 | ida_remove(&fsi->allocated_ptys, index); |
718a9163 | 521 | mutex_unlock(&allocated_ptys_lock); |
e9aba515 | 522 | return -ENOSPC; |
718a9163 | 523 | } |
a4834c10 | 524 | pty_count++; |
718a9163 SB |
525 | mutex_unlock(&allocated_ptys_lock); |
526 | return index; | |
527 | } | |
528 | ||
15f1a633 | 529 | void devpts_kill_index(struct inode *ptmx_inode, int idx) |
718a9163 | 530 | { |
e76b7c01 SB |
531 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
532 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
533 | ||
718a9163 | 534 | mutex_lock(&allocated_ptys_lock); |
e76b7c01 | 535 | ida_remove(&fsi->allocated_ptys, idx); |
a4834c10 | 536 | pty_count--; |
718a9163 SB |
537 | mutex_unlock(&allocated_ptys_lock); |
538 | } | |
539 | ||
15f1a633 | 540 | int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty) |
1da177e4 | 541 | { |
835aa440 AC |
542 | /* tty layer puts index from devpts_new_index() in here */ |
543 | int number = tty->index; | |
1da177e4 LT |
544 | struct tty_driver *driver = tty->driver; |
545 | dev_t device = MKDEV(driver->major, driver->minor_start+number); | |
546 | struct dentry *dentry; | |
59e55e6c SB |
547 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
548 | struct inode *inode = new_inode(sb); | |
549 | struct dentry *root = sb->s_root; | |
31af0abb SB |
550 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
551 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
aa597bc1 | 552 | int ret = 0; |
89a52e10 | 553 | char s[12]; |
1da177e4 LT |
554 | |
555 | /* We're supposed to be given the slave end of a pty */ | |
556 | BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY); | |
557 | BUG_ON(driver->subtype != PTY_TYPE_SLAVE); | |
558 | ||
559 | if (!inode) | |
560 | return -ENOMEM; | |
561 | ||
d0eafc7d DH |
562 | inode->i_ino = number + 3; |
563 | inode->i_uid = opts->setuid ? opts->uid : current_fsuid(); | |
564 | inode->i_gid = opts->setgid ? opts->gid : current_fsgid(); | |
1da177e4 | 565 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
31af0abb | 566 | init_special_inode(inode, S_IFCHR|opts->mode, device); |
8e18e294 | 567 | inode->i_private = tty; |
a6f37daa | 568 | tty->driver_data = inode; |
1da177e4 | 569 | |
89a52e10 SB |
570 | sprintf(s, "%d", number); |
571 | ||
59e55e6c | 572 | mutex_lock(&root->d_inode->i_mutex); |
89a52e10 | 573 | |
59e55e6c | 574 | dentry = d_alloc_name(root, s); |
b12d1259 | 575 | if (dentry) { |
89a52e10 | 576 | d_add(dentry, inode); |
59e55e6c | 577 | fsnotify_create(root->d_inode, dentry); |
aa597bc1 AV |
578 | } else { |
579 | iput(inode); | |
580 | ret = -ENOMEM; | |
3972b7f6 | 581 | } |
1da177e4 | 582 | |
59e55e6c | 583 | mutex_unlock(&root->d_inode->i_mutex); |
1da177e4 | 584 | |
aa597bc1 | 585 | return ret; |
1da177e4 LT |
586 | } |
587 | ||
15f1a633 | 588 | struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number) |
1da177e4 | 589 | { |
edfacdd6 SB |
590 | struct dentry *dentry; |
591 | struct tty_struct *tty; | |
592 | ||
527b3e47 | 593 | BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
1da177e4 | 594 | |
edfacdd6 SB |
595 | /* Ensure dentry has not been deleted by devpts_pty_kill() */ |
596 | dentry = d_find_alias(pts_inode); | |
597 | if (!dentry) | |
598 | return NULL; | |
599 | ||
600 | tty = NULL; | |
527b3e47 | 601 | if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
edfacdd6 SB |
602 | tty = (struct tty_struct *)pts_inode->i_private; |
603 | ||
604 | dput(dentry); | |
605 | ||
606 | return tty; | |
1da177e4 LT |
607 | } |
608 | ||
15f1a633 | 609 | void devpts_pty_kill(struct tty_struct *tty) |
1da177e4 | 610 | { |
a6f37daa | 611 | struct inode *inode = tty->driver_data; |
59e55e6c SB |
612 | struct super_block *sb = pts_sb_from_inode(inode); |
613 | struct dentry *root = sb->s_root; | |
a6f37daa | 614 | struct dentry *dentry; |
1da177e4 | 615 | |
a6f37daa SB |
616 | BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
617 | ||
59e55e6c | 618 | mutex_lock(&root->d_inode->i_mutex); |
a6f37daa SB |
619 | |
620 | dentry = d_find_alias(inode); | |
a6f37daa | 621 | |
6d6b77f1 | 622 | drop_nlink(inode); |
aa597bc1 AV |
623 | d_delete(dentry); |
624 | dput(dentry); /* d_alloc_name() in devpts_pty_new() */ | |
835aa440 | 625 | dput(dentry); /* d_find_alias above */ |
aa597bc1 | 626 | |
59e55e6c | 627 | mutex_unlock(&root->d_inode->i_mutex); |
1da177e4 LT |
628 | } |
629 | ||
630 | static int __init init_devpts_fs(void) | |
631 | { | |
632 | int err = register_filesystem(&devpts_fs_type); | |
a4834c10 KK |
633 | struct ctl_table_header *table; |
634 | ||
1da177e4 | 635 | if (!err) { |
a4834c10 | 636 | table = register_sysctl_table(pty_root_table); |
1da177e4 | 637 | devpts_mnt = kern_mount(&devpts_fs_type); |
93d5581e | 638 | if (IS_ERR(devpts_mnt)) { |
1da177e4 | 639 | err = PTR_ERR(devpts_mnt); |
93d5581e | 640 | unregister_filesystem(&devpts_fs_type); |
a4834c10 | 641 | unregister_sysctl_table(table); |
93d5581e | 642 | } |
1da177e4 LT |
643 | } |
644 | return err; | |
645 | } | |
1da177e4 | 646 | module_init(init_devpts_fs) |