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