| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*****************************************************************************/ | 
|  | 2 |  | 
|  | 3 | /* | 
|  | 4 | *	inode.c  --  Inode/Dentry functions for the USB device file system. | 
|  | 5 | * | 
|  | 6 | *	Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch) | 
|  | 7 | *	Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com) | 
|  | 8 | * | 
|  | 9 | *	This program is free software; you can redistribute it and/or modify | 
|  | 10 | *	it under the terms of the GNU General Public License as published by | 
|  | 11 | *	the Free Software Foundation; either version 2 of the License, or | 
|  | 12 | *	(at your option) any later version. | 
|  | 13 | * | 
|  | 14 | *	This program is distributed in the hope that it will be useful, | 
|  | 15 | *	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | *	GNU General Public License for more details. | 
|  | 18 | * | 
|  | 19 | *	You should have received a copy of the GNU General Public License | 
|  | 20 | *	along with this program; if not, write to the Free Software | 
|  | 21 | *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 22 | * | 
|  | 23 | *  History: | 
|  | 24 | *   0.1  04.01.2000  Created | 
|  | 25 | *   0.2  10.12.2001  converted to use the vfs layer better | 
|  | 26 | */ | 
|  | 27 |  | 
|  | 28 | /*****************************************************************************/ | 
|  | 29 |  | 
|  | 30 | #include <linux/config.h> | 
|  | 31 | #include <linux/module.h> | 
|  | 32 | #include <linux/fs.h> | 
|  | 33 | #include <linux/mount.h> | 
|  | 34 | #include <linux/pagemap.h> | 
|  | 35 | #include <linux/init.h> | 
|  | 36 | #include <linux/proc_fs.h> | 
|  | 37 | #include <linux/usb.h> | 
|  | 38 | #include <linux/namei.h> | 
|  | 39 | #include <linux/usbdevice_fs.h> | 
|  | 40 | #include <linux/smp_lock.h> | 
|  | 41 | #include <linux/parser.h> | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 42 | #include <linux/notifier.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/byteorder.h> | 
|  | 44 | #include "usb.h" | 
| Greg KH | 6d5e825 | 2005-04-18 17:39:24 -0700 | [diff] [blame] | 45 | #include "hcd.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | static struct super_operations usbfs_ops; | 
|  | 48 | static struct file_operations default_file_operations; | 
|  | 49 | static struct inode_operations usbfs_dir_inode_operations; | 
|  | 50 | static struct vfsmount *usbfs_mount; | 
|  | 51 | static int usbfs_mount_count;	/* = 0 */ | 
|  | 52 | static int ignore_mount = 0; | 
|  | 53 |  | 
|  | 54 | static struct dentry *devices_usbfs_dentry; | 
|  | 55 | static int num_buses;	/* = 0 */ | 
|  | 56 |  | 
|  | 57 | static uid_t devuid;	/* = 0 */ | 
|  | 58 | static uid_t busuid;	/* = 0 */ | 
|  | 59 | static uid_t listuid;	/* = 0 */ | 
|  | 60 | static gid_t devgid;	/* = 0 */ | 
|  | 61 | static gid_t busgid;	/* = 0 */ | 
|  | 62 | static gid_t listgid;	/* = 0 */ | 
|  | 63 | static umode_t devmode = S_IWUSR | S_IRUGO; | 
|  | 64 | static umode_t busmode = S_IXUGO | S_IRUGO; | 
|  | 65 | static umode_t listmode = S_IRUGO; | 
|  | 66 |  | 
|  | 67 | enum { | 
|  | 68 | Opt_devuid, Opt_devgid, Opt_devmode, | 
|  | 69 | Opt_busuid, Opt_busgid, Opt_busmode, | 
|  | 70 | Opt_listuid, Opt_listgid, Opt_listmode, | 
|  | 71 | Opt_err, | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | static match_table_t tokens = { | 
|  | 75 | {Opt_devuid, "devuid=%u"}, | 
|  | 76 | {Opt_devgid, "devgid=%u"}, | 
|  | 77 | {Opt_devmode, "devmode=%o"}, | 
|  | 78 | {Opt_busuid, "busuid=%u"}, | 
|  | 79 | {Opt_busgid, "busgid=%u"}, | 
|  | 80 | {Opt_busmode, "busmode=%o"}, | 
|  | 81 | {Opt_listuid, "listuid=%u"}, | 
|  | 82 | {Opt_listgid, "listgid=%u"}, | 
|  | 83 | {Opt_listmode, "listmode=%o"}, | 
|  | 84 | {Opt_err, NULL} | 
|  | 85 | }; | 
|  | 86 |  | 
|  | 87 | static int parse_options(struct super_block *s, char *data) | 
|  | 88 | { | 
|  | 89 | char *p; | 
|  | 90 | int option; | 
|  | 91 |  | 
|  | 92 | /* (re)set to defaults. */ | 
|  | 93 | devuid = 0; | 
|  | 94 | busuid = 0; | 
|  | 95 | listuid = 0; | 
|  | 96 | devgid = 0; | 
|  | 97 | busgid = 0; | 
|  | 98 | listgid = 0; | 
|  | 99 | devmode = S_IWUSR | S_IRUGO; | 
|  | 100 | busmode = S_IXUGO | S_IRUGO; | 
|  | 101 | listmode = S_IRUGO; | 
|  | 102 |  | 
|  | 103 | while ((p = strsep(&data, ",")) != NULL) { | 
|  | 104 | substring_t args[MAX_OPT_ARGS]; | 
|  | 105 | int token; | 
|  | 106 | if (!*p) | 
|  | 107 | continue; | 
|  | 108 |  | 
|  | 109 | token = match_token(p, tokens, args); | 
|  | 110 | switch (token) { | 
|  | 111 | case Opt_devuid: | 
|  | 112 | if (match_int(&args[0], &option)) | 
|  | 113 | return -EINVAL; | 
|  | 114 | devuid = option; | 
|  | 115 | break; | 
|  | 116 | case Opt_devgid: | 
|  | 117 | if (match_int(&args[0], &option)) | 
|  | 118 | return -EINVAL; | 
|  | 119 | devgid = option; | 
|  | 120 | break; | 
|  | 121 | case Opt_devmode: | 
|  | 122 | if (match_octal(&args[0], &option)) | 
|  | 123 | return -EINVAL; | 
|  | 124 | devmode = option & S_IRWXUGO; | 
|  | 125 | break; | 
|  | 126 | case Opt_busuid: | 
|  | 127 | if (match_int(&args[0], &option)) | 
|  | 128 | return -EINVAL; | 
|  | 129 | busuid = option; | 
|  | 130 | break; | 
|  | 131 | case Opt_busgid: | 
|  | 132 | if (match_int(&args[0], &option)) | 
|  | 133 | return -EINVAL; | 
|  | 134 | busgid = option; | 
|  | 135 | break; | 
|  | 136 | case Opt_busmode: | 
|  | 137 | if (match_octal(&args[0], &option)) | 
|  | 138 | return -EINVAL; | 
|  | 139 | busmode = option & S_IRWXUGO; | 
|  | 140 | break; | 
|  | 141 | case Opt_listuid: | 
|  | 142 | if (match_int(&args[0], &option)) | 
|  | 143 | return -EINVAL; | 
|  | 144 | listuid = option; | 
|  | 145 | break; | 
|  | 146 | case Opt_listgid: | 
|  | 147 | if (match_int(&args[0], &option)) | 
|  | 148 | return -EINVAL; | 
|  | 149 | listgid = option; | 
|  | 150 | break; | 
|  | 151 | case Opt_listmode: | 
|  | 152 | if (match_octal(&args[0], &option)) | 
|  | 153 | return -EINVAL; | 
|  | 154 | listmode = option & S_IRWXUGO; | 
|  | 155 | break; | 
|  | 156 | default: | 
|  | 157 | err("usbfs: unrecognised mount option \"%s\" " | 
|  | 158 | "or missing value\n", p); | 
|  | 159 | return -EINVAL; | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | static void update_special(struct dentry *special) | 
|  | 167 | { | 
|  | 168 | special->d_inode->i_uid = listuid; | 
|  | 169 | special->d_inode->i_gid = listgid; | 
|  | 170 | special->d_inode->i_mode = S_IFREG | listmode; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | static void update_dev(struct dentry *dev) | 
|  | 174 | { | 
|  | 175 | dev->d_inode->i_uid = devuid; | 
|  | 176 | dev->d_inode->i_gid = devgid; | 
|  | 177 | dev->d_inode->i_mode = S_IFREG | devmode; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | static void update_bus(struct dentry *bus) | 
|  | 181 | { | 
|  | 182 | struct dentry *dev = NULL; | 
|  | 183 |  | 
|  | 184 | bus->d_inode->i_uid = busuid; | 
|  | 185 | bus->d_inode->i_gid = busgid; | 
|  | 186 | bus->d_inode->i_mode = S_IFDIR | busmode; | 
|  | 187 |  | 
|  | 188 | down(&bus->d_inode->i_sem); | 
|  | 189 |  | 
|  | 190 | list_for_each_entry(dev, &bus->d_subdirs, d_child) | 
|  | 191 | if (dev->d_inode) | 
|  | 192 | update_dev(dev); | 
|  | 193 |  | 
|  | 194 | up(&bus->d_inode->i_sem); | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | static void update_sb(struct super_block *sb) | 
|  | 198 | { | 
|  | 199 | struct dentry *root = sb->s_root; | 
|  | 200 | struct dentry *bus = NULL; | 
|  | 201 |  | 
|  | 202 | if (!root) | 
|  | 203 | return; | 
|  | 204 |  | 
|  | 205 | down(&root->d_inode->i_sem); | 
|  | 206 |  | 
|  | 207 | list_for_each_entry(bus, &root->d_subdirs, d_child) { | 
|  | 208 | if (bus->d_inode) { | 
|  | 209 | switch (S_IFMT & bus->d_inode->i_mode) { | 
|  | 210 | case S_IFDIR: | 
|  | 211 | update_bus(bus); | 
|  | 212 | break; | 
|  | 213 | case S_IFREG: | 
|  | 214 | update_special(bus); | 
|  | 215 | break; | 
|  | 216 | default: | 
|  | 217 | warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode); | 
|  | 218 | break; | 
|  | 219 | } | 
|  | 220 | } | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | up(&root->d_inode->i_sem); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | static int remount(struct super_block *sb, int *flags, char *data) | 
|  | 227 | { | 
|  | 228 | /* If this is not a real mount, | 
|  | 229 | * i.e. it's a simple_pin_fs from create_special_files, | 
|  | 230 | * then ignore it. | 
|  | 231 | */ | 
|  | 232 | if (ignore_mount) | 
|  | 233 | return 0; | 
|  | 234 |  | 
|  | 235 | if (parse_options(sb, data)) { | 
|  | 236 | warn("usbfs: mount parameter error:"); | 
|  | 237 | return -EINVAL; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | if (usbfs_mount && usbfs_mount->mnt_sb) | 
|  | 241 | update_sb(usbfs_mount->mnt_sb); | 
|  | 242 |  | 
|  | 243 | return 0; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev) | 
|  | 247 | { | 
|  | 248 | struct inode *inode = new_inode(sb); | 
|  | 249 |  | 
|  | 250 | if (inode) { | 
|  | 251 | inode->i_mode = mode; | 
|  | 252 | inode->i_uid = current->fsuid; | 
|  | 253 | inode->i_gid = current->fsgid; | 
|  | 254 | inode->i_blksize = PAGE_CACHE_SIZE; | 
|  | 255 | inode->i_blocks = 0; | 
|  | 256 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 
|  | 257 | switch (mode & S_IFMT) { | 
|  | 258 | default: | 
|  | 259 | init_special_inode(inode, mode, dev); | 
|  | 260 | break; | 
|  | 261 | case S_IFREG: | 
|  | 262 | inode->i_fop = &default_file_operations; | 
|  | 263 | break; | 
|  | 264 | case S_IFDIR: | 
|  | 265 | inode->i_op = &usbfs_dir_inode_operations; | 
|  | 266 | inode->i_fop = &simple_dir_operations; | 
|  | 267 |  | 
|  | 268 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ | 
|  | 269 | inode->i_nlink++; | 
|  | 270 | break; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 | return inode; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | /* SMP-safe */ | 
|  | 277 | static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode, | 
|  | 278 | dev_t dev) | 
|  | 279 | { | 
|  | 280 | struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev); | 
|  | 281 | int error = -EPERM; | 
|  | 282 |  | 
|  | 283 | if (dentry->d_inode) | 
|  | 284 | return -EEXIST; | 
|  | 285 |  | 
|  | 286 | if (inode) { | 
|  | 287 | d_instantiate(dentry, inode); | 
|  | 288 | dget(dentry); | 
|  | 289 | error = 0; | 
|  | 290 | } | 
|  | 291 | return error; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode) | 
|  | 295 | { | 
|  | 296 | int res; | 
|  | 297 |  | 
|  | 298 | mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR; | 
|  | 299 | res = usbfs_mknod (dir, dentry, mode, 0); | 
|  | 300 | if (!res) | 
|  | 301 | dir->i_nlink++; | 
|  | 302 | return res; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode) | 
|  | 306 | { | 
|  | 307 | mode = (mode & S_IALLUGO) | S_IFREG; | 
|  | 308 | return usbfs_mknod (dir, dentry, mode, 0); | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | static inline int usbfs_positive (struct dentry *dentry) | 
|  | 312 | { | 
|  | 313 | return dentry->d_inode && !d_unhashed(dentry); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | static int usbfs_empty (struct dentry *dentry) | 
|  | 317 | { | 
|  | 318 | struct list_head *list; | 
|  | 319 |  | 
|  | 320 | spin_lock(&dcache_lock); | 
|  | 321 |  | 
|  | 322 | list_for_each(list, &dentry->d_subdirs) { | 
|  | 323 | struct dentry *de = list_entry(list, struct dentry, d_child); | 
|  | 324 | if (usbfs_positive(de)) { | 
|  | 325 | spin_unlock(&dcache_lock); | 
|  | 326 | return 0; | 
|  | 327 | } | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | spin_unlock(&dcache_lock); | 
|  | 331 | return 1; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | static int usbfs_unlink (struct inode *dir, struct dentry *dentry) | 
|  | 335 | { | 
|  | 336 | struct inode *inode = dentry->d_inode; | 
|  | 337 | down(&inode->i_sem); | 
|  | 338 | dentry->d_inode->i_nlink--; | 
|  | 339 | dput(dentry); | 
|  | 340 | up(&inode->i_sem); | 
|  | 341 | d_delete(dentry); | 
|  | 342 | return 0; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | static int usbfs_rmdir(struct inode *dir, struct dentry *dentry) | 
|  | 346 | { | 
|  | 347 | int error = -ENOTEMPTY; | 
|  | 348 | struct inode * inode = dentry->d_inode; | 
|  | 349 |  | 
|  | 350 | down(&inode->i_sem); | 
|  | 351 | dentry_unhash(dentry); | 
|  | 352 | if (usbfs_empty(dentry)) { | 
|  | 353 | dentry->d_inode->i_nlink -= 2; | 
|  | 354 | dput(dentry); | 
|  | 355 | inode->i_flags |= S_DEAD; | 
|  | 356 | dir->i_nlink--; | 
|  | 357 | error = 0; | 
|  | 358 | } | 
|  | 359 | up(&inode->i_sem); | 
|  | 360 | if (!error) | 
|  | 361 | d_delete(dentry); | 
|  | 362 | dput(dentry); | 
|  | 363 | return error; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 |  | 
|  | 367 | /* default file operations */ | 
|  | 368 | static ssize_t default_read_file (struct file *file, char __user *buf, | 
|  | 369 | size_t count, loff_t *ppos) | 
|  | 370 | { | 
|  | 371 | return 0; | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | static ssize_t default_write_file (struct file *file, const char __user *buf, | 
|  | 375 | size_t count, loff_t *ppos) | 
|  | 376 | { | 
|  | 377 | return count; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | static loff_t default_file_lseek (struct file *file, loff_t offset, int orig) | 
|  | 381 | { | 
|  | 382 | loff_t retval = -EINVAL; | 
|  | 383 |  | 
|  | 384 | down(&file->f_dentry->d_inode->i_sem); | 
|  | 385 | switch(orig) { | 
|  | 386 | case 0: | 
|  | 387 | if (offset > 0) { | 
|  | 388 | file->f_pos = offset; | 
|  | 389 | retval = file->f_pos; | 
|  | 390 | } | 
|  | 391 | break; | 
|  | 392 | case 1: | 
|  | 393 | if ((offset + file->f_pos) > 0) { | 
|  | 394 | file->f_pos += offset; | 
|  | 395 | retval = file->f_pos; | 
|  | 396 | } | 
|  | 397 | break; | 
|  | 398 | default: | 
|  | 399 | break; | 
|  | 400 | } | 
|  | 401 | up(&file->f_dentry->d_inode->i_sem); | 
|  | 402 | return retval; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | static int default_open (struct inode *inode, struct file *file) | 
|  | 406 | { | 
|  | 407 | if (inode->u.generic_ip) | 
|  | 408 | file->private_data = inode->u.generic_ip; | 
|  | 409 |  | 
|  | 410 | return 0; | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | static struct file_operations default_file_operations = { | 
|  | 414 | .read =		default_read_file, | 
|  | 415 | .write =	default_write_file, | 
|  | 416 | .open =		default_open, | 
|  | 417 | .llseek =	default_file_lseek, | 
|  | 418 | }; | 
|  | 419 |  | 
|  | 420 | static struct inode_operations usbfs_dir_inode_operations = { | 
|  | 421 | .lookup =	simple_lookup, | 
|  | 422 | }; | 
|  | 423 |  | 
|  | 424 | static struct super_operations usbfs_ops = { | 
|  | 425 | .statfs =	simple_statfs, | 
|  | 426 | .drop_inode =	generic_delete_inode, | 
|  | 427 | .remount_fs =	remount, | 
|  | 428 | }; | 
|  | 429 |  | 
|  | 430 | static int usbfs_fill_super(struct super_block *sb, void *data, int silent) | 
|  | 431 | { | 
|  | 432 | struct inode *inode; | 
|  | 433 | struct dentry *root; | 
|  | 434 |  | 
|  | 435 | sb->s_blocksize = PAGE_CACHE_SIZE; | 
|  | 436 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | 
|  | 437 | sb->s_magic = USBDEVICE_SUPER_MAGIC; | 
|  | 438 | sb->s_op = &usbfs_ops; | 
|  | 439 | sb->s_time_gran = 1; | 
|  | 440 | inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0); | 
|  | 441 |  | 
|  | 442 | if (!inode) { | 
|  | 443 | dbg("%s: could not get inode!",__FUNCTION__); | 
|  | 444 | return -ENOMEM; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | root = d_alloc_root(inode); | 
|  | 448 | if (!root) { | 
|  | 449 | dbg("%s: could not get root dentry!",__FUNCTION__); | 
|  | 450 | iput(inode); | 
|  | 451 | return -ENOMEM; | 
|  | 452 | } | 
|  | 453 | sb->s_root = root; | 
|  | 454 | return 0; | 
|  | 455 | } | 
|  | 456 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | /* | 
|  | 458 | * fs_create_by_name - create a file, given a name | 
|  | 459 | * @name:	name of file | 
|  | 460 | * @mode:	type of file | 
|  | 461 | * @parent:	dentry of directory to create it in | 
|  | 462 | * @dentry:	resulting dentry of file | 
|  | 463 | * | 
|  | 464 | * This function handles both regular files and directories. | 
|  | 465 | */ | 
|  | 466 | static int fs_create_by_name (const char *name, mode_t mode, | 
|  | 467 | struct dentry *parent, struct dentry **dentry) | 
|  | 468 | { | 
|  | 469 | int error = 0; | 
|  | 470 |  | 
|  | 471 | /* If the parent is not specified, we create it in the root. | 
|  | 472 | * We need the root dentry to do this, which is in the super | 
|  | 473 | * block. A pointer to that is in the struct vfsmount that we | 
|  | 474 | * have around. | 
|  | 475 | */ | 
|  | 476 | if (!parent ) { | 
|  | 477 | if (usbfs_mount && usbfs_mount->mnt_sb) { | 
|  | 478 | parent = usbfs_mount->mnt_sb->s_root; | 
|  | 479 | } | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | if (!parent) { | 
|  | 483 | dbg("Ah! can not find a parent!"); | 
|  | 484 | return -EFAULT; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | *dentry = NULL; | 
|  | 488 | down(&parent->d_inode->i_sem); | 
| Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 489 | *dentry = lookup_one_len(name, parent, strlen(name)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (!IS_ERR(dentry)) { | 
|  | 491 | if ((mode & S_IFMT) == S_IFDIR) | 
|  | 492 | error = usbfs_mkdir (parent->d_inode, *dentry, mode); | 
|  | 493 | else | 
|  | 494 | error = usbfs_create (parent->d_inode, *dentry, mode); | 
|  | 495 | } else | 
|  | 496 | error = PTR_ERR(dentry); | 
|  | 497 | up(&parent->d_inode->i_sem); | 
|  | 498 |  | 
|  | 499 | return error; | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | static struct dentry *fs_create_file (const char *name, mode_t mode, | 
|  | 503 | struct dentry *parent, void *data, | 
|  | 504 | struct file_operations *fops, | 
|  | 505 | uid_t uid, gid_t gid) | 
|  | 506 | { | 
|  | 507 | struct dentry *dentry; | 
|  | 508 | int error; | 
|  | 509 |  | 
|  | 510 | dbg("creating file '%s'",name); | 
|  | 511 |  | 
|  | 512 | error = fs_create_by_name (name, mode, parent, &dentry); | 
|  | 513 | if (error) { | 
|  | 514 | dentry = NULL; | 
|  | 515 | } else { | 
|  | 516 | if (dentry->d_inode) { | 
|  | 517 | if (data) | 
|  | 518 | dentry->d_inode->u.generic_ip = data; | 
|  | 519 | if (fops) | 
|  | 520 | dentry->d_inode->i_fop = fops; | 
|  | 521 | dentry->d_inode->i_uid = uid; | 
|  | 522 | dentry->d_inode->i_gid = gid; | 
|  | 523 | } | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | return dentry; | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | static void fs_remove_file (struct dentry *dentry) | 
|  | 530 | { | 
|  | 531 | struct dentry *parent = dentry->d_parent; | 
|  | 532 |  | 
|  | 533 | if (!parent || !parent->d_inode) | 
|  | 534 | return; | 
|  | 535 |  | 
|  | 536 | down(&parent->d_inode->i_sem); | 
|  | 537 | if (usbfs_positive(dentry)) { | 
|  | 538 | if (dentry->d_inode) { | 
|  | 539 | if (S_ISDIR(dentry->d_inode->i_mode)) | 
|  | 540 | usbfs_rmdir(parent->d_inode, dentry); | 
|  | 541 | else | 
|  | 542 | usbfs_unlink(parent->d_inode, dentry); | 
|  | 543 | dput(dentry); | 
|  | 544 | } | 
|  | 545 | } | 
|  | 546 | up(&parent->d_inode->i_sem); | 
|  | 547 | } | 
|  | 548 |  | 
|  | 549 | /* --------------------------------------------------------------------- */ | 
|  | 550 |  | 
|  | 551 | static struct super_block *usb_get_sb(struct file_system_type *fs_type, | 
|  | 552 | int flags, const char *dev_name, void *data) | 
|  | 553 | { | 
|  | 554 | return get_sb_single(fs_type, flags, data, usbfs_fill_super); | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | static struct file_system_type usb_fs_type = { | 
|  | 558 | .owner =	THIS_MODULE, | 
|  | 559 | .name =		"usbfs", | 
|  | 560 | .get_sb =	usb_get_sb, | 
|  | 561 | .kill_sb =	kill_litter_super, | 
|  | 562 | }; | 
|  | 563 |  | 
|  | 564 | /* --------------------------------------------------------------------- */ | 
|  | 565 |  | 
|  | 566 | static int create_special_files (void) | 
|  | 567 | { | 
|  | 568 | struct dentry *parent; | 
|  | 569 | int retval; | 
|  | 570 |  | 
|  | 571 | /* the simple_pin_fs calls will call remount with no options | 
|  | 572 | * without this flag that would overwrite the real mount options (if any) | 
|  | 573 | */ | 
|  | 574 | ignore_mount = 1; | 
|  | 575 |  | 
|  | 576 | /* create the devices special file */ | 
|  | 577 | retval = simple_pin_fs("usbfs", &usbfs_mount, &usbfs_mount_count); | 
|  | 578 | if (retval) { | 
|  | 579 | err ("Unable to get usbfs mount"); | 
|  | 580 | goto exit; | 
|  | 581 | } | 
|  | 582 |  | 
|  | 583 | ignore_mount = 0; | 
|  | 584 |  | 
|  | 585 | parent = usbfs_mount->mnt_sb->s_root; | 
|  | 586 | devices_usbfs_dentry = fs_create_file ("devices", | 
|  | 587 | listmode | S_IFREG, parent, | 
|  | 588 | NULL, &usbfs_devices_fops, | 
|  | 589 | listuid, listgid); | 
|  | 590 | if (devices_usbfs_dentry == NULL) { | 
|  | 591 | err ("Unable to create devices usbfs file"); | 
|  | 592 | retval = -ENODEV; | 
|  | 593 | goto error_clean_mounts; | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | goto exit; | 
|  | 597 |  | 
|  | 598 | error_clean_mounts: | 
|  | 599 | simple_release_fs(&usbfs_mount, &usbfs_mount_count); | 
|  | 600 | exit: | 
|  | 601 | return retval; | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | static void remove_special_files (void) | 
|  | 605 | { | 
|  | 606 | if (devices_usbfs_dentry) | 
|  | 607 | fs_remove_file (devices_usbfs_dentry); | 
|  | 608 | devices_usbfs_dentry = NULL; | 
|  | 609 | simple_release_fs(&usbfs_mount, &usbfs_mount_count); | 
|  | 610 | } | 
|  | 611 |  | 
|  | 612 | void usbfs_update_special (void) | 
|  | 613 | { | 
|  | 614 | struct inode *inode; | 
|  | 615 |  | 
|  | 616 | if (devices_usbfs_dentry) { | 
|  | 617 | inode = devices_usbfs_dentry->d_inode; | 
|  | 618 | if (inode) | 
|  | 619 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 
|  | 620 | } | 
|  | 621 | } | 
|  | 622 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 623 | static void usbfs_add_bus(struct usb_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { | 
|  | 625 | struct dentry *parent; | 
|  | 626 | char name[8]; | 
|  | 627 | int retval; | 
|  | 628 |  | 
|  | 629 | /* create the special files if this is the first bus added */ | 
|  | 630 | if (num_buses == 0) { | 
|  | 631 | retval = create_special_files(); | 
|  | 632 | if (retval) | 
|  | 633 | return; | 
|  | 634 | } | 
|  | 635 | ++num_buses; | 
|  | 636 |  | 
|  | 637 | sprintf (name, "%03d", bus->busnum); | 
|  | 638 |  | 
|  | 639 | parent = usbfs_mount->mnt_sb->s_root; | 
|  | 640 | bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent, | 
|  | 641 | bus, NULL, busuid, busgid); | 
|  | 642 | if (bus->usbfs_dentry == NULL) { | 
|  | 643 | err ("error creating usbfs bus entry"); | 
|  | 644 | return; | 
|  | 645 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 648 | static void usbfs_remove_bus(struct usb_bus *bus) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { | 
|  | 650 | if (bus->usbfs_dentry) { | 
|  | 651 | fs_remove_file (bus->usbfs_dentry); | 
|  | 652 | bus->usbfs_dentry = NULL; | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | --num_buses; | 
|  | 656 | if (num_buses <= 0) { | 
|  | 657 | remove_special_files(); | 
|  | 658 | num_buses = 0; | 
|  | 659 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } | 
|  | 661 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 662 | static void usbfs_add_device(struct usb_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | { | 
|  | 664 | char name[8]; | 
|  | 665 | int i; | 
|  | 666 | int i_size; | 
|  | 667 |  | 
|  | 668 | sprintf (name, "%03d", dev->devnum); | 
|  | 669 | dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG, | 
|  | 670 | dev->bus->usbfs_dentry, dev, | 
|  | 671 | &usbfs_device_file_operations, | 
|  | 672 | devuid, devgid); | 
|  | 673 | if (dev->usbfs_dentry == NULL) { | 
|  | 674 | err ("error creating usbfs device entry"); | 
|  | 675 | return; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | /* Set the size of the device's file to be | 
|  | 679 | * equal to the size of the device descriptors. */ | 
|  | 680 | i_size = sizeof (struct usb_device_descriptor); | 
|  | 681 | for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) { | 
|  | 682 | struct usb_config_descriptor *config = | 
|  | 683 | (struct usb_config_descriptor *)dev->rawdescriptors[i]; | 
|  | 684 | i_size += le16_to_cpu(config->wTotalLength); | 
|  | 685 | } | 
|  | 686 | if (dev->usbfs_dentry->d_inode) | 
|  | 687 | dev->usbfs_dentry->d_inode->i_size = i_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } | 
|  | 689 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 690 | static void usbfs_remove_device(struct usb_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { | 
|  | 692 | struct dev_state *ds; | 
|  | 693 | struct siginfo sinfo; | 
|  | 694 |  | 
|  | 695 | if (dev->usbfs_dentry) { | 
|  | 696 | fs_remove_file (dev->usbfs_dentry); | 
|  | 697 | dev->usbfs_dentry = NULL; | 
|  | 698 | } | 
|  | 699 | while (!list_empty(&dev->filelist)) { | 
|  | 700 | ds = list_entry(dev->filelist.next, struct dev_state, list); | 
|  | 701 | wake_up_all(&ds->wait); | 
|  | 702 | list_del_init(&ds->list); | 
|  | 703 | if (ds->discsignr) { | 
|  | 704 | sinfo.si_signo = SIGPIPE; | 
|  | 705 | sinfo.si_errno = EPIPE; | 
|  | 706 | sinfo.si_code = SI_ASYNCIO; | 
|  | 707 | sinfo.si_addr = ds->disccontext; | 
| Linus Torvalds | d7dd8a7 | 2005-10-10 16:31:30 -0700 | [diff] [blame] | 708 | kill_proc_info_as_uid(ds->discsignr, &sinfo, ds->disc_pid, ds->disc_uid, ds->disc_euid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } | 
|  | 710 | } | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 711 | } | 
|  | 712 |  | 
|  | 713 | static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev) | 
|  | 714 | { | 
|  | 715 | switch (action) { | 
|  | 716 | case USB_DEVICE_ADD: | 
|  | 717 | usbfs_add_device(dev); | 
|  | 718 | break; | 
|  | 719 | case USB_DEVICE_REMOVE: | 
|  | 720 | usbfs_remove_device(dev); | 
|  | 721 | break; | 
|  | 722 | case USB_BUS_ADD: | 
|  | 723 | usbfs_add_bus(dev); | 
|  | 724 | break; | 
|  | 725 | case USB_BUS_REMOVE: | 
|  | 726 | usbfs_remove_bus(dev); | 
|  | 727 | } | 
|  | 728 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | usbfs_update_special(); | 
|  | 730 | usbfs_conn_disc_event(); | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 731 | return NOTIFY_OK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } | 
|  | 733 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 734 | static struct notifier_block usbfs_nb = { | 
|  | 735 | .notifier_call = 	usbfs_notify, | 
|  | 736 | }; | 
|  | 737 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | /* --------------------------------------------------------------------- */ | 
|  | 739 |  | 
|  | 740 | static struct proc_dir_entry *usbdir = NULL; | 
|  | 741 |  | 
|  | 742 | int __init usbfs_init(void) | 
|  | 743 | { | 
|  | 744 | int retval; | 
|  | 745 |  | 
| Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 746 | retval = register_filesystem(&usb_fs_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | if (retval) | 
|  | 748 | return retval; | 
|  | 749 |  | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 750 | usb_register_notify(&usbfs_nb); | 
|  | 751 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | /* create mount point for usbfs */ | 
|  | 753 | usbdir = proc_mkdir("usb", proc_bus); | 
|  | 754 |  | 
|  | 755 | return 0; | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | void usbfs_cleanup(void) | 
|  | 759 | { | 
| Greg Kroah-Hartman | 54a5c4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 760 | usb_unregister_notify(&usbfs_nb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | unregister_filesystem(&usb_fs_type); | 
|  | 762 | if (usbdir) | 
|  | 763 | remove_proc_entry("usb", proc_bus); | 
|  | 764 | } | 
|  | 765 |  |