Import 2.1.112pre1
[davej-history.git] / fs / devpts / inode.c
blob093140e9d3817159aa15444bc1858eb151191cf2
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 < NR_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);
53 #ifdef MODULE
54 MOD_DEC_USE_COUNT;
55 #endif
58 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
59 static void devpts_read_inode(struct inode *inode);
60 static void devpts_write_inode(struct inode *inode);
62 static struct super_operations devpts_sops = {
63 devpts_read_inode,
64 devpts_write_inode,
65 NULL, /* put_inode */
66 NULL, /* delete_inode */
67 NULL, /* notify_change */
68 devpts_put_super,
69 NULL, /* write_super */
70 devpts_statfs,
71 NULL, /* remount_fs */
72 NULL, /* clear_inode */
75 static int devpts_parse_options(char *options, struct devpts_sb_info *sbi)
77 int setuid = 0;
78 int setgid = 0;
79 uid_t uid = 0; /* To shut up gcc */
80 gid_t gid = 0;
81 umode_t mode = 0600;
82 char *this_char, *value;
84 if ( !options ) return 1;
85 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
86 if ((value = strchr(this_char,'=')) != NULL)
87 *value++ = 0;
88 if (!strcmp(this_char,"uid")) {
89 if (!value || !*value)
90 return 1;
91 uid = simple_strtoul(value,&value,0);
92 if (*value)
93 return 1;
94 setuid = 1;
96 else if (!strcmp(this_char,"gid")) {
97 if (!value || !*value)
98 return 1;
99 gid = simple_strtoul(value,&value,0);
100 if (*value)
101 return 1;
102 setgid = 1;
104 else if (!strcmp(this_char,"mode")) {
105 if (!value || !*value)
106 return 1;
107 mode = simple_strtoul(value,&value,8);
108 if (*value)
109 return 1;
111 else
112 return 1;
114 sbi->setuid = setuid;
115 sbi->setgid = setgid;
116 sbi->uid = uid;
117 sbi->gid = gid;
118 sbi->mode = mode & ~S_IFMT;
120 return 0;
123 struct super_block *devpts_read_super(struct super_block *s, void *data,
124 int silent)
126 struct inode * root_inode;
127 struct dentry * root;
128 struct devpts_sb_info *sbi;
130 MOD_INC_USE_COUNT;
132 lock_super(s);
133 /* Super block already completed? */
134 if (s->s_root)
135 goto out_unlock;
137 sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
138 if ( !sbi )
139 goto fail_unlock;
141 sbi->magic = DEVPTS_SBI_MAGIC;
142 memset(sbi->inodes, 0, sizeof sbi->inodes);
144 s->u.generic_sbp = (void *) sbi;
145 s->s_blocksize = 1024;
146 s->s_blocksize_bits = 10;
147 s->s_magic = DEVPTS_SUPER_MAGIC;
148 s->s_op = &devpts_sops;
149 s->s_root = NULL;
150 unlock_super(s); /* shouldn't we keep it locked a while longer? */
153 * Get the root inode and dentry, but defer checking for errors.
155 root_inode = iget(s, 1); /* inode 1 == root directory */
156 root = d_alloc_root(root_inode, NULL);
159 * Check whether somebody else completed the super block.
161 if (s->s_root)
162 goto out_dput;
164 if (!root)
165 goto fail_iput;
167 /* Can this call block? (It shouldn't) */
168 if ( devpts_parse_options(data,sbi) ) {
169 printk("devpts: called with bogus options\n");
170 goto fail_dput;
174 * Check whether somebody else completed the super block.
176 if (s->s_root)
177 goto out_dec;
180 * Success! Install the root dentry now to indicate completion.
182 lock_super(s);
183 s->s_root = root;
185 sbi->next = mounts;
186 if ( sbi->next )
187 SBI(sbi->next)->back = &(sbi->next);
188 sbi->back = &mounts;
189 mounts = s;
191 unlock_super(s);
192 return s;
195 * Success ... somebody else completed the super block for us.
197 out_unlock:
198 unlock_super(s);
199 goto out_dec;
200 out_dput:
201 if (root)
202 dput(root);
203 else
204 iput(root_inode);
205 out_dec:
206 MOD_DEC_USE_COUNT;
207 return s;
210 * Failure ... clear the s_dev slot and clean up.
212 fail_dput:
214 * dput() can block, so we clear the super block first.
216 s->s_dev = 0;
217 dput(root);
218 goto fail_free;
219 fail_iput:
220 printk("devpts: get root dentry failed\n");
222 * iput() can block, so we clear the super block first.
224 s->s_dev = 0;
225 iput(root_inode);
226 fail_free:
227 kfree(sbi);
228 goto fail_dec;
229 fail_unlock:
230 unlock_super(s);
231 fail_dec:
232 s->s_dev = 0;
233 MOD_DEC_USE_COUNT;
234 return NULL;
237 static int devpts_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
239 struct statfs tmp;
241 tmp.f_type = DEVPTS_SUPER_MAGIC;
242 tmp.f_bsize = 1024;
243 tmp.f_blocks = 0;
244 tmp.f_bfree = 0;
245 tmp.f_bavail = 0;
246 tmp.f_files = 0;
247 tmp.f_ffree = 0;
248 tmp.f_namelen = NAME_MAX;
249 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
252 static void devpts_read_inode(struct inode *inode)
254 ino_t ino = inode->i_ino;
256 inode->i_op = NULL;
257 inode->i_mode = 0;
258 inode->i_nlink = 0;
259 inode->i_size = 0;
260 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
261 inode->i_blocks = 0;
262 inode->i_blksize = 1024;
263 inode->i_uid = inode->i_gid = 0;
265 if ( ino == 1 ) {
266 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
267 inode->i_op = &devpts_root_inode_operations;
268 inode->i_nlink = 2;
269 return;
272 ino -= 2;
273 if ( ino >= NR_PTYS )
274 return; /* Bogus */
276 inode->i_mode = S_IFCHR;
277 inode->i_rdev = MKDEV(0,0); /* Gets filled in by devpts_pty_new() */
279 inode->i_op = &chrdev_inode_operations;
281 return;
284 static void devpts_write_inode(struct inode *inode)
288 static struct file_system_type devpts_fs_type = {
289 "devpts",
291 devpts_read_super,
292 NULL
295 void devpts_pty_new(int number, kdev_t device)
297 struct super_block *sb;
298 struct devpts_sb_info *sbi;
299 struct inode *inode;
301 for ( sb = mounts ; sb ; sb = sbi->next ) {
302 sbi = SBI(sb);
304 if ( sbi->inodes[number] ) {
305 continue; /* Already registered, this does happen */
308 /* Yes, this looks backwards, but it is correct */
309 inode = iget(sb, number+2);
310 if ( inode ) {
311 inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
312 inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
313 inode->i_mode = sbi->mode | S_IFCHR;
314 inode->i_rdev = device;
315 inode->i_nlink++;
316 sbi->inodes[number] = inode;
321 void devpts_pty_kill(int number)
323 struct super_block *sb;
324 struct devpts_sb_info *sbi;
325 struct inode *inode;
327 for ( sb = mounts ; sb ; sb = sbi->next ) {
328 sbi = SBI(sb);
330 inode = sbi->inodes[number];
332 if ( inode ) {
333 sbi->inodes[number] = NULL;
334 inode->i_nlink--;
335 iput(inode);
340 __initfunc(int init_devpts_fs(void))
342 return register_filesystem(&devpts_fs_type);
346 #ifdef MODULE
348 int init_module(void)
350 int err = init_devpts_fs();
351 if ( !err ) {
352 devpts_upcall_new = devpts_pty_new;
353 devpts_upcall_kill = devpts_pty_kill;
355 return err;
358 void cleanup_module(void)
360 devpts_upcall_new = NULL;
361 devpts_upcall_kill = NULL;
362 unregister_filesystem(&devpts_fs_type);
365 #endif