Skip to content

Commit 2a1b2dc

Browse files
Sukadev Bhattiprolutorvalds
authored andcommitted
Enable multiple instances of devpts
To support containers, allow multiple instances of devpts filesystem, such that indices of ptys allocated in one instance are independent of ptys allocated in other instances of devpts. But to preserve backward compatibility, enable this support for multiple instances only if: - CONFIG_DEVPTS_MULTIPLE_INSTANCES is set to Y, and - '-o newinstance' mount option is specified while mounting devpts To use multi-instance mount, a container startup script could: $ ns_exec -cm /bin/bash $ umount /dev/pts $ mount -t devpts -o newinstance lxcpts /dev/pts $ mount -o bind /dev/pts/ptmx /dev/ptmx $ /usr/sbin/sshd -p 1234 where 'ns_exec -cm /bin/bash' is calls clone() with CLONE_NEWNS flag and execs /bin/bash in the child process. A pty created by the sshd is not visible in the original mount of /dev/pts. USER-SPACE-IMPACT: - See Documentation/fs/devpts.txt (included in next patch) for user- space impact in multi-instance and mixed-mode operation. TODO: - Update mount(8), pts(4) man pages. Highlight impact of not redirecting /dev/ptmx to /dev/pts/ptmx after a multi-instance mount. Changelog[v6]: - [Dave Hansen] Use new get_init_pts_sb() interface - [Serge Hallyn] Don't bother displaying 'newinstance' in show_options - [Serge Hallyn] Use macros (PARSE_REMOUNT/PARSE_MOUNT) instead of 0/1. - [Serge Hallyn] Check error return from get_sb_single() (now get_init_pts_sb()) - devpts_pty_kill(): don't dput error dentries Changelog[v5]: - Move get_sb_ref() definition to earlier patch - Move usage info to Documentation/filesystems/devpts.txt (next patch) - Make ptmx node even in init_pts_ns, now that default mode is 0000 (defined in earlier patch, enabled here). - Cache ptmx dentry and use to update mode during remount (defined in earlier patch, enabled here). - Bugfix: explicitly ignore newinstance on remount (if newinstance was specified on remount of initial mount, it would be ignored but /proc/mounts would imply that the option was set) Changelog[v4]: - Update patch description to address H. Peter Anvin's comments - Consolidate multi-instance mode code under new config token, CONFIG_DEVPTS_MULTIPLE_INSTANCE. - Move usage-details from patch description to Documentation/fs/devpts.txt Changelog[v3]: - Rename new mount option to 'newinstance' - Create ptmx nodes only in 'newinstance' mounts - Bugfix: parse_mount_options() modifies @DaTa but since we need to parse the @DaTa twice (once in devpts_get_sb() and once during do_remount_sb()), parse a local copy of @DaTa in devpts_get_sb(). (restructured code in devpts_get_sb() to fix this) Changelog[v2]: - Support both single-mount and multiple-mount semantics and provide '-onewmnt' option to select the semantics. Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> Signed-off-by: Alan Cox <alan@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d4076ac commit 2a1b2dc

1 file changed

Lines changed: 163 additions & 7 deletions

File tree

‎fs/devpts/inode.c‎

Lines changed: 163 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ struct pts_mount_opts {
4848
gid_t gid;
4949
umode_t mode;
5050
umode_t ptmxmode;
51+
int newinstance;
5152
};
5253

5354
enum {
54-
Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode,
55+
Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance,
5556
Opt_err
5657
};
5758

@@ -61,6 +62,7 @@ static const match_table_t tokens = {
6162
{Opt_mode, "mode=%o"},
6263
#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
6364
{Opt_ptmxmode, "ptmxmode=%o"},
65+
{Opt_newinstance, "newinstance"},
6466
#endif
6567
{Opt_err, NULL}
6668
};
@@ -78,13 +80,17 @@ static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
7880

7981
static inline struct super_block *pts_sb_from_inode(struct inode *inode)
8082
{
83+
#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
8184
if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
8285
return inode->i_sb;
83-
86+
#endif
8487
return devpts_mnt->mnt_sb;
8588
}
8689

87-
static int parse_mount_options(char *data, struct pts_mount_opts *opts)
90+
#define PARSE_MOUNT 0
91+
#define PARSE_REMOUNT 1
92+
93+
static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
8894
{
8995
char *p;
9096

@@ -95,6 +101,10 @@ static int parse_mount_options(char *data, struct pts_mount_opts *opts)
95101
opts->mode = DEVPTS_DEFAULT_MODE;
96102
opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
97103

104+
/* newinstance makes sense only on initial mount */
105+
if (op == PARSE_MOUNT)
106+
opts->newinstance = 0;
107+
98108
while ((p = strsep(&data, ",")) != NULL) {
99109
substring_t args[MAX_OPT_ARGS];
100110
int token;
@@ -128,6 +138,11 @@ static int parse_mount_options(char *data, struct pts_mount_opts *opts)
128138
return -EINVAL;
129139
opts->ptmxmode = option & S_IALLUGO;
130140
break;
141+
case Opt_newinstance:
142+
/* newinstance makes sense only on initial mount */
143+
if (op == PARSE_MOUNT)
144+
opts->newinstance = 1;
145+
break;
131146
#endif
132147
default:
133148
printk(KERN_ERR "devpts: called with bogus options\n");
@@ -214,7 +229,7 @@ static int devpts_remount(struct super_block *sb, int *flags, char *data)
214229
struct pts_fs_info *fsi = DEVPTS_SB(sb);
215230
struct pts_mount_opts *opts = &fsi->mount_opts;
216231

217-
err = parse_mount_options(data, opts);
232+
err = parse_mount_options(data, PARSE_REMOUNT, opts);
218233

219234
/*
220235
* parse_mount_options() restores options to default values
@@ -309,8 +324,100 @@ static int compare_init_pts_sb(struct super_block *s, void *p)
309324
{
310325
if (devpts_mnt)
311326
return devpts_mnt->mnt_sb == s;
327+
return 0;
328+
}
329+
330+
#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
331+
/*
332+
* Safely parse the mount options in @data and update @opts.
333+
*
334+
* devpts ends up parsing options two times during mount, due to the
335+
* two modes of operation it supports. The first parse occurs in
336+
* devpts_get_sb() when determining the mode (single-instance or
337+
* multi-instance mode). The second parse happens in devpts_remount()
338+
* or new_pts_mount() depending on the mode.
339+
*
340+
* Parsing of options modifies the @data making subsequent parsing
341+
* incorrect. So make a local copy of @data and parse it.
342+
*
343+
* Return: 0 On success, -errno on error
344+
*/
345+
static int safe_parse_mount_options(void *data, struct pts_mount_opts *opts)
346+
{
347+
int rc;
348+
void *datacp;
349+
350+
if (!data)
351+
return 0;
352+
353+
/* Use kstrdup() ? */
354+
datacp = kmalloc(PAGE_SIZE, GFP_KERNEL);
355+
if (!datacp)
356+
return -ENOMEM;
357+
358+
memcpy(datacp, data, PAGE_SIZE);
359+
rc = parse_mount_options((char *)datacp, PARSE_MOUNT, opts);
360+
kfree(datacp);
361+
362+
return rc;
363+
}
364+
365+
/*
366+
* Mount a new (private) instance of devpts. PTYs created in this
367+
* instance are independent of the PTYs in other devpts instances.
368+
*/
369+
static int new_pts_mount(struct file_system_type *fs_type, int flags,
370+
void *data, struct vfsmount *mnt)
371+
{
372+
int err;
373+
struct pts_fs_info *fsi;
374+
struct pts_mount_opts *opts;
375+
376+
printk(KERN_NOTICE "devpts: newinstance mount\n");
377+
378+
err = get_sb_nodev(fs_type, flags, data, devpts_fill_super, mnt);
379+
if (err)
380+
return err;
381+
382+
fsi = DEVPTS_SB(mnt->mnt_sb);
383+
opts = &fsi->mount_opts;
384+
385+
err = parse_mount_options(data, PARSE_MOUNT, opts);
386+
if (err)
387+
goto fail;
388+
389+
err = mknod_ptmx(mnt->mnt_sb);
390+
if (err)
391+
goto fail;
312392

313393
return 0;
394+
395+
fail:
396+
dput(mnt->mnt_sb->s_root);
397+
deactivate_super(mnt->mnt_sb);
398+
return err;
399+
}
400+
401+
/*
402+
* Check if 'newinstance' mount option was specified in @data.
403+
*
404+
* Return: -errno on error (eg: invalid mount options specified)
405+
* : 1 if 'newinstance' mount option was specified
406+
* : 0 if 'newinstance' mount option was NOT specified
407+
*/
408+
static int is_new_instance_mount(void *data)
409+
{
410+
int rc;
411+
struct pts_mount_opts opts;
412+
413+
if (!data)
414+
return 0;
415+
416+
rc = safe_parse_mount_options(data, &opts);
417+
if (!rc)
418+
rc = opts.newinstance;
419+
420+
return rc;
314421
}
315422

316423
/*
@@ -358,11 +465,54 @@ static int get_init_pts_sb(struct file_system_type *fs_type, int flags,
358465
return simple_set_mnt(mnt, s);
359466
}
360467

468+
/*
469+
* Mount or remount the initial kernel mount of devpts. This type of
470+
* mount maintains the legacy, single-instance semantics, while the
471+
* kernel still allows multiple-instances.
472+
*/
473+
static int init_pts_mount(struct file_system_type *fs_type, int flags,
474+
void *data, struct vfsmount *mnt)
475+
{
476+
int err;
477+
478+
err = get_init_pts_sb(fs_type, flags, data, mnt);
479+
if (err)
480+
return err;
481+
482+
err = mknod_ptmx(mnt->mnt_sb);
483+
if (err) {
484+
dput(mnt->mnt_sb->s_root);
485+
deactivate_super(mnt->mnt_sb);
486+
}
487+
488+
return err;
489+
}
490+
361491
static int devpts_get_sb(struct file_system_type *fs_type,
362492
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
363493
{
364-
return get_init_pts_sb(fs_type, flags, data, mnt);
494+
int new;
495+
496+
new = is_new_instance_mount(data);
497+
if (new < 0)
498+
return new;
499+
500+
if (new)
501+
return new_pts_mount(fs_type, flags, data, mnt);
502+
503+
return init_pts_mount(fs_type, flags, data, mnt);
365504
}
505+
#else
506+
/*
507+
* This supports only the legacy single-instance semantics (no
508+
* multiple-instance semantics)
509+
*/
510+
static int devpts_get_sb(struct file_system_type *fs_type, int flags,
511+
const char *dev_name, void *data, struct vfsmount *mnt)
512+
{
513+
return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
514+
}
515+
#endif
366516

367517
static void devpts_kill_sb(struct super_block *sb)
368518
{
@@ -488,12 +638,18 @@ void devpts_pty_kill(struct tty_struct *tty)
488638
mutex_lock(&root->d_inode->i_mutex);
489639

490640
dentry = d_find_alias(inode);
491-
if (dentry && !IS_ERR(dentry)) {
641+
if (IS_ERR(dentry))
642+
goto out;
643+
644+
if (dentry) {
492645
inode->i_nlink--;
493646
d_delete(dentry);
494-
dput(dentry);
647+
dput(dentry); // d_alloc_name() in devpts_pty_new()
495648
}
496649

650+
dput(dentry); // d_find_alias above
651+
652+
out:
497653
mutex_unlock(&root->d_inode->i_mutex);
498654
}
499655

0 commit comments

Comments
 (0)