Import 2.4.0-test2pre7
[davej-history.git] / fs / devpts / inode.c
blobc3667b2086e22e283a11fd62df72a28c561198b7
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>
16 #include <linux/fs.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/malloc.h>
23 #include <linux/stat.h>
24 #include <linux/tty.h>
25 #include <asm/bitops.h>
26 #include <asm/uaccess.h>
28 #include "devpts_i.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);
35 struct inode *inode;
36 int i;
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));
43 inode->i_nlink--;
44 iput(inode);
47 kfree(sbi->inodes);
48 kfree(sbi);
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)
62 int setuid = 0;
63 int setgid = 0;
64 uid_t uid = 0;
65 gid_t gid = 0;
66 umode_t mode = 0600;
67 char *this_char, *value;
69 this_char = NULL;
70 if ( options )
71 this_char = strtok(options,",");
72 for ( ; this_char; this_char = strtok(NULL,",")) {
73 if ((value = strchr(this_char,'=')) != NULL)
74 *value++ = 0;
75 if (!strcmp(this_char,"uid")) {
76 if (!value || !*value)
77 return 1;
78 uid = simple_strtoul(value,&value,0);
79 if (*value)
80 return 1;
81 setuid = 1;
83 else if (!strcmp(this_char,"gid")) {
84 if (!value || !*value)
85 return 1;
86 gid = simple_strtoul(value,&value,0);
87 if (*value)
88 return 1;
89 setgid = 1;
91 else if (!strcmp(this_char,"mode")) {
92 if (!value || !*value)
93 return 1;
94 mode = simple_strtoul(value,&value,8);
95 if (*value)
96 return 1;
98 else
99 return 1;
101 sbi->setuid = setuid;
102 sbi->setgid = setgid;
103 sbi->uid = uid;
104 sbi->gid = gid;
105 sbi->mode = mode & ~S_IFMT;
107 return 0;
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);
114 if (res) {
115 printk("devpts: called with bogus options\n");
116 return -EINVAL;
118 return 0;
121 struct super_block *devpts_read_super(struct super_block *s, void *data,
122 int silent)
124 struct inode * inode;
125 struct devpts_sb_info *sbi;
127 sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
128 if ( !sbi )
129 goto fail;
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);
134 if ( !sbi->inodes )
135 goto fail_free;
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");
140 goto fail_free;
143 inode = get_empty_inode();
144 if (!inode)
145 goto fail_free;
146 inode->i_sb = s;
147 inode->i_dev = s->s_dev;
148 inode->i_ino = 1;
149 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
150 inode->i_size = 0;
151 inode->i_blocks = 0;
152 inode->i_blksize = 1024;
153 inode->i_uid = inode->i_gid = 0;
154 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
155 inode->i_op = &devpts_root_inode_operations;
156 inode->i_fop = &devpts_root_operations;
157 inode->i_nlink = 2;
159 s->u.generic_sbp = (void *) sbi;
160 s->s_blocksize = 1024;
161 s->s_blocksize_bits = 10;
162 s->s_magic = DEVPTS_SUPER_MAGIC;
163 s->s_op = &devpts_sops;
164 s->s_root = d_alloc_root(inode);
165 if (s->s_root)
166 return s;
168 printk("devpts: get root dentry failed\n");
169 iput(inode);
170 fail_free:
171 kfree(sbi);
172 fail:
173 return NULL;
176 static int devpts_statfs(struct super_block *sb, struct statfs *buf)
178 buf->f_type = DEVPTS_SUPER_MAGIC;
179 buf->f_bsize = 1024;
180 buf->f_namelen = NAME_MAX;
181 return 0;
184 static DECLARE_FSTYPE(devpts_fs_type, "devpts", devpts_read_super, FS_SINGLE);
186 void devpts_pty_new(int number, kdev_t device)
188 struct super_block *sb = devpts_mnt->mnt_sb;
189 struct devpts_sb_info *sbi = SBI(sb);
190 struct inode *inode;
192 if ( sbi->inodes[number] )
193 return; /* Already registered, this does happen */
195 inode = get_empty_inode();
196 if (!inode)
197 return;
198 inode->i_sb = sb;
199 inode->i_dev = sb->s_dev;
200 inode->i_ino = number+2;
201 inode->i_blocks = 0;
202 inode->i_blksize = 1024;
203 inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
204 inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
205 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
206 init_special_inode(inode, S_IFCHR|sbi->mode, kdev_t_to_nr(device));
208 if ( sbi->inodes[number] ) {
209 iput(inode);
210 return;
212 sbi->inodes[number] = inode;
215 void devpts_pty_kill(int number)
217 struct super_block *sb = devpts_mnt->mnt_sb;
218 struct devpts_sb_info *sbi = SBI(sb);
219 struct inode *inode = sbi->inodes[number];
221 if ( inode ) {
222 sbi->inodes[number] = NULL;
223 inode->i_nlink--;
224 iput(inode);
228 int __init init_devpts_fs(void)
230 int err = register_filesystem(&devpts_fs_type);
231 if (!err) {
232 devpts_mnt = kern_mount(&devpts_fs_type);
233 err = PTR_ERR(devpts_mnt);
234 if (!IS_ERR(devpts_mnt))
235 err = 0;
237 return err;
240 #ifdef MODULE
242 int init_module(void)
244 int err = init_devpts_fs();
245 if ( !err ) {
246 devpts_upcall_new = devpts_pty_new;
247 devpts_upcall_kill = devpts_pty_kill;
249 return err;
252 void cleanup_module(void)
254 devpts_upcall_new = NULL;
255 devpts_upcall_kill = NULL;
256 unregister_filesystem(&devpts_fs_type);
257 kern_umount(devpts_mnt);
260 #endif