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