Import 2.3.5
[davej-history.git] / fs / devpts / inode.c
blobf5f8469d8270091a0a4ea41894cd6d05796bca61
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 super_block *mounts = NULL;
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 ( inode->i_count != 1 )
41 printk("devpts_put_super: badness: entry %d count %d\n",
42 i, inode->i_count);
43 inode->i_nlink--;
44 iput(inode);
48 *sbi->back = sbi->next;
49 if ( sbi->next )
50 SBI(sbi->next)->back = sbi->back;
52 kfree(sbi->inodes);
53 kfree(sbi);
55 #ifdef MODULE
56 MOD_DEC_USE_COUNT;
57 #endif
60 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
61 static void devpts_read_inode(struct inode *inode);
62 static void devpts_write_inode(struct inode *inode);
64 static struct super_operations devpts_sops = {
65 devpts_read_inode,
66 devpts_write_inode,
67 NULL, /* put_inode */
68 NULL, /* delete_inode */
69 NULL, /* notify_change */
70 devpts_put_super,
71 NULL, /* write_super */
72 devpts_statfs,
73 NULL, /* remount_fs */
74 NULL, /* clear_inode */
77 static int devpts_parse_options(char *options, struct devpts_sb_info *sbi)
79 int setuid = 0;
80 int setgid = 0;
81 uid_t uid = 0;
82 gid_t gid = 0;
83 umode_t mode = 0600;
84 char *this_char, *value;
86 this_char = NULL;
87 if ( options )
88 this_char = strtok(options,",");
89 for ( ; this_char; this_char = strtok(NULL,",")) {
90 if ((value = strchr(this_char,'=')) != NULL)
91 *value++ = 0;
92 if (!strcmp(this_char,"uid")) {
93 if (!value || !*value)
94 return 1;
95 uid = simple_strtoul(value,&value,0);
96 if (*value)
97 return 1;
98 setuid = 1;
100 else if (!strcmp(this_char,"gid")) {
101 if (!value || !*value)
102 return 1;
103 gid = simple_strtoul(value,&value,0);
104 if (*value)
105 return 1;
106 setgid = 1;
108 else if (!strcmp(this_char,"mode")) {
109 if (!value || !*value)
110 return 1;
111 mode = simple_strtoul(value,&value,8);
112 if (*value)
113 return 1;
115 else
116 return 1;
118 sbi->setuid = setuid;
119 sbi->setgid = setgid;
120 sbi->uid = uid;
121 sbi->gid = gid;
122 sbi->mode = mode & ~S_IFMT;
124 return 0;
127 struct super_block *devpts_read_super(struct super_block *s, void *data,
128 int silent)
130 struct inode * root_inode;
131 struct dentry * root;
132 struct devpts_sb_info *sbi;
134 MOD_INC_USE_COUNT;
136 lock_super(s);
137 /* Super block already completed? */
138 if (s->s_root)
139 goto out_unlock;
141 sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
142 if ( !sbi )
143 goto fail_unlock;
145 sbi->magic = DEVPTS_SBI_MAGIC;
146 sbi->max_ptys = unix98_max_ptys;
147 sbi->inodes = kmalloc(sizeof(struct inode *) * sbi->max_ptys, GFP_KERNEL);
148 if ( !sbi->inodes ) {
149 kfree(sbi);
150 goto fail_unlock;
152 memset(sbi->inodes, 0, sizeof(struct inode *) * sbi->max_ptys);
154 s->u.generic_sbp = (void *) sbi;
155 s->s_blocksize = 1024;
156 s->s_blocksize_bits = 10;
157 s->s_magic = DEVPTS_SUPER_MAGIC;
158 s->s_op = &devpts_sops;
159 s->s_root = NULL;
160 unlock_super(s); /* shouldn't we keep it locked a while longer? */
163 * Get the root inode and dentry, but defer checking for errors.
165 root_inode = iget(s, 1); /* inode 1 == root directory */
166 root = d_alloc_root(root_inode, NULL);
169 * Check whether somebody else completed the super block.
171 if (s->s_root)
172 goto out_dput;
174 if (!root)
175 goto fail_iput;
177 /* Can this call block? (It shouldn't) */
178 if ( devpts_parse_options(data,sbi) ) {
179 printk("devpts: called with bogus options\n");
180 goto fail_dput;
184 * Check whether somebody else completed the super block.
186 if (s->s_root)
187 goto out_dec;
190 * Success! Install the root dentry now to indicate completion.
192 lock_super(s);
193 s->s_root = root;
195 sbi->next = mounts;
196 if ( sbi->next )
197 SBI(sbi->next)->back = &(sbi->next);
198 sbi->back = &mounts;
199 mounts = s;
201 unlock_super(s);
202 return s;
205 * Success ... somebody else completed the super block for us.
207 out_unlock:
208 unlock_super(s);
209 goto out_dec;
210 out_dput:
211 if (root)
212 dput(root);
213 else
214 iput(root_inode);
215 out_dec:
216 MOD_DEC_USE_COUNT;
217 return s;
220 * Failure ... clear the s_dev slot and clean up.
222 fail_dput:
224 * dput() can block, so we clear the super block first.
226 s->s_dev = 0;
227 dput(root);
228 goto fail_free;
229 fail_iput:
230 printk("devpts: get root dentry failed\n");
232 * iput() can block, so we clear the super block first.
234 s->s_dev = 0;
235 iput(root_inode);
236 fail_free:
237 kfree(sbi);
238 goto fail_dec;
239 fail_unlock:
240 unlock_super(s);
241 fail_dec:
242 s->s_dev = 0;
243 MOD_DEC_USE_COUNT;
244 return NULL;
247 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
249 struct statfs tmp;
251 tmp.f_type = DEVPTS_SUPER_MAGIC;
252 tmp.f_bsize = 1024;
253 tmp.f_blocks = 0;
254 tmp.f_bfree = 0;
255 tmp.f_bavail = 0;
256 tmp.f_files = 0;
257 tmp.f_ffree = 0;
258 tmp.f_namelen = NAME_MAX;
259 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
262 static void devpts_read_inode(struct inode *inode)
264 ino_t ino = inode->i_ino;
265 struct devpts_sb_info *sbi = SBI(inode->i_sb);
267 inode->i_op = NULL;
268 inode->i_mode = 0;
269 inode->i_nlink = 0;
270 inode->i_size = 0;
271 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
272 inode->i_blocks = 0;
273 inode->i_blksize = 1024;
274 inode->i_uid = inode->i_gid = 0;
276 if ( ino == 1 ) {
277 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
278 inode->i_op = &devpts_root_inode_operations;
279 inode->i_nlink = 2;
280 return;
283 ino -= 2;
284 if ( ino >= sbi->max_ptys )
285 return; /* Bogus */
287 inode->i_mode = S_IFCHR;
288 inode->i_rdev = MKDEV(0,0); /* Gets filled in by devpts_pty_new() */
290 inode->i_op = &chrdev_inode_operations;
292 return;
295 static void devpts_write_inode(struct inode *inode)
299 static struct file_system_type devpts_fs_type = {
300 "devpts",
302 devpts_read_super,
303 NULL
306 void devpts_pty_new(int number, kdev_t device)
308 struct super_block *sb;
309 struct devpts_sb_info *sbi;
310 struct inode *inode;
312 for ( sb = mounts ; sb ; sb = sbi->next ) {
313 sbi = SBI(sb);
315 if ( sbi->inodes[number] ) {
316 continue; /* Already registered, this does happen */
319 /* Yes, this looks backwards, but it is correct */
320 inode = iget(sb, number+2);
321 if ( inode ) {
322 inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
323 inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
324 inode->i_mode = sbi->mode | S_IFCHR;
325 inode->i_rdev = device;
326 inode->i_nlink++;
327 sbi->inodes[number] = inode;
332 void devpts_pty_kill(int number)
334 struct super_block *sb;
335 struct devpts_sb_info *sbi;
336 struct inode *inode;
338 for ( sb = mounts ; sb ; sb = sbi->next ) {
339 sbi = SBI(sb);
341 inode = sbi->inodes[number];
343 if ( inode ) {
344 sbi->inodes[number] = NULL;
345 inode->i_nlink--;
346 iput(inode);
351 __initfunc(int init_devpts_fs(void))
353 return register_filesystem(&devpts_fs_type);
357 #ifdef MODULE
359 int init_module(void)
361 int err = init_devpts_fs();
362 if ( !err ) {
363 devpts_upcall_new = devpts_pty_new;
364 devpts_upcall_kill = devpts_pty_kill;
366 return err;
369 void cleanup_module(void)
371 devpts_upcall_new = NULL;
372 devpts_upcall_kill = NULL;
373 unregister_filesystem(&devpts_fs_type);
376 #endif