blob: 2a129cb7bb568232e2c7869c26bd7061d62e3d9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
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 Torvalds1da177e2005-04-16 15:20:36 -070030#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 Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/parser.h>
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -070040#include <linux/notifier.h>
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010041#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/byteorder.h>
43#include "usb.h"
Greg KH6d5e8252005-04-18 17:39:24 -070044#include "hcd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010046#define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
47#define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
48#define USBFS_DEFAULT_LISTMODE S_IRUGO
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static struct super_operations usbfs_ops;
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -030051static const struct file_operations default_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static struct vfsmount *usbfs_mount;
53static int usbfs_mount_count; /* = 0 */
54static int ignore_mount = 0;
55
56static struct dentry *devices_usbfs_dentry;
57static int num_buses; /* = 0 */
58
59static uid_t devuid; /* = 0 */
60static uid_t busuid; /* = 0 */
61static uid_t listuid; /* = 0 */
62static gid_t devgid; /* = 0 */
63static gid_t busgid; /* = 0 */
64static gid_t listgid; /* = 0 */
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010065static umode_t devmode = USBFS_DEFAULT_DEVMODE;
66static umode_t busmode = USBFS_DEFAULT_BUSMODE;
67static umode_t listmode = USBFS_DEFAULT_LISTMODE;
68
69static 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 Torvalds1da177e2005-04-16 15:20:36 -070092
93enum {
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 Whitehousea447c092008-10-13 10:46:57 +0100100static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 {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
113static 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 Szeredi2e4f3c02008-01-24 20:34:07 +0100125 devmode = USBFS_DEFAULT_DEVMODE;
126 busmode = USBFS_DEFAULT_BUSMODE;
127 listmode = USBFS_DEFAULT_LISTMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
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-Hartman69a85942008-08-14 09:37:34 -0700183 printk(KERN_ERR "usbfs: unrecognised mount option "
184 "\"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -EINVAL;
186 }
187 }
188
189 return 0;
190}
191
192static 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
199static 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
206static 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 Sorensen1b1dcc12006-01-09 15:59:24 -0800214 mutex_lock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Eric Dumazet5160ee62006-01-08 01:03:32 -0800216 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (dev->d_inode)
218 update_dev(dev);
219
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800220 mutex_unlock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static 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 Molnarf2eace22006-07-03 00:25:05 -0700231 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric Dumazet5160ee62006-01-08 01:03:32 -0800233 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 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-Hartman3b6004f2008-08-14 09:37:34 -0700243 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 Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 }
248 }
249 }
250
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800251 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static 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-Hartman3b6004f2008-08-14 09:37:34 -0700264 printk(KERN_WARNING "usbfs: mount parameter error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
266 }
267
268 if (usbfs_mount && usbfs_mount->mnt_sb)
269 update_sb(usbfs_mount->mnt_sb);
270
271 return 0;
272}
273
274static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
275{
276 struct inode *inode = new_inode(sb);
277
278 if (inode) {
279 inode->i_mode = mode;
David Howellscd80ca82008-11-14 10:38:43 +1100280 inode->i_uid = current_fsuid();
281 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
283 switch (mode & S_IFMT) {
284 default:
285 init_special_inode(inode, mode, dev);
286 break;
287 case S_IFREG:
288 inode->i_fop = &default_file_operations;
289 break;
290 case S_IFDIR:
OGAWA Hirofumibc7cb322005-11-17 09:47:07 -0800291 inode->i_op = &simple_dir_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 inode->i_fop = &simple_dir_operations;
293
294 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700295 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298 }
299 return inode;
300}
301
302/* SMP-safe */
303static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
304 dev_t dev)
305{
306 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
307 int error = -EPERM;
308
309 if (dentry->d_inode)
310 return -EEXIST;
311
312 if (inode) {
313 d_instantiate(dentry, inode);
314 dget(dentry);
315 error = 0;
316 }
317 return error;
318}
319
320static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
321{
322 int res;
323
324 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
325 res = usbfs_mknod (dir, dentry, mode, 0);
326 if (!res)
Dave Hansend8c76e62006-09-30 23:29:04 -0700327 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return res;
329}
330
331static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
332{
333 mode = (mode & S_IALLUGO) | S_IFREG;
334 return usbfs_mknod (dir, dentry, mode, 0);
335}
336
337static inline int usbfs_positive (struct dentry *dentry)
338{
339 return dentry->d_inode && !d_unhashed(dentry);
340}
341
342static int usbfs_empty (struct dentry *dentry)
343{
344 struct list_head *list;
345
346 spin_lock(&dcache_lock);
347
348 list_for_each(list, &dentry->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800349 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (usbfs_positive(de)) {
351 spin_unlock(&dcache_lock);
352 return 0;
353 }
354 }
355
356 spin_unlock(&dcache_lock);
357 return 1;
358}
359
360static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
361{
362 struct inode *inode = dentry->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800363 mutex_lock(&inode->i_mutex);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700364 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dput(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800366 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 d_delete(dentry);
368 return 0;
369}
370
371static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
372{
373 int error = -ENOTEMPTY;
374 struct inode * inode = dentry->d_inode;
375
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800376 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dentry_unhash(dentry);
378 if (usbfs_empty(dentry)) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700379 drop_nlink(dentry->d_inode);
380 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dput(dentry);
382 inode->i_flags |= S_DEAD;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700383 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 error = 0;
385 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800386 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!error)
388 d_delete(dentry);
389 dput(dentry);
390 return error;
391}
392
393
394/* default file operations */
395static ssize_t default_read_file (struct file *file, char __user *buf,
396 size_t count, loff_t *ppos)
397{
398 return 0;
399}
400
401static ssize_t default_write_file (struct file *file, const char __user *buf,
402 size_t count, loff_t *ppos)
403{
404 return count;
405}
406
407static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
408{
409 loff_t retval = -EINVAL;
410
Josef Sipek33cb8992006-12-08 02:37:46 -0800411 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 switch(orig) {
413 case 0:
414 if (offset > 0) {
415 file->f_pos = offset;
416 retval = file->f_pos;
417 }
418 break;
419 case 1:
420 if ((offset + file->f_pos) > 0) {
421 file->f_pos += offset;
422 retval = file->f_pos;
423 }
424 break;
425 default:
426 break;
427 }
Josef Sipek33cb8992006-12-08 02:37:46 -0800428 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return retval;
430}
431
432static int default_open (struct inode *inode, struct file *file)
433{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700434 if (inode->i_private)
435 file->private_data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return 0;
438}
439
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300440static const struct file_operations default_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .read = default_read_file,
442 .write = default_write_file,
443 .open = default_open,
444 .llseek = default_file_lseek,
445};
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static struct super_operations usbfs_ops = {
448 .statfs = simple_statfs,
449 .drop_inode = generic_delete_inode,
450 .remount_fs = remount,
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +0100451 .show_options = usbfs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452};
453
454static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
455{
456 struct inode *inode;
457 struct dentry *root;
458
459 sb->s_blocksize = PAGE_CACHE_SIZE;
460 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
461 sb->s_magic = USBDEVICE_SUPER_MAGIC;
462 sb->s_op = &usbfs_ops;
463 sb->s_time_gran = 1;
464 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
465
466 if (!inode) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800467 dbg("%s: could not get inode!",__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -ENOMEM;
469 }
470
471 root = d_alloc_root(inode);
472 if (!root) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800473 dbg("%s: could not get root dentry!",__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 iput(inode);
475 return -ENOMEM;
476 }
477 sb->s_root = root;
478 return 0;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * fs_create_by_name - create a file, given a name
483 * @name: name of file
484 * @mode: type of file
485 * @parent: dentry of directory to create it in
486 * @dentry: resulting dentry of file
487 *
488 * This function handles both regular files and directories.
489 */
490static int fs_create_by_name (const char *name, mode_t mode,
491 struct dentry *parent, struct dentry **dentry)
492{
493 int error = 0;
494
495 /* If the parent is not specified, we create it in the root.
496 * We need the root dentry to do this, which is in the super
497 * block. A pointer to that is in the struct vfsmount that we
498 * have around.
499 */
500 if (!parent ) {
501 if (usbfs_mount && usbfs_mount->mnt_sb) {
502 parent = usbfs_mount->mnt_sb->s_root;
503 }
504 }
505
506 if (!parent) {
507 dbg("Ah! can not find a parent!");
508 return -EFAULT;
509 }
510
511 *dentry = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800512 mutex_lock(&parent->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700513 *dentry = lookup_one_len(name, parent, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!IS_ERR(dentry)) {
515 if ((mode & S_IFMT) == S_IFDIR)
516 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
517 else
518 error = usbfs_create (parent->d_inode, *dentry, mode);
519 } else
520 error = PTR_ERR(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800521 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 return error;
524}
525
526static struct dentry *fs_create_file (const char *name, mode_t mode,
527 struct dentry *parent, void *data,
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300528 const struct file_operations *fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 uid_t uid, gid_t gid)
530{
531 struct dentry *dentry;
532 int error;
533
534 dbg("creating file '%s'",name);
535
536 error = fs_create_by_name (name, mode, parent, &dentry);
537 if (error) {
538 dentry = NULL;
539 } else {
540 if (dentry->d_inode) {
541 if (data)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700542 dentry->d_inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (fops)
544 dentry->d_inode->i_fop = fops;
545 dentry->d_inode->i_uid = uid;
546 dentry->d_inode->i_gid = gid;
547 }
548 }
549
550 return dentry;
551}
552
553static void fs_remove_file (struct dentry *dentry)
554{
555 struct dentry *parent = dentry->d_parent;
556
557 if (!parent || !parent->d_inode)
558 return;
559
Arjan van de Ven8e7795e2006-07-03 00:25:21 -0700560 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (usbfs_positive(dentry)) {
562 if (dentry->d_inode) {
563 if (S_ISDIR(dentry->d_inode->i_mode))
564 usbfs_rmdir(parent->d_inode, dentry);
565 else
566 usbfs_unlink(parent->d_inode, dentry);
567 dput(dentry);
568 }
569 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800570 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/* --------------------------------------------------------------------- */
574
David Howells454e2392006-06-23 02:02:57 -0700575static int usb_get_sb(struct file_system_type *fs_type,
576 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
David Howells454e2392006-06-23 02:02:57 -0700578 return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static struct file_system_type usb_fs_type = {
582 .owner = THIS_MODULE,
583 .name = "usbfs",
584 .get_sb = usb_get_sb,
585 .kill_sb = kill_litter_super,
586};
587
588/* --------------------------------------------------------------------- */
589
590static int create_special_files (void)
591{
592 struct dentry *parent;
593 int retval;
594
595 /* the simple_pin_fs calls will call remount with no options
596 * without this flag that would overwrite the real mount options (if any)
597 */
598 ignore_mount = 1;
599
600 /* create the devices special file */
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400601 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700603 printk(KERN_ERR "Unable to get usbfs mount\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto exit;
605 }
606
607 ignore_mount = 0;
608
609 parent = usbfs_mount->mnt_sb->s_root;
610 devices_usbfs_dentry = fs_create_file ("devices",
611 listmode | S_IFREG, parent,
612 NULL, &usbfs_devices_fops,
613 listuid, listgid);
614 if (devices_usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700615 printk(KERN_ERR "Unable to create devices usbfs file\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 retval = -ENODEV;
617 goto error_clean_mounts;
618 }
619
620 goto exit;
621
622error_clean_mounts:
623 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
624exit:
625 return retval;
626}
627
628static void remove_special_files (void)
629{
630 if (devices_usbfs_dentry)
631 fs_remove_file (devices_usbfs_dentry);
632 devices_usbfs_dentry = NULL;
633 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
634}
635
636void usbfs_update_special (void)
637{
638 struct inode *inode;
639
640 if (devices_usbfs_dentry) {
641 inode = devices_usbfs_dentry->d_inode;
642 if (inode)
643 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
644 }
645}
646
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700647static void usbfs_add_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct dentry *parent;
650 char name[8];
651 int retval;
652
653 /* create the special files if this is the first bus added */
654 if (num_buses == 0) {
655 retval = create_special_files();
656 if (retval)
657 return;
658 }
659 ++num_buses;
660
661 sprintf (name, "%03d", bus->busnum);
662
663 parent = usbfs_mount->mnt_sb->s_root;
664 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
665 bus, NULL, busuid, busgid);
666 if (bus->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700667 printk(KERN_ERR "Error creating usbfs bus entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700672static void usbfs_remove_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 if (bus->usbfs_dentry) {
675 fs_remove_file (bus->usbfs_dentry);
676 bus->usbfs_dentry = NULL;
677 }
678
679 --num_buses;
680 if (num_buses <= 0) {
681 remove_special_files();
682 num_buses = 0;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700686static void usbfs_add_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 char name[8];
689 int i;
690 int i_size;
691
692 sprintf (name, "%03d", dev->devnum);
693 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
694 dev->bus->usbfs_dentry, dev,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100695 &usbdev_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 devuid, devgid);
697 if (dev->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700698 printk(KERN_ERR "Error creating usbfs device entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return;
700 }
701
702 /* Set the size of the device's file to be
703 * equal to the size of the device descriptors. */
704 i_size = sizeof (struct usb_device_descriptor);
705 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
706 struct usb_config_descriptor *config =
707 (struct usb_config_descriptor *)dev->rawdescriptors[i];
708 i_size += le16_to_cpu(config->wTotalLength);
709 }
710 if (dev->usbfs_dentry->d_inode)
711 dev->usbfs_dentry->d_inode->i_size = i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700714static void usbfs_remove_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (dev->usbfs_dentry) {
717 fs_remove_file (dev->usbfs_dentry);
718 dev->usbfs_dentry = NULL;
719 }
Alan Sterncd9f0372008-06-24 14:47:04 -0400720 usb_fs_classdev_common_remove(dev);
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700721}
722
723static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
724{
725 switch (action) {
726 case USB_DEVICE_ADD:
727 usbfs_add_device(dev);
728 break;
729 case USB_DEVICE_REMOVE:
730 usbfs_remove_device(dev);
731 break;
732 case USB_BUS_ADD:
733 usbfs_add_bus(dev);
734 break;
735 case USB_BUS_REMOVE:
736 usbfs_remove_bus(dev);
737 }
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 usbfs_update_special();
740 usbfs_conn_disc_event();
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700741 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700744static struct notifier_block usbfs_nb = {
745 .notifier_call = usbfs_notify,
746};
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* --------------------------------------------------------------------- */
749
750static struct proc_dir_entry *usbdir = NULL;
751
752int __init usbfs_init(void)
753{
754 int retval;
755
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200756 retval = register_filesystem(&usb_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (retval)
758 return retval;
759
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700760 usb_register_notify(&usbfs_nb);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* create mount point for usbfs */
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700763 usbdir = proc_mkdir("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 return 0;
766}
767
768void usbfs_cleanup(void)
769{
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700770 usb_unregister_notify(&usbfs_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unregister_filesystem(&usb_fs_type);
772 if (usbdir)
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700773 remove_proc_entry("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775