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