1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/devpts/inode.c
5 * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
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.
11 * ------------------------------------------------------------------------- */
13 #include <linux/module.h>
14 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/namei.h>
18 #include <linux/mount.h>
19 #include <linux/tty.h>
20 #include <linux/devpts_fs.h>
21 #include <linux/parser.h>
23 #define DEVPTS_SUPER_MAGIC 0x1cd1
25 static struct vfsmount
*devpts_mnt
;
26 static struct dentry
*devpts_root
;
34 } config
= {.mode
= 0600};
37 Opt_uid
, Opt_gid
, Opt_mode
,
41 static match_table_t tokens
= {
44 {Opt_mode
, "mode=%o"},
48 static int devpts_remount(struct super_block
*sb
, int *flags
, char *data
)
58 while ((p
= strsep(&data
, ",")) != NULL
) {
59 substring_t args
[MAX_OPT_ARGS
];
66 token
= match_token(p
, tokens
, args
);
69 if (match_int(&args
[0], &option
))
75 if (match_int(&args
[0], &option
))
81 if (match_octal(&args
[0], &option
))
83 config
.mode
= option
& ~S_IFMT
;
86 printk(KERN_ERR
"devpts: called with bogus options\n");
94 static struct super_operations devpts_sops
= {
95 .statfs
= simple_statfs
,
96 .remount_fs
= devpts_remount
,
100 devpts_fill_super(struct super_block
*s
, void *data
, int silent
)
102 struct inode
* inode
;
104 s
->s_blocksize
= 1024;
105 s
->s_blocksize_bits
= 10;
106 s
->s_magic
= DEVPTS_SUPER_MAGIC
;
107 s
->s_op
= &devpts_sops
;
110 inode
= new_inode(s
);
114 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
116 inode
->i_blksize
= 1024;
117 inode
->i_uid
= inode
->i_gid
= 0;
118 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
;
119 inode
->i_op
= &simple_dir_inode_operations
;
120 inode
->i_fop
= &simple_dir_operations
;
123 devpts_root
= s
->s_root
= d_alloc_root(inode
);
127 printk("devpts: get root dentry failed\n");
133 static struct super_block
*devpts_get_sb(struct file_system_type
*fs_type
,
134 int flags
, const char *dev_name
, void *data
)
136 return get_sb_single(fs_type
, flags
, data
, devpts_fill_super
);
139 static struct file_system_type devpts_fs_type
= {
140 .owner
= THIS_MODULE
,
142 .get_sb
= devpts_get_sb
,
143 .kill_sb
= kill_anon_super
,
147 * The normal naming convention is simply /dev/pts/<number>; this conforms
148 * to the System V naming convention
151 static struct dentry
*get_node(int num
)
154 struct dentry
*root
= devpts_root
;
155 mutex_lock(&root
->d_inode
->i_mutex
);
156 return lookup_one_len(s
, root
, sprintf(s
, "%d", num
));
159 int devpts_pty_new(struct tty_struct
*tty
)
161 int number
= tty
->index
;
162 struct tty_driver
*driver
= tty
->driver
;
163 dev_t device
= MKDEV(driver
->major
, driver
->minor_start
+number
);
164 struct dentry
*dentry
;
165 struct inode
*inode
= new_inode(devpts_mnt
->mnt_sb
);
167 /* We're supposed to be given the slave end of a pty */
168 BUG_ON(driver
->type
!= TTY_DRIVER_TYPE_PTY
);
169 BUG_ON(driver
->subtype
!= PTY_TYPE_SLAVE
);
174 inode
->i_ino
= number
+2;
175 inode
->i_blksize
= 1024;
176 inode
->i_uid
= config
.setuid
? config
.uid
: current
->fsuid
;
177 inode
->i_gid
= config
.setgid
? config
.gid
: current
->fsgid
;
178 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
179 init_special_inode(inode
, S_IFCHR
|config
.mode
, device
);
180 inode
->u
.generic_ip
= tty
;
182 dentry
= get_node(number
);
183 if (!IS_ERR(dentry
) && !dentry
->d_inode
)
184 d_instantiate(dentry
, inode
);
186 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
191 struct tty_struct
*devpts_get_tty(int number
)
193 struct dentry
*dentry
= get_node(number
);
194 struct tty_struct
*tty
;
197 if (!IS_ERR(dentry
)) {
199 tty
= dentry
->d_inode
->u
.generic_ip
;
203 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
208 void devpts_pty_kill(int number
)
210 struct dentry
*dentry
= get_node(number
);
212 if (!IS_ERR(dentry
)) {
213 struct inode
*inode
= dentry
->d_inode
;
221 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
224 static int __init
init_devpts_fs(void)
226 int err
= register_filesystem(&devpts_fs_type
);
228 devpts_mnt
= kern_mount(&devpts_fs_type
);
229 if (IS_ERR(devpts_mnt
))
230 err
= PTR_ERR(devpts_mnt
);
235 static void __exit
exit_devpts_fs(void)
237 unregister_filesystem(&devpts_fs_type
);
241 module_init(init_devpts_fs
)
242 module_exit(exit_devpts_fs
)
243 MODULE_LICENSE("GPL");