| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: capifs.c,v 1.1.2.3 2004/01/16 21:09:26 keil 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/fs.h> | 
 | 13 | #include <linux/mount.h> | 
 | 14 | #include <linux/namei.h> | 
 | 15 | #include <linux/module.h> | 
 | 16 | #include <linux/init.h> | 
 | 17 | #include <linux/ctype.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 18 | #include <linux/sched.h>	/* current */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  | 
| Adrian Bunk | 6e21bd9 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 20 | #include "capifs.h" | 
 | 21 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | MODULE_DESCRIPTION("CAPI4Linux: /dev/capi/ filesystem"); | 
 | 23 | MODULE_AUTHOR("Carsten Paeth"); | 
 | 24 | MODULE_LICENSE("GPL"); | 
 | 25 |  | 
 | 26 | /* ------------------------------------------------------------------ */ | 
 | 27 |  | 
 | 28 | static char *revision = "$Revision: 1.1.2.3 $"; | 
 | 29 |  | 
 | 30 | /* ------------------------------------------------------------------ */ | 
 | 31 |  | 
 | 32 | #define CAPIFS_SUPER_MAGIC (('C'<<8)|'N') | 
 | 33 |  | 
 | 34 | static struct vfsmount *capifs_mnt; | 
 | 35 | static struct dentry *capifs_root; | 
 | 36 |  | 
 | 37 | static struct { | 
 | 38 | 	int setuid; | 
 | 39 | 	int setgid; | 
 | 40 | 	uid_t   uid; | 
 | 41 | 	gid_t   gid; | 
 | 42 | 	umode_t mode; | 
 | 43 | } config = {.mode = 0600}; | 
 | 44 |  | 
 | 45 | /* ------------------------------------------------------------------ */ | 
 | 46 |  | 
 | 47 | static int capifs_remount(struct super_block *s, int *flags, char *data) | 
 | 48 | { | 
 | 49 | 	int setuid = 0; | 
 | 50 | 	int setgid = 0; | 
 | 51 | 	uid_t uid = 0; | 
 | 52 | 	gid_t gid = 0; | 
 | 53 | 	umode_t mode = 0600; | 
 | 54 | 	char *this_char; | 
 | 55 |  | 
 | 56 | 	this_char = NULL; | 
 | 57 | 	while ((this_char = strsep(&data, ",")) != NULL) { | 
 | 58 | 		int n; | 
 | 59 | 		char dummy; | 
 | 60 | 		if (!*this_char) | 
 | 61 | 			continue; | 
 | 62 | 		if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) { | 
 | 63 | 			setuid = 1; | 
 | 64 | 			uid = n; | 
 | 65 | 		} else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) { | 
 | 66 | 			setgid = 1; | 
 | 67 | 			gid = n; | 
 | 68 | 		} else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1) | 
 | 69 | 			mode = n & ~S_IFMT; | 
 | 70 | 		else { | 
 | 71 | 			printk("capifs: called with bogus options\n"); | 
 | 72 | 			return -EINVAL; | 
 | 73 | 		} | 
 | 74 | 	} | 
 | 75 | 	config.setuid  = setuid; | 
 | 76 | 	config.setgid  = setgid; | 
 | 77 | 	config.uid     = uid; | 
 | 78 | 	config.gid     = gid; | 
 | 79 | 	config.mode    = mode; | 
 | 80 | 	return 0; | 
 | 81 | } | 
 | 82 |  | 
 | 83 | static struct super_operations capifs_sops = | 
 | 84 | { | 
 | 85 | 	.statfs		= simple_statfs, | 
 | 86 | 	.remount_fs	= capifs_remount, | 
 | 87 | }; | 
 | 88 |  | 
 | 89 |  | 
 | 90 | static int | 
 | 91 | capifs_fill_super(struct super_block *s, void *data, int silent) | 
 | 92 | { | 
 | 93 | 	struct inode * inode; | 
 | 94 |  | 
 | 95 | 	s->s_blocksize = 1024; | 
 | 96 | 	s->s_blocksize_bits = 10; | 
 | 97 | 	s->s_magic = CAPIFS_SUPER_MAGIC; | 
 | 98 | 	s->s_op = &capifs_sops; | 
 | 99 | 	s->s_time_gran = 1; | 
 | 100 |  | 
 | 101 | 	inode = new_inode(s); | 
 | 102 | 	if (!inode) | 
 | 103 | 		goto fail; | 
 | 104 | 	inode->i_ino = 1; | 
 | 105 | 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | 
 | 106 | 	inode->i_blocks = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | 	inode->i_uid = inode->i_gid = 0; | 
 | 108 | 	inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; | 
 | 109 | 	inode->i_op = &simple_dir_inode_operations; | 
 | 110 | 	inode->i_fop = &simple_dir_operations; | 
 | 111 | 	inode->i_nlink = 2; | 
 | 112 |  | 
 | 113 | 	capifs_root = s->s_root = d_alloc_root(inode); | 
 | 114 | 	if (s->s_root) | 
 | 115 | 		return 0; | 
 | 116 | 	 | 
 | 117 | 	printk("capifs: get root dentry failed\n"); | 
 | 118 | 	iput(inode); | 
 | 119 | fail: | 
 | 120 | 	return -ENOMEM; | 
 | 121 | } | 
 | 122 |  | 
| David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 123 | static int capifs_get_sb(struct file_system_type *fs_type, | 
 | 124 | 	int flags, const char *dev_name, void *data, struct vfsmount *mnt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { | 
| David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 126 | 	return get_sb_single(fs_type, flags, data, capifs_fill_super, mnt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } | 
 | 128 |  | 
 | 129 | static struct file_system_type capifs_fs_type = { | 
 | 130 | 	.owner		= THIS_MODULE, | 
 | 131 | 	.name		= "capifs", | 
 | 132 | 	.get_sb		= capifs_get_sb, | 
 | 133 | 	.kill_sb	= kill_anon_super, | 
 | 134 | }; | 
 | 135 |  | 
 | 136 | static struct dentry *get_node(int num) | 
 | 137 | { | 
 | 138 | 	char s[10]; | 
 | 139 | 	struct dentry *root = capifs_root; | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 140 | 	mutex_lock(&root->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | 	return lookup_one_len(s, root, sprintf(s, "%d", num)); | 
 | 142 | } | 
 | 143 |  | 
 | 144 | void capifs_new_ncci(unsigned int number, dev_t device) | 
 | 145 | { | 
 | 146 | 	struct dentry *dentry; | 
 | 147 | 	struct inode *inode = new_inode(capifs_mnt->mnt_sb); | 
 | 148 | 	if (!inode) | 
 | 149 | 		return; | 
 | 150 | 	inode->i_ino = number+2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 	inode->i_uid = config.setuid ? config.uid : current->fsuid; | 
 | 152 | 	inode->i_gid = config.setgid ? config.gid : current->fsgid; | 
 | 153 | 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | 
 | 154 | 	init_special_inode(inode, S_IFCHR|config.mode, device); | 
 | 155 | 	//inode->i_op = &capifs_file_inode_operations; | 
 | 156 |  | 
 | 157 | 	dentry = get_node(number); | 
 | 158 | 	if (!IS_ERR(dentry) && !dentry->d_inode) | 
 | 159 | 		d_instantiate(dentry, inode); | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 160 | 	mutex_unlock(&capifs_root->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } | 
 | 162 |  | 
 | 163 | void capifs_free_ncci(unsigned int number) | 
 | 164 | { | 
 | 165 | 	struct dentry *dentry = get_node(number); | 
 | 166 |  | 
 | 167 | 	if (!IS_ERR(dentry)) { | 
 | 168 | 		struct inode *inode = dentry->d_inode; | 
 | 169 | 		if (inode) { | 
 | 170 | 			inode->i_nlink--; | 
 | 171 | 			d_delete(dentry); | 
 | 172 | 			dput(dentry); | 
 | 173 | 		} | 
 | 174 | 		dput(dentry); | 
 | 175 | 	} | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 176 | 	mutex_unlock(&capifs_root->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } | 
 | 178 |  | 
 | 179 | static int __init capifs_init(void) | 
 | 180 | { | 
 | 181 | 	char rev[32]; | 
 | 182 | 	char *p; | 
 | 183 | 	int err; | 
 | 184 |  | 
 | 185 | 	if ((p = strchr(revision, ':')) != 0 && p[1]) { | 
 | 186 | 		strlcpy(rev, p + 2, sizeof(rev)); | 
 | 187 | 		if ((p = strchr(rev, '$')) != 0 && p > rev) | 
 | 188 | 		   *(p-1) = 0; | 
 | 189 | 	} else | 
 | 190 | 		strcpy(rev, "1.0"); | 
 | 191 |  | 
 | 192 | 	err = register_filesystem(&capifs_fs_type); | 
 | 193 | 	if (!err) { | 
 | 194 | 		capifs_mnt = kern_mount(&capifs_fs_type); | 
| James Morris | 820d220 | 2005-08-27 13:47:06 +0200 | [diff] [blame] | 195 | 		if (IS_ERR(capifs_mnt)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 			err = PTR_ERR(capifs_mnt); | 
| James Morris | 820d220 | 2005-08-27 13:47:06 +0200 | [diff] [blame] | 197 | 			unregister_filesystem(&capifs_fs_type); | 
 | 198 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 	} | 
 | 200 | 	if (!err) | 
 | 201 | 		printk(KERN_NOTICE "capifs: Rev %s\n", rev); | 
 | 202 | 	return err; | 
 | 203 | } | 
 | 204 |  | 
 | 205 | static void __exit capifs_exit(void) | 
 | 206 | { | 
 | 207 | 	unregister_filesystem(&capifs_fs_type); | 
 | 208 | 	mntput(capifs_mnt); | 
 | 209 | } | 
 | 210 |  | 
 | 211 | EXPORT_SYMBOL(capifs_new_ncci); | 
 | 212 | EXPORT_SYMBOL(capifs_free_ncci); | 
 | 213 |  | 
 | 214 | module_init(capifs_init); | 
 | 215 | module_exit(capifs_exit); |