Import 2.1.118
[davej-history.git] / fs / devpts / inode.c
blob78d3ae6250c7d238694d2d224abdb77e9cf646a0
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/fs.h>
16 #include <linux/init.h>
17 #include <linux/kdev_t.h>
18 #include <linux/kernel.h>
19 #include <linux/locks.h>
20 #include <linux/major.h>
21 #include <linux/malloc.h>
22 #include <linux/stat.h>
23 #include <linux/tty.h>
24 #include <asm/bitops.h>
25 #include <asm/uaccess.h>
27 #include "devpts_i.h"
29 static struct super_block *mounts = NULL;
31 static void devpts_put_super(struct super_block *sb)
33 struct devpts_sb_info *sbi = SBI(sb);
34 struct inode *inode;
35 int i;
37 for ( i = 0 ; i < sbi->max_ptys ; i++ ) {
38 if ( (inode = sbi->inodes[i]) ) {
39 if ( inode->i_count != 1 )
40 printk("devpts_put_super: badness: entry %d count %d\n",
41 i, inode->i_count);
42 inode->i_nlink--;
43 iput(inode);
47 *sbi->back = sbi->next;
48 if ( sbi->next )
49 SBI(sbi->next)->back = sbi->back;
51 kfree(sbi->inodes);
52 kfree(sbi);
54 #ifdef MODULE
55 MOD_DEC_USE_COUNT;
56 #endif
59 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
60 static void devpts_read_inode(struct inode *inode);
61 static void devpts_write_inode(struct inode *inode);
63 static struct super_operations devpts_sops = {
64 devpts_read_inode,
65 devpts_write_inode,
66 NULL, /* put_inode */
67 NULL, /* delete_inode */
68 NULL, /* notify_change */
69 devpts_put_super,
70 NULL, /* write_super */
71 devpts_statfs,
72 NULL, /* remount_fs */
73 NULL, /* clear_inode */
76 static int devpts_parse_options(char *options, struct devpts_sb_info *sbi)
78 int setuid = 0;
79 int setgid = 0;
80 uid_t uid = 0;
81 gid_t gid = 0;
82 umode_t mode = 0600;
83 char *this_char, *value;
85 this_char = NULL;
86 if ( options )
87 this_char = strtok(options,",");
88 for ( ; this_char; this_char = strtok(NULL,",")) {
89 if ((value = strchr(this_char,'=')) != NULL)
90 *value++ = 0;
91 if (!strcmp(this_char,"uid")) {
92 if (!value || !*value)
93 return 1;
94 uid = simple_strtoul(value,&value,0);
95 if (*value)
96 return 1;
97 setuid = 1;
99 else if (!strcmp(this_char,"gid")) {
100 if (!value || !*value)
101 return 1;
102 gid = simple_strtoul(value,&value,0);
103 if (*value)
104 return 1;
105 setgid = 1;
107 else if (!strcmp(this_char,"mode")) {
108 if (!value || !*value)
109 return 1;
110 mode = simple_strtoul(value,&value,8);
111 if (*value)
112 return 1;
114 else
115 return 1;
117 sbi->setuid = setuid;
118 sbi->setgid = setgid;
119 sbi->uid = uid;
120 sbi->gid = gid;
121 sbi->mode = mode & ~S_IFMT;
123 return 0;
126 struct super_block *devpts_read_super(struct super_block *s, void *data,
127 int silent)
129 struct inode * root_inode;
130 struct dentry * root;
131 struct devpts_sb_info *sbi;
133 MOD_INC_USE_COUNT;
135 lock_super(s);
136 /* Super block already completed? */
137 if (s->s_root)
138 goto out_unlock;
140 sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
141 if ( !sbi )
142 goto fail_unlock;
144 sbi->magic = DEVPTS_SBI_MAGIC;
145 sbi->max_ptys = unix98_max_ptys;
146 sbi->inodes = kmalloc(sizeof(struct inode *) * sbi->max_ptys, GFP_KERNEL);
147 if ( !sbi->inodes ) {
148 kfree(sbi);
149 goto fail_unlock;
151 memset(sbi->inodes, 0, sizeof(struct inode *) * sbi->max_ptys);
153 s->u.generic_sbp = (void *) sbi;
154 s->s_blocksize = 1024;
155 s->s_blocksize_bits = 10;
156 s->s_magic = DEVPTS_SUPER_MAGIC;
157 s->s_op = &devpts_sops;
158 s->s_root = NULL;
159 unlock_super(s); /* shouldn't we keep it locked a while longer? */
162 * Get the root inode and dentry, but defer checking for errors.
164 root_inode = iget(s, 1); /* inode 1 == root directory */
165 root = d_alloc_root(root_inode, NULL);
168 * Check whether somebody else completed the super block.
170 if (s->s_root)
171 goto out_dput;
173 if (!root)
174 goto fail_iput;
176 /* Can this call block? (It shouldn't) */
177 if ( devpts_parse_options(data,sbi) ) {
178 printk("devpts: called with bogus options\n");
179 goto fail_dput;
183 * Check whether somebody else completed the super block.
185 if (s->s_root)
186 goto out_dec;
189 * Success! Install the root dentry now to indicate completion.
191 lock_super(s);
192 s->s_root = root;
194 sbi->next = mounts;
195 if ( sbi->next )
196 SBI(sbi->next)->back = &(sbi->next);
197 sbi->back = &mounts;
198 mounts = s;
200 unlock_super(s);
201 return s;
204 * Success ... somebody else completed the super block for us.
206 out_unlock:
207 unlock_super(s);
208 goto out_dec;
209 out_dput:
210 if (root)
211 dput(root);
212 else
213 iput(root_inode);
214 out_dec:
215 MOD_DEC_USE_COUNT;
216 return s;
219 * Failure ... clear the s_dev slot and clean up.
221 fail_dput:
223 * dput() can block, so we clear the super block first.
225 s->s_dev = 0;
226 dput(root);
227 goto fail_free;
228 fail_iput:
229 printk("devpts: get root dentry failed\n");
231 * iput() can block, so we clear the super block first.
233 s->s_dev = 0;
234 iput(root_inode);
235 fail_free:
236 kfree(sbi);
237 goto fail_dec;
238 fail_unlock:
239 unlock_super(s);
240 fail_dec:
241 s->s_dev = 0;
242 MOD_DEC_USE_COUNT;
243 return NULL;
246 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
248 struct statfs tmp;
250 tmp.f_type = DEVPTS_SUPER_MAGIC;
251 tmp.f_bsize = 1024;
252 tmp.f_blocks = 0;
253 tmp.f_bfree = 0;
254 tmp.f_bavail = 0;
255 tmp.f_files = 0;
256 tmp.f_ffree = 0;
257 tmp.f_namelen = NAME_MAX;
258 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
261 static void devpts_read_inode(struct inode *inode)
263 ino_t ino = inode->i_ino;
264 struct devpts_sb_info *sbi = SBI(inode->i_sb);
266 inode->i_op = NULL;
267 inode->i_mode = 0;
268 inode->i_nlink = 0;
269 inode->i_size = 0;
270 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
271 inode->i_blocks = 0;
272 inode->i_blksize = 1024;
273 inode->i_uid = inode->i_gid = 0;
275 if ( ino == 1 ) {
276 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
277 inode->i_op = &devpts_root_inode_operations;
278 inode->i_nlink = 2;
279 return;
282 ino -= 2;
283 if ( ino >= sbi->max_ptys )
284 return; /* Bogus */
286 inode->i_mode = S_IFCHR;
287 inode->i_rdev = MKDEV(0,0); /* Gets filled in by devpts_pty_new() */
289 inode->i_op = &chrdev_inode_operations;
291 return;
294 static void devpts_write_inode(struct inode *inode)
298 static struct file_system_type devpts_fs_type = {
299 "devpts",
301 devpts_read_super,
302 NULL
305 void devpts_pty_new(int number, kdev_t device)
307 struct super_block *sb;
308 struct devpts_sb_info *sbi;
309 struct inode *inode;
311 for ( sb = mounts ; sb ; sb = sbi->next ) {
312 sbi = SBI(sb);
314 if ( sbi->inodes[number] ) {
315 continue; /* Already registered, this does happen */
318 /* Yes, this looks backwards, but it is correct */
319 inode = iget(sb, number+2);
320 if ( inode ) {
321 inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
322 inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
323 inode->i_mode = sbi->mode | S_IFCHR;
324 inode->i_rdev = device;
325 inode->i_nlink++;
326 sbi->inodes[number] = inode;
331 void devpts_pty_kill(int number)
333 struct super_block *sb;
334 struct devpts_sb_info *sbi;
335 struct inode *inode;
337 for ( sb = mounts ; sb ; sb = sbi->next ) {
338 sbi = SBI(sb);
340 inode = sbi->inodes[number];
342 if ( inode ) {
343 sbi->inodes[number] = NULL;
344 inode->i_nlink--;
345 iput(inode);
350 __initfunc(int init_devpts_fs(void))
352 return register_filesystem(&devpts_fs_type);
356 #ifdef MODULE
358 int init_module(void)
360 int err = init_devpts_fs();
361 if ( !err ) {
362 devpts_upcall_new = devpts_pty_new;
363 devpts_upcall_kill = devpts_pty_kill;
365 return err;
368 void cleanup_module(void)
370 devpts_upcall_new = NULL;
371 devpts_upcall_kill = NULL;
372 unregister_filesystem(&devpts_fs_type);
375 #endif