1 /* $Id: capifs.c,v 1.1.4.1 2001/11/20 14:19:34 kai Exp $
3 * Copyright 2000 by Carsten Paeth <calle@calle.de>
5 * Heavily based on devpts filesystem from H. Peter Anvin
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/version.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>
31 MODULE_DESCRIPTION("CAPI4Linux: /dev/capi/ filesystem");
32 MODULE_AUTHOR("Carsten Paeth");
33 MODULE_LICENSE("GPL");
35 static char *revision = "$Revision: 1.1.4.1 $";
45 struct capifs_sb_info {
47 struct super_block *next;
48 struct super_block **back;
55 unsigned int max_ncci;
56 struct capifs_ncci *nccis;
59 #define CAPIFS_SUPER_MAGIC (('C'<<8)|'N')
60 #define CAPIFS_SBI_MAGIC (('C'<<24)|('A'<<16)|('P'<<8)|'I')
62 static inline struct capifs_sb_info *SBI(struct super_block *sb)
64 return (struct capifs_sb_info *)(sb->u.generic_sbp);
67 /* ------------------------------------------------------------------ */
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);
74 static struct file_operations capifs_root_operations = {
75 read: generic_read_dir,
76 readdir: capifs_root_readdir,
79 struct inode_operations capifs_root_inode_operations = {
80 lookup: capifs_root_lookup,
83 static struct dentry_operations capifs_dentry_operations = {
84 d_revalidate: capifs_revalidate,
92 static int capifs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
94 struct inode * inode = filp->f_dentry->d_inode;
95 struct capifs_sb_info * sbi = SBI(filp->f_dentry->d_inode->i_sb);
104 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
109 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
114 while (nr < sbi->max_ncci) {
116 struct capifs_ncci *np = &sbi->nccis[n];
117 if (np->inode && np->used) {
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 )
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?)
137 static int capifs_revalidate(struct dentry * dentry, int flags)
139 struct capifs_sb_info *sbi;
141 if ( !dentry->d_inode )
144 sbi = SBI(dentry->d_inode->i_sb);
146 return ( sbi->nccis[dentry->d_inode->i_ino - 2].inode == dentry->d_inode );
149 static struct dentry *capifs_root_lookup(struct inode * dir, struct dentry * dentry)
151 struct capifs_sb_info *sbi = SBI(dir->i_sb);
152 struct capifs_ncci *np;
159 dentry->d_inode = NULL; /* Assume failure */
160 dentry->d_op = &capifs_dentry_operations;
162 if (dentry->d_name.len >= sizeof(numbuf) )
164 strncpy(numbuf, dentry->d_name.name, dentry->d_name.len);
165 numbuf[dentry->d_name.len] = 0;
167 if (!isdigit(*p)) type = *p++;
169 num = (unsigned int)simple_strtoul(p, &tmp, 10);
170 if (tmp == p || *tmp)
173 for (i = 0, np = sbi->nccis ; i < sbi->max_ncci; i++, np++) {
174 if (np->used && np->num == num && np->type == type)
178 if ( i >= sbi->max_ncci )
181 dentry->d_inode = np->inode;
182 if ( dentry->d_inode )
183 atomic_inc(&dentry->d_inode->i_count);
185 d_add(dentry, dentry->d_inode);
190 /* ------------------------------------------------------------------ */
192 static struct super_block *mounts = NULL;
194 static void capifs_put_super(struct super_block *sb)
196 struct capifs_sb_info *sbi = SBI(sb);
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));
210 *sbi->back = sbi->next;
212 SBI(sbi->next)->back = sbi->back;
218 static int capifs_statfs(struct super_block *sb, struct statfs *buf);
220 static struct super_operations capifs_sops = {
221 put_super: capifs_put_super,
222 statfs: capifs_statfs,
225 static int capifs_parse_options(char *options, struct capifs_sb_info *sbi)
232 unsigned int maxncci = 512;
233 char *this_char, *value;
237 this_char = strtok(options,",");
238 for ( ; this_char; this_char = strtok(NULL,",")) {
239 if ((value = strchr(this_char,'=')) != NULL)
241 if (!strcmp(this_char,"uid")) {
242 if (!value || !*value)
244 uid = simple_strtoul(value,&value,0);
249 else if (!strcmp(this_char,"gid")) {
250 if (!value || !*value)
252 gid = simple_strtoul(value,&value,0);
257 else if (!strcmp(this_char,"mode")) {
258 if (!value || !*value)
260 mode = simple_strtoul(value,&value,8);
264 else if (!strcmp(this_char,"maxncci")) {
265 if (!value || !*value)
267 maxncci = simple_strtoul(value,&value,8);
274 sbi->setuid = setuid;
275 sbi->setgid = setgid;
278 sbi->mode = mode & ~S_IFMT;
279 sbi->max_ncci = maxncci;
284 struct super_block *capifs_read_super(struct super_block *s, void *data,
287 struct inode * root_inode;
288 struct dentry * root;
289 struct capifs_sb_info *sbi;
291 /* Super block already completed? */
295 sbi = (struct capifs_sb_info *) kmalloc(sizeof(struct capifs_sb_info), GFP_KERNEL);
299 memset(sbi, 0, sizeof(struct capifs_sb_info));
300 sbi->magic = CAPIFS_SBI_MAGIC;
302 if ( capifs_parse_options(data,sbi) ) {
304 printk("capifs: called with bogus options\n");
308 sbi->nccis = kmalloc(sizeof(struct capifs_ncci) * sbi->max_ncci, GFP_KERNEL);
313 memset(sbi->nccis, 0, sizeof(struct capifs_ncci) * sbi->max_ncci);
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;
323 * Get the root inode and dentry, but defer checking for errors.
325 root_inode = capifs_new_inode(s);
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;
333 root = d_alloc_root(root_inode);
336 * Check whether somebody else completed the super block.
339 if (root) dput(root);
340 else iput(root_inode);
345 printk("capifs: get root dentry failed\n");
347 * iput() can block, so we clear the super block first.
356 * Check whether somebody else completed the super block.
362 * Success! Install the root dentry now to indicate completion.
368 SBI(sbi->next)->back = &(sbi->next);
372 out: /* Success ... somebody else completed the super block for us. */
378 static int capifs_statfs(struct super_block *sb, struct statfs *buf)
380 buf->f_type = CAPIFS_SUPER_MAGIC;
387 buf->f_namelen = NAME_MAX;
391 static struct inode *capifs_new_inode(struct super_block *sb)
393 struct inode *inode = new_inode(sb);
395 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
397 inode->i_blksize = 1024;
398 inode->i_uid = inode->i_gid = 0;
403 static DECLARE_FSTYPE(capifs_fs_type, "capifs", capifs_read_super, 0);
405 void capifs_new_ncci(char type, unsigned int num, kdev_t device)
407 struct super_block *sb;
408 struct capifs_sb_info *sbi;
409 struct capifs_ncci *np;
412 for ( sb = mounts ; sb ; sb = sbi->next ) {
415 for (ino = 0, np = sbi->nccis ; ino < sbi->max_ncci; ino++, np++) {
424 if ( ino >= sbi->max_ncci )
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;
432 inode->i_ino = ino + 2;
433 init_special_inode(inode, sbi->mode|S_IFCHR, np->kdev);
438 void capifs_free_ncci(char type, unsigned int num)
440 struct super_block *sb;
441 struct capifs_sb_info *sbi;
443 struct capifs_ncci *np;
446 for ( sb = mounts ; sb ; sb = sbi->next ) {
449 for (ino = 0, np = sbi->nccis ; ino < sbi->max_ncci; ino++, np++) {
450 if (!np->used || np->type != type || np->num != num)
464 static int __init capifs_init(void)
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)
480 err = register_filesystem(&capifs_fs_type);
485 printk(KERN_NOTICE "capifs: Rev %s\n", rev);
490 static void __exit capifs_exit(void)
492 unregister_filesystem(&capifs_fs_type);
495 EXPORT_SYMBOL(capifs_new_ncci);
496 EXPORT_SYMBOL(capifs_free_ncci);
498 module_init(capifs_init);
499 module_exit(capifs_exit);