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