import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / drivers / isdn / avmb1 / capifs.c
1 /* $Id: capifs.c,v 1.1.4.1 2001/11/20 14:19:34 kai Exp $
2  * 
3  * Copyright 2000 by Carsten Paeth <calle@calle.de>
4  *
5  * Heavily based on devpts filesystem from H. Peter Anvin
6  * 
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/version.h>
13 #include <linux/fs.h>
14 #include <linux/tty.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/param.h>
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/kdev_t.h>
23 #include <linux/kernel.h>
24 #include <linux/locks.h>
25 #include <linux/major.h>
26 #include <linux/slab.h>
27 #include <linux/ctype.h>
28 #include <asm/bitops.h>
29 #include <asm/uaccess.h>
30
31 MODULE_DESCRIPTION("CAPI4Linux: /dev/capi/ filesystem");
32 MODULE_AUTHOR("Carsten Paeth");
33 MODULE_LICENSE("GPL");
34
35 static char *revision = "$Revision: 1.1.4.1 $";
36
37 struct capifs_ncci {
38         struct inode *inode;
39         char used;
40         char type;
41         unsigned int num;
42         kdev_t kdev;
43 };
44
45 struct capifs_sb_info {
46         u32 magic;
47         struct super_block *next;
48         struct super_block **back;
49         int setuid;
50         int setgid;
51         uid_t   uid;
52         gid_t   gid;
53         umode_t mode;
54
55         unsigned int max_ncci;
56         struct capifs_ncci *nccis;
57 };
58
59 #define CAPIFS_SUPER_MAGIC (('C'<<8)|'N')
60 #define CAPIFS_SBI_MAGIC   (('C'<<24)|('A'<<16)|('P'<<8)|'I')
61
62 static inline struct capifs_sb_info *SBI(struct super_block *sb)
63 {
64         return (struct capifs_sb_info *)(sb->u.generic_sbp);
65 }
66
67 /* ------------------------------------------------------------------ */
68
69 static int capifs_root_readdir(struct file *,void *,filldir_t);
70 static struct dentry *capifs_root_lookup(struct inode *,struct dentry *);
71 static int capifs_revalidate(struct dentry *, int);
72 static struct inode *capifs_new_inode(struct super_block *sb);
73
74 static struct file_operations capifs_root_operations = {
75         read:           generic_read_dir,
76         readdir:        capifs_root_readdir,
77 };
78
79 struct inode_operations capifs_root_inode_operations = {
80         lookup: capifs_root_lookup,
81 };
82
83 static struct dentry_operations capifs_dentry_operations = {
84         d_revalidate: capifs_revalidate,
85 };
86
87 /*
88  * /dev/capi/%d
89  * /dev/capi/r%d
90  */
91
92 static int capifs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
93 {
94         struct inode * inode = filp->f_dentry->d_inode;
95         struct capifs_sb_info * sbi = SBI(filp->f_dentry->d_inode->i_sb);
96         off_t nr;
97         char numbuf[32];
98
99         nr = filp->f_pos;
100
101         switch(nr)
102         {
103         case 0:
104                 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
105                         return 0;
106                 filp->f_pos = ++nr;
107                 /* fall through */
108         case 1:
109                 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
110                         return 0;
111                 filp->f_pos = ++nr;
112                 /* fall through */
113         default:
114                 while (nr < sbi->max_ncci) {
115                         int n = nr - 2;
116                         struct capifs_ncci *np = &sbi->nccis[n];
117                         if (np->inode && np->used) {
118                                 char *p = numbuf;
119                                 if (np->type) *p++ = np->type;
120                                 sprintf(p, "%u", np->num);
121                                 if ( filldir(dirent, numbuf, strlen(numbuf), nr, nr, DT_UNKNOWN) < 0 )
122                                         return 0;
123                         }
124                         filp->f_pos = ++nr;
125                 }
126                 break;
127         }
128
129         return 0;
130 }
131
132 /*
133  * Revalidate is called on every cache lookup.  We use it to check that
134  * the ncci really does still exist.  Never revalidate negative dentries;
135  * for simplicity (fix later?)
136  */
137 static int capifs_revalidate(struct dentry * dentry, int flags)
138 {
139         struct capifs_sb_info *sbi;
140
141         if ( !dentry->d_inode )
142                 return 0;
143
144         sbi = SBI(dentry->d_inode->i_sb);
145
146         return ( sbi->nccis[dentry->d_inode->i_ino - 2].inode == dentry->d_inode );
147 }
148
149 static struct dentry *capifs_root_lookup(struct inode * dir, struct dentry * dentry)
150 {
151         struct capifs_sb_info *sbi = SBI(dir->i_sb);
152         struct capifs_ncci *np;
153         unsigned int i;
154         char numbuf[32];
155         char *p, *tmp;
156         unsigned int num;
157         char type = 0;
158
159         dentry->d_inode = NULL; /* Assume failure */
160         dentry->d_op    = &capifs_dentry_operations;
161
162         if (dentry->d_name.len >= sizeof(numbuf) )
163                 return NULL;
164         strncpy(numbuf, dentry->d_name.name, dentry->d_name.len);
165         numbuf[dentry->d_name.len] = 0;
166         p = numbuf;
167         if (!isdigit(*p)) type = *p++;
168         tmp = p;
169         num = (unsigned int)simple_strtoul(p, &tmp, 10);
170         if (tmp == p || *tmp)
171                 return NULL;
172
173         for (i = 0, np = sbi->nccis ; i < sbi->max_ncci; i++, np++) {
174                 if (np->used && np->num == num && np->type == type)
175                         break;
176         }
177
178         if ( i >= sbi->max_ncci )
179                 return NULL;
180
181         dentry->d_inode = np->inode;
182         if ( dentry->d_inode )
183                 atomic_inc(&dentry->d_inode->i_count);
184         
185         d_add(dentry, dentry->d_inode);
186
187         return NULL;
188 }
189
190 /* ------------------------------------------------------------------ */
191
192 static struct super_block *mounts = NULL;
193
194 static void capifs_put_super(struct super_block *sb)
195 {
196         struct capifs_sb_info *sbi = SBI(sb);
197         struct inode *inode;
198         int i;
199
200         for ( i = 0 ; i < sbi->max_ncci ; i++ ) {
201                 if ( (inode = sbi->nccis[i].inode) ) {
202                         if (atomic_read(&inode->i_count) != 1 )
203                                 printk("capifs_put_super: badness: entry %d count %d\n",
204                                        i, (unsigned)atomic_read(&inode->i_count));
205                         inode->i_nlink--;
206                         iput(inode);
207                 }
208         }
209
210         *sbi->back = sbi->next;
211         if ( sbi->next )
212                 SBI(sbi->next)->back = sbi->back;
213
214         kfree(sbi->nccis);
215         kfree(sbi);
216 }
217
218 static int capifs_statfs(struct super_block *sb, struct statfs *buf);
219
220 static struct super_operations capifs_sops = {
221         put_super:      capifs_put_super,
222         statfs:         capifs_statfs,
223 };
224
225 static int capifs_parse_options(char *options, struct capifs_sb_info *sbi)
226 {
227         int setuid = 0;
228         int setgid = 0;
229         uid_t uid = 0;
230         gid_t gid = 0;
231         umode_t mode = 0600;
232         unsigned int maxncci = 512;
233         char *this_char, *value;
234
235         this_char = NULL;
236         if ( options )
237                 this_char = strtok(options,",");
238         for ( ; this_char; this_char = strtok(NULL,",")) {
239                 if ((value = strchr(this_char,'=')) != NULL)
240                         *value++ = 0;
241                 if (!strcmp(this_char,"uid")) {
242                         if (!value || !*value)
243                                 return 1;
244                         uid = simple_strtoul(value,&value,0);
245                         if (*value)
246                                 return 1;
247                         setuid = 1;
248                 }
249                 else if (!strcmp(this_char,"gid")) {
250                         if (!value || !*value)
251                                 return 1;
252                         gid = simple_strtoul(value,&value,0);
253                         if (*value)
254                                 return 1;
255                         setgid = 1;
256                 }
257                 else if (!strcmp(this_char,"mode")) {
258                         if (!value || !*value)
259                                 return 1;
260                         mode = simple_strtoul(value,&value,8);
261                         if (*value)
262                                 return 1;
263                 }
264                 else if (!strcmp(this_char,"maxncci")) {
265                         if (!value || !*value)
266                                 return 1;
267                         maxncci = simple_strtoul(value,&value,8);
268                         if (*value)
269                                 return 1;
270                 }
271                 else
272                         return 1;
273         }
274         sbi->setuid   = setuid;
275         sbi->setgid   = setgid;
276         sbi->uid      = uid;
277         sbi->gid      = gid;
278         sbi->mode     = mode & ~S_IFMT;
279         sbi->max_ncci = maxncci;
280
281         return 0;
282 }
283
284 struct super_block *capifs_read_super(struct super_block *s, void *data,
285                                       int silent)
286 {
287         struct inode * root_inode;
288         struct dentry * root;
289         struct capifs_sb_info *sbi;
290
291         /* Super block already completed? */
292         if (s->s_root)
293                 goto out;
294
295         sbi = (struct capifs_sb_info *) kmalloc(sizeof(struct capifs_sb_info), GFP_KERNEL);
296         if ( !sbi )
297                 goto fail;
298
299         memset(sbi, 0, sizeof(struct capifs_sb_info));
300         sbi->magic  = CAPIFS_SBI_MAGIC;
301
302         if ( capifs_parse_options(data,sbi) ) {
303                 kfree(sbi);
304                 printk("capifs: called with bogus options\n");
305                 goto fail;
306         }
307
308         sbi->nccis = kmalloc(sizeof(struct capifs_ncci) * sbi->max_ncci, GFP_KERNEL);
309         if ( !sbi->nccis ) {
310                 kfree(sbi);
311                 goto fail;
312         }
313         memset(sbi->nccis, 0, sizeof(struct capifs_ncci) * sbi->max_ncci);
314
315         s->u.generic_sbp = (void *) sbi;
316         s->s_blocksize = 1024;
317         s->s_blocksize_bits = 10;
318         s->s_magic = CAPIFS_SUPER_MAGIC;
319         s->s_op = &capifs_sops;
320         s->s_root = NULL;
321
322         /*
323          * Get the root inode and dentry, but defer checking for errors.
324          */
325         root_inode = capifs_new_inode(s);
326         if (root_inode) {
327                 root_inode->i_ino = 1;
328                 root_inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
329                 root_inode->i_op = &capifs_root_inode_operations;
330                 root_inode->i_fop = &capifs_root_operations;
331                 root_inode->i_nlink = 2;
332         } 
333         root = d_alloc_root(root_inode);
334
335         /*
336          * Check whether somebody else completed the super block.
337          */
338         if (s->s_root) {
339                 if (root) dput(root);
340                 else iput(root_inode);
341                 goto out;
342         }
343
344         if (!root) {
345                 printk("capifs: get root dentry failed\n");
346                 /*
347                 * iput() can block, so we clear the super block first.
348                 */
349                 iput(root_inode);
350                 kfree(sbi->nccis);
351                 kfree(sbi);
352                 goto fail;
353         }
354
355         /*
356          * Check whether somebody else completed the super block.
357          */
358         if (s->s_root)
359                 goto out;
360         
361         /*
362          * Success! Install the root dentry now to indicate completion.
363          */
364         s->s_root = root;
365
366         sbi->next = mounts;
367         if ( sbi->next )
368                 SBI(sbi->next)->back = &(sbi->next);
369         sbi->back = &mounts;
370         mounts = s;
371
372 out:    /* Success ... somebody else completed the super block for us. */ 
373         return s;
374 fail:
375         return NULL;
376 }
377
378 static int capifs_statfs(struct super_block *sb, struct statfs *buf)
379 {
380         buf->f_type = CAPIFS_SUPER_MAGIC;
381         buf->f_bsize = 1024;
382         buf->f_blocks = 0;
383         buf->f_bfree = 0;
384         buf->f_bavail = 0;
385         buf->f_files = 0;
386         buf->f_ffree = 0;
387         buf->f_namelen = NAME_MAX;
388         return 0;
389 }
390
391 static struct inode *capifs_new_inode(struct super_block *sb)
392 {
393         struct inode *inode = new_inode(sb);
394         if (inode) {
395                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
396                 inode->i_blocks = 0;
397                 inode->i_blksize = 1024;
398                 inode->i_uid = inode->i_gid = 0;
399         }
400         return inode;
401 }
402
403 static DECLARE_FSTYPE(capifs_fs_type, "capifs", capifs_read_super, 0);
404
405 void capifs_new_ncci(char type, unsigned int num, kdev_t device)
406 {
407         struct super_block *sb;
408         struct capifs_sb_info *sbi;
409         struct capifs_ncci *np;
410         ino_t ino;
411
412         for ( sb = mounts ; sb ; sb = sbi->next ) {
413                 sbi = SBI(sb);
414
415                 for (ino = 0, np = sbi->nccis ; ino < sbi->max_ncci; ino++, np++) {
416                         if (np->used == 0) {
417                                 np->used = 1;
418                                 np->type = type;
419                                 np->num = num;
420                                 np->kdev = device;
421                                 break;
422                         }
423                 }
424                 if ( ino >= sbi->max_ncci )
425                         continue;
426
427                 if ((np->inode = capifs_new_inode(sb)) != NULL) {
428                         struct inode *inode = np->inode;
429                         inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
430                         inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
431                         inode->i_nlink = 1;
432                         inode->i_ino = ino + 2;
433                         init_special_inode(inode, sbi->mode|S_IFCHR, np->kdev);
434                 }
435         }
436 }
437
438 void capifs_free_ncci(char type, unsigned int num)
439 {
440         struct super_block *sb;
441         struct capifs_sb_info *sbi;
442         struct inode *inode;
443         struct capifs_ncci *np;
444         ino_t ino;
445
446         for ( sb = mounts ; sb ; sb = sbi->next ) {
447                 sbi = SBI(sb);
448
449                 for (ino = 0, np = sbi->nccis ; ino < sbi->max_ncci; ino++, np++) {
450                         if (!np->used || np->type != type || np->num != num)
451                                 continue;
452                         if (np->inode) {
453                                 inode = np->inode;
454                                 np->inode = 0;
455                                 np->used = 0;
456                                 inode->i_nlink--;
457                                 iput(inode);
458                                 break;
459                         }
460                 }
461         }
462 }
463
464 static int __init capifs_init(void)
465 {
466         char rev[32];
467         char *p;
468         int err;
469
470         MOD_INC_USE_COUNT;
471
472         if ((p = strchr(revision, ':')) != 0 && p[1]) {
473                 strncpy(rev, p + 2, sizeof(rev));
474                 rev[sizeof(rev)-1] = 0;
475                 if ((p = strchr(rev, '$')) != 0 && p > rev)
476                    *(p-1) = 0;
477         } else
478                 strcpy(rev, "1.0");
479
480         err = register_filesystem(&capifs_fs_type);
481         if (err) {
482                 MOD_DEC_USE_COUNT;
483                 return err;
484         }
485         printk(KERN_NOTICE "capifs: Rev %s\n", rev);
486         MOD_DEC_USE_COUNT;
487         return 0;
488 }
489
490 static void __exit capifs_exit(void)
491 {
492         unregister_filesystem(&capifs_fs_type);
493 }
494
495 EXPORT_SYMBOL(capifs_new_ncci);
496 EXPORT_SYMBOL(capifs_free_ncci);
497
498 module_init(capifs_init);
499 module_exit(capifs_exit);