1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/devpts/inode.c
5 * Copyright 1998 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>
15 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/kdev_t.h>
19 #include <linux/kernel.h>
20 #include <linux/locks.h>
21 #include <linux/major.h>
22 #include <linux/slab.h>
23 #include <linux/stat.h>
24 #include <linux/tty.h>
25 #include <asm/bitops.h>
26 #include <asm/uaccess.h>
30 static struct vfsmount *devpts_mnt;
32 static void devpts_put_super(struct super_block *sb)
34 struct devpts_sb_info *sbi = SBI(sb);
38 for ( i = 0 ; i < sbi->max_ptys ; i++ ) {
39 if ( (inode = sbi->inodes[i]) ) {
40 if ( atomic_read(&inode->i_count) != 1 )
41 printk("devpts_put_super: badness: entry %d count %d\n",
42 i, atomic_read(&inode->i_count));
51 static int devpts_statfs(struct super_block *sb, struct statfs *buf);
52 static int devpts_remount (struct super_block * sb, int * flags, char * data);
54 static struct super_operations devpts_sops = {
55 put_super: devpts_put_super,
56 statfs: devpts_statfs,
57 remount_fs: devpts_remount,
60 static int devpts_parse_options(char *options, struct devpts_sb_info *sbi)
67 char *this_char, *value;
71 this_char = strtok(options,",");
72 for ( ; this_char; this_char = strtok(NULL,",")) {
73 if ((value = strchr(this_char,'=')) != NULL)
75 if (!strcmp(this_char,"uid")) {
76 if (!value || !*value)
78 uid = simple_strtoul(value,&value,0);
83 else if (!strcmp(this_char,"gid")) {
84 if (!value || !*value)
86 gid = simple_strtoul(value,&value,0);
91 else if (!strcmp(this_char,"mode")) {
92 if (!value || !*value)
94 mode = simple_strtoul(value,&value,8);
101 sbi->setuid = setuid;
102 sbi->setgid = setgid;
105 sbi->mode = mode & ~S_IFMT;
110 static int devpts_remount(struct super_block * sb, int * flags, char * data)
112 struct devpts_sb_info *sbi = sb->u.generic_sbp;
113 int res = devpts_parse_options(data,sbi);
115 printk("devpts: called with bogus options\n");
121 struct super_block *devpts_read_super(struct super_block *s, void *data,
124 struct inode * inode;
125 struct devpts_sb_info *sbi;
127 sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
131 sbi->magic = DEVPTS_SBI_MAGIC;
132 sbi->max_ptys = unix98_max_ptys;
133 sbi->inodes = kmalloc(sizeof(struct inode *) * sbi->max_ptys, GFP_KERNEL);
136 memset(sbi->inodes, 0, sizeof(struct inode *) * sbi->max_ptys);
138 if ( devpts_parse_options(data,sbi) && !silent) {
139 printk("devpts: called with bogus options\n");
143 inode = new_inode(s);
147 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
149 inode->i_blksize = 1024;
150 inode->i_uid = inode->i_gid = 0;
151 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
152 inode->i_op = &devpts_root_inode_operations;
153 inode->i_fop = &devpts_root_operations;
156 s->u.generic_sbp = (void *) sbi;
157 s->s_blocksize = 1024;
158 s->s_blocksize_bits = 10;
159 s->s_magic = DEVPTS_SUPER_MAGIC;
160 s->s_op = &devpts_sops;
161 s->s_root = d_alloc_root(inode);
165 printk("devpts: get root dentry failed\n");
175 static int devpts_statfs(struct super_block *sb, struct statfs *buf)
177 buf->f_type = DEVPTS_SUPER_MAGIC;
179 buf->f_namelen = NAME_MAX;
183 static DECLARE_FSTYPE(devpts_fs_type, "devpts", devpts_read_super, FS_SINGLE);
185 void devpts_pty_new(int number, kdev_t device)
187 struct super_block *sb = devpts_mnt->mnt_sb;
188 struct devpts_sb_info *sbi = SBI(sb);
191 if ( sbi->inodes[number] )
192 return; /* Already registered, this does happen */
194 inode = new_inode(sb);
197 inode->i_ino = number+2;
199 inode->i_blksize = 1024;
200 inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
201 inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
202 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
203 init_special_inode(inode, S_IFCHR|sbi->mode, kdev_t_to_nr(device));
205 if ( sbi->inodes[number] ) {
209 sbi->inodes[number] = inode;
212 void devpts_pty_kill(int number)
214 struct super_block *sb = devpts_mnt->mnt_sb;
215 struct devpts_sb_info *sbi = SBI(sb);
216 struct inode *inode = sbi->inodes[number];
219 sbi->inodes[number] = NULL;
225 static int __init init_devpts_fs(void)
227 int err = register_filesystem(&devpts_fs_type);
229 devpts_mnt = kern_mount(&devpts_fs_type);
230 err = PTR_ERR(devpts_mnt);
231 if (!IS_ERR(devpts_mnt))
235 devpts_upcall_new = devpts_pty_new;
236 devpts_upcall_kill = devpts_pty_kill;
243 static void __exit exit_devpts_fs(void)
246 devpts_upcall_new = NULL;
247 devpts_upcall_kill = NULL;
249 unregister_filesystem(&devpts_fs_type);
250 kern_umount(devpts_mnt);
253 module_init(init_devpts_fs)
254 module_exit(exit_devpts_fs)
255 MODULE_LICENSE("GPL");