1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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/parser.h>
40#include <linux/notifier.h>
41#include <linux/seq_file.h>
42#include <linux/usb/hcd.h>
43#include <asm/byteorder.h>
44#include "usb.h"
45
46#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
50static const struct file_operations default_file_operations;
51static struct vfsmount *usbfs_mount;
52static int usbfs_mount_count;
53static int ignore_mount = 0;
54
55static struct dentry *devices_usbfs_dentry;
56static int num_buses;
57
58static uid_t devuid;
59static uid_t busuid;
60static uid_t listuid;
61static gid_t devgid;
62static gid_t busgid;
63static gid_t listgid;
64static umode_t devmode = USBFS_DEFAULT_DEVMODE;
65static umode_t busmode = USBFS_DEFAULT_BUSMODE;
66static umode_t listmode = USBFS_DEFAULT_LISTMODE;
67
68static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
69{
70 if (devuid != 0)
71 seq_printf(seq, ",devuid=%u", devuid);
72 if (devgid != 0)
73 seq_printf(seq, ",devgid=%u", devgid);
74 if (devmode != USBFS_DEFAULT_DEVMODE)
75 seq_printf(seq, ",devmode=%o", devmode);
76 if (busuid != 0)
77 seq_printf(seq, ",busuid=%u", busuid);
78 if (busgid != 0)
79 seq_printf(seq, ",busgid=%u", busgid);
80 if (busmode != USBFS_DEFAULT_BUSMODE)
81 seq_printf(seq, ",busmode=%o", busmode);
82 if (listuid != 0)
83 seq_printf(seq, ",listuid=%u", listuid);
84 if (listgid != 0)
85 seq_printf(seq, ",listgid=%u", listgid);
86 if (listmode != USBFS_DEFAULT_LISTMODE)
87 seq_printf(seq, ",listmode=%o", listmode);
88
89 return 0;
90}
91
92enum {
93 Opt_devuid, Opt_devgid, Opt_devmode,
94 Opt_busuid, Opt_busgid, Opt_busmode,
95 Opt_listuid, Opt_listgid, Opt_listmode,
96 Opt_err,
97};
98
99static const match_table_t tokens = {
100 {Opt_devuid, "devuid=%u"},
101 {Opt_devgid, "devgid=%u"},
102 {Opt_devmode, "devmode=%o"},
103 {Opt_busuid, "busuid=%u"},
104 {Opt_busgid, "busgid=%u"},
105 {Opt_busmode, "busmode=%o"},
106 {Opt_listuid, "listuid=%u"},
107 {Opt_listgid, "listgid=%u"},
108 {Opt_listmode, "listmode=%o"},
109 {Opt_err, NULL}
110};
111
112static int parse_options(struct super_block *s, char *data)
113{
114 char *p;
115 int option;
116
117
118 devuid = 0;
119 busuid = 0;
120 listuid = 0;
121 devgid = 0;
122 busgid = 0;
123 listgid = 0;
124 devmode = USBFS_DEFAULT_DEVMODE;
125 busmode = USBFS_DEFAULT_BUSMODE;
126 listmode = USBFS_DEFAULT_LISTMODE;
127
128 while ((p = strsep(&data, ",")) != NULL) {
129 substring_t args[MAX_OPT_ARGS];
130 int token;
131 if (!*p)
132 continue;
133
134 token = match_token(p, tokens, args);
135 switch (token) {
136 case Opt_devuid:
137 if (match_int(&args[0], &option))
138 return -EINVAL;
139 devuid = option;
140 break;
141 case Opt_devgid:
142 if (match_int(&args[0], &option))
143 return -EINVAL;
144 devgid = option;
145 break;
146 case Opt_devmode:
147 if (match_octal(&args[0], &option))
148 return -EINVAL;
149 devmode = option & S_IRWXUGO;
150 break;
151 case Opt_busuid:
152 if (match_int(&args[0], &option))
153 return -EINVAL;
154 busuid = option;
155 break;
156 case Opt_busgid:
157 if (match_int(&args[0], &option))
158 return -EINVAL;
159 busgid = option;
160 break;
161 case Opt_busmode:
162 if (match_octal(&args[0], &option))
163 return -EINVAL;
164 busmode = option & S_IRWXUGO;
165 break;
166 case Opt_listuid:
167 if (match_int(&args[0], &option))
168 return -EINVAL;
169 listuid = option;
170 break;
171 case Opt_listgid:
172 if (match_int(&args[0], &option))
173 return -EINVAL;
174 listgid = option;
175 break;
176 case Opt_listmode:
177 if (match_octal(&args[0], &option))
178 return -EINVAL;
179 listmode = option & S_IRWXUGO;
180 break;
181 default:
182 printk(KERN_ERR "usbfs: unrecognised mount option "
183 "\"%s\" or missing value\n", p);
184 return -EINVAL;
185 }
186 }
187
188 return 0;
189}
190
191static void update_special(struct dentry *special)
192{
193 special->d_inode->i_uid = listuid;
194 special->d_inode->i_gid = listgid;
195 special->d_inode->i_mode = S_IFREG | listmode;
196}
197
198static void update_dev(struct dentry *dev)
199{
200 dev->d_inode->i_uid = devuid;
201 dev->d_inode->i_gid = devgid;
202 dev->d_inode->i_mode = S_IFREG | devmode;
203}
204
205static void update_bus(struct dentry *bus)
206{
207 struct dentry *dev = NULL;
208
209 bus->d_inode->i_uid = busuid;
210 bus->d_inode->i_gid = busgid;
211 bus->d_inode->i_mode = S_IFDIR | busmode;
212
213 mutex_lock(&bus->d_inode->i_mutex);
214
215 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
216 if (dev->d_inode)
217 update_dev(dev);
218
219 mutex_unlock(&bus->d_inode->i_mutex);
220}
221
222static void update_sb(struct super_block *sb)
223{
224 struct dentry *root = sb->s_root;
225 struct dentry *bus = NULL;
226
227 if (!root)
228 return;
229
230 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
231
232 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
233 if (bus->d_inode) {
234 switch (S_IFMT & bus->d_inode->i_mode) {
235 case S_IFDIR:
236 update_bus(bus);
237 break;
238 case S_IFREG:
239 update_special(bus);
240 break;
241 default:
242 printk(KERN_WARNING "usbfs: Unknown node %s "
243 "mode %x found on remount!\n",
244 bus->d_name.name, bus->d_inode->i_mode);
245 break;
246 }
247 }
248 }
249
250 mutex_unlock(&root->d_inode->i_mutex);
251}
252
253static int remount(struct super_block *sb, int *flags, char *data)
254{
255
256
257
258
259 if (ignore_mount)
260 return 0;
261
262 if (parse_options(sb, data)) {
263 printk(KERN_WARNING "usbfs: mount parameter error.\n");
264 return -EINVAL;
265 }
266
267 if (usbfs_mount && usbfs_mount->mnt_sb)
268 update_sb(usbfs_mount->mnt_sb);
269
270 return 0;
271}
272
273static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
274{
275 struct inode *inode = new_inode(sb);
276
277 if (inode) {
278 inode->i_ino = get_next_ino();
279 inode->i_mode = mode;
280 inode->i_uid = current_fsuid();
281 inode->i_gid = current_fsgid();
282 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:
291 inode->i_op = &simple_dir_inode_operations;
292 inode->i_fop = &simple_dir_operations;
293
294
295 inc_nlink(inode);
296 break;
297 }
298 }
299 return inode;
300}
301
302
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)
327 inc_nlink(dir);
328 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(&dentry->d_lock);
347 list_for_each(list, &dentry->d_subdirs) {
348 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
349
350 spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
351 if (usbfs_positive(de)) {
352 spin_unlock(&de->d_lock);
353 spin_unlock(&dentry->d_lock);
354 return 0;
355 }
356 spin_unlock(&de->d_lock);
357 }
358 spin_unlock(&dentry->d_lock);
359 return 1;
360}
361
362static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
363{
364 struct inode *inode = dentry->d_inode;
365 mutex_lock(&inode->i_mutex);
366 drop_nlink(dentry->d_inode);
367 dput(dentry);
368 mutex_unlock(&inode->i_mutex);
369 d_delete(dentry);
370 return 0;
371}
372
373static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
374{
375 int error = -ENOTEMPTY;
376 struct inode * inode = dentry->d_inode;
377
378 mutex_lock(&inode->i_mutex);
379 dentry_unhash(dentry);
380 if (usbfs_empty(dentry)) {
381 dont_mount(dentry);
382 drop_nlink(dentry->d_inode);
383 drop_nlink(dentry->d_inode);
384 dput(dentry);
385 inode->i_flags |= S_DEAD;
386 drop_nlink(dir);
387 error = 0;
388 }
389 mutex_unlock(&inode->i_mutex);
390 if (!error)
391 d_delete(dentry);
392 dput(dentry);
393 return error;
394}
395
396
397
398static ssize_t default_read_file (struct file *file, char __user *buf,
399 size_t count, loff_t *ppos)
400{
401 return 0;
402}
403
404static ssize_t default_write_file (struct file *file, const char __user *buf,
405 size_t count, loff_t *ppos)
406{
407 return count;
408}
409
410static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
411{
412 loff_t retval = -EINVAL;
413
414 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
415 switch(orig) {
416 case 0:
417 if (offset > 0) {
418 file->f_pos = offset;
419 retval = file->f_pos;
420 }
421 break;
422 case 1:
423 if ((offset + file->f_pos) > 0) {
424 file->f_pos += offset;
425 retval = file->f_pos;
426 }
427 break;
428 default:
429 break;
430 }
431 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
432 return retval;
433}
434
435static int default_open (struct inode *inode, struct file *file)
436{
437 if (inode->i_private)
438 file->private_data = inode->i_private;
439
440 return 0;
441}
442
443static const struct file_operations default_file_operations = {
444 .read = default_read_file,
445 .write = default_write_file,
446 .open = default_open,
447 .llseek = default_file_lseek,
448};
449
450static const struct super_operations usbfs_ops = {
451 .statfs = simple_statfs,
452 .drop_inode = generic_delete_inode,
453 .remount_fs = remount,
454 .show_options = usbfs_show_options,
455};
456
457static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
458{
459 struct inode *inode;
460 struct dentry *root;
461
462 sb->s_blocksize = PAGE_CACHE_SIZE;
463 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
464 sb->s_magic = USBDEVICE_SUPER_MAGIC;
465 sb->s_op = &usbfs_ops;
466 sb->s_time_gran = 1;
467 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
468
469 if (!inode) {
470 dbg("%s: could not get inode!",__func__);
471 return -ENOMEM;
472 }
473
474 root = d_alloc_root(inode);
475 if (!root) {
476 dbg("%s: could not get root dentry!",__func__);
477 iput(inode);
478 return -ENOMEM;
479 }
480 sb->s_root = root;
481 return 0;
482}
483
484
485
486
487
488
489
490
491
492
493static int fs_create_by_name (const char *name, mode_t mode,
494 struct dentry *parent, struct dentry **dentry)
495{
496 int error = 0;
497
498
499
500
501
502
503 if (!parent ) {
504 if (usbfs_mount && usbfs_mount->mnt_sb) {
505 parent = usbfs_mount->mnt_sb->s_root;
506 }
507 }
508
509 if (!parent) {
510 dbg("Ah! can not find a parent!");
511 return -EFAULT;
512 }
513
514 *dentry = NULL;
515 mutex_lock(&parent->d_inode->i_mutex);
516 *dentry = lookup_one_len(name, parent, strlen(name));
517 if (!IS_ERR(*dentry)) {
518 if ((mode & S_IFMT) == S_IFDIR)
519 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
520 else
521 error = usbfs_create (parent->d_inode, *dentry, mode);
522 } else
523 error = PTR_ERR(*dentry);
524 mutex_unlock(&parent->d_inode->i_mutex);
525
526 return error;
527}
528
529static struct dentry *fs_create_file (const char *name, mode_t mode,
530 struct dentry *parent, void *data,
531 const struct file_operations *fops,
532 uid_t uid, gid_t gid)
533{
534 struct dentry *dentry;
535 int error;
536
537 dbg("creating file '%s'",name);
538
539 error = fs_create_by_name (name, mode, parent, &dentry);
540 if (error) {
541 dentry = NULL;
542 } else {
543 if (dentry->d_inode) {
544 if (data)
545 dentry->d_inode->i_private = data;
546 if (fops)
547 dentry->d_inode->i_fop = fops;
548 dentry->d_inode->i_uid = uid;
549 dentry->d_inode->i_gid = gid;
550 }
551 }
552
553 return dentry;
554}
555
556static void fs_remove_file (struct dentry *dentry)
557{
558 struct dentry *parent = dentry->d_parent;
559
560 if (!parent || !parent->d_inode)
561 return;
562
563 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
564 if (usbfs_positive(dentry)) {
565 if (dentry->d_inode) {
566 if (S_ISDIR(dentry->d_inode->i_mode))
567 usbfs_rmdir(parent->d_inode, dentry);
568 else
569 usbfs_unlink(parent->d_inode, dentry);
570 dput(dentry);
571 }
572 }
573 mutex_unlock(&parent->d_inode->i_mutex);
574}
575
576
577
578static struct dentry *usb_mount(struct file_system_type *fs_type,
579 int flags, const char *dev_name, void *data)
580{
581 return mount_single(fs_type, flags, data, usbfs_fill_super);
582}
583
584static struct file_system_type usb_fs_type = {
585 .owner = THIS_MODULE,
586 .name = "usbfs",
587 .mount = usb_mount,
588 .kill_sb = kill_litter_super,
589};
590
591
592
593static int create_special_files (void)
594{
595 struct dentry *parent;
596 int retval;
597
598
599
600
601 ignore_mount = 1;
602
603
604 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
605 if (retval) {
606 printk(KERN_ERR "Unable to get usbfs mount\n");
607 goto exit;
608 }
609
610 ignore_mount = 0;
611
612 parent = usbfs_mount->mnt_sb->s_root;
613 devices_usbfs_dentry = fs_create_file ("devices",
614 listmode | S_IFREG, parent,
615 NULL, &usbfs_devices_fops,
616 listuid, listgid);
617 if (devices_usbfs_dentry == NULL) {
618 printk(KERN_ERR "Unable to create devices usbfs file\n");
619 retval = -ENODEV;
620 goto error_clean_mounts;
621 }
622
623 goto exit;
624
625error_clean_mounts:
626 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
627exit:
628 return retval;
629}
630
631static void remove_special_files (void)
632{
633 if (devices_usbfs_dentry)
634 fs_remove_file (devices_usbfs_dentry);
635 devices_usbfs_dentry = NULL;
636 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
637}
638
639void usbfs_update_special (void)
640{
641 struct inode *inode;
642
643 if (devices_usbfs_dentry) {
644 inode = devices_usbfs_dentry->d_inode;
645 if (inode)
646 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
647 }
648}
649
650static void usbfs_add_bus(struct usb_bus *bus)
651{
652 struct dentry *parent;
653 char name[8];
654 int retval;
655
656
657 if (num_buses == 0) {
658 retval = create_special_files();
659 if (retval)
660 return;
661 }
662 ++num_buses;
663
664 sprintf (name, "%03d", bus->busnum);
665
666 parent = usbfs_mount->mnt_sb->s_root;
667 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
668 bus, NULL, busuid, busgid);
669 if (bus->usbfs_dentry == NULL) {
670 printk(KERN_ERR "Error creating usbfs bus entry\n");
671 return;
672 }
673}
674
675static void usbfs_remove_bus(struct usb_bus *bus)
676{
677 if (bus->usbfs_dentry) {
678 fs_remove_file (bus->usbfs_dentry);
679 bus->usbfs_dentry = NULL;
680 }
681
682 --num_buses;
683 if (num_buses <= 0) {
684 remove_special_files();
685 num_buses = 0;
686 }
687}
688
689static void usbfs_add_device(struct usb_device *dev)
690{
691 char name[8];
692 int i;
693 int i_size;
694
695 sprintf (name, "%03d", dev->devnum);
696 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
697 dev->bus->usbfs_dentry, dev,
698 &usbdev_file_operations,
699 devuid, devgid);
700 if (dev->usbfs_dentry == NULL) {
701 printk(KERN_ERR "Error creating usbfs device entry\n");
702 return;
703 }
704
705
706
707 i_size = sizeof (struct usb_device_descriptor);
708 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
709 struct usb_config_descriptor *config =
710 (struct usb_config_descriptor *)dev->rawdescriptors[i];
711 i_size += le16_to_cpu(config->wTotalLength);
712 }
713 if (dev->usbfs_dentry->d_inode)
714 dev->usbfs_dentry->d_inode->i_size = i_size;
715}
716
717static void usbfs_remove_device(struct usb_device *dev)
718{
719 if (dev->usbfs_dentry) {
720 fs_remove_file (dev->usbfs_dentry);
721 dev->usbfs_dentry = NULL;
722 }
723}
724
725static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
726{
727 switch (action) {
728 case USB_DEVICE_ADD:
729 usbfs_add_device(dev);
730 break;
731 case USB_DEVICE_REMOVE:
732 usbfs_remove_device(dev);
733 break;
734 case USB_BUS_ADD:
735 usbfs_add_bus(dev);
736 break;
737 case USB_BUS_REMOVE:
738 usbfs_remove_bus(dev);
739 }
740
741 usbfs_update_special();
742 usbfs_conn_disc_event();
743 return NOTIFY_OK;
744}
745
746static struct notifier_block usbfs_nb = {
747 .notifier_call = usbfs_notify,
748};
749
750
751
752static struct proc_dir_entry *usbdir = NULL;
753
754int __init usbfs_init(void)
755{
756 int retval;
757
758 retval = register_filesystem(&usb_fs_type);
759 if (retval)
760 return retval;
761
762 usb_register_notify(&usbfs_nb);
763
764
765 usbdir = proc_mkdir("bus/usb", NULL);
766
767 return 0;
768}
769
770void usbfs_cleanup(void)
771{
772 usb_unregister_notify(&usbfs_nb);
773 unregister_filesystem(&usb_fs_type);
774 if (usbdir)
775 remove_proc_entry("bus/usb", NULL);
776}
777
778