1 /*****************************************************************************/
4 * inode.c -- Inode/Dentry functions for the USB device file system.
6 * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7 * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
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.
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.
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.
24 * 0.1 04.01.2000 Created
25 * 0.2 10.12.2001 converted to use the vfs layer better
28 /*****************************************************************************/
30 #include <linux/module.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>
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
50 static const struct file_operations default_file_operations
;
51 static struct vfsmount
*usbfs_mount
;
52 static int usbfs_mount_count
; /* = 0 */
53 static int ignore_mount
= 0;
55 static struct dentry
*devices_usbfs_dentry
;
56 static int num_buses
; /* = 0 */
58 static uid_t devuid
; /* = 0 */
59 static uid_t busuid
; /* = 0 */
60 static uid_t listuid
; /* = 0 */
61 static gid_t devgid
; /* = 0 */
62 static gid_t busgid
; /* = 0 */
63 static gid_t listgid
; /* = 0 */
64 static umode_t devmode
= USBFS_DEFAULT_DEVMODE
;
65 static umode_t busmode
= USBFS_DEFAULT_BUSMODE
;
66 static umode_t listmode
= USBFS_DEFAULT_LISTMODE
;
68 static int usbfs_show_options(struct seq_file
*seq
, struct vfsmount
*mnt
)
71 seq_printf(seq
, ",devuid=%u", devuid
);
73 seq_printf(seq
, ",devgid=%u", devgid
);
74 if (devmode
!= USBFS_DEFAULT_DEVMODE
)
75 seq_printf(seq
, ",devmode=%o", devmode
);
77 seq_printf(seq
, ",busuid=%u", busuid
);
79 seq_printf(seq
, ",busgid=%u", busgid
);
80 if (busmode
!= USBFS_DEFAULT_BUSMODE
)
81 seq_printf(seq
, ",busmode=%o", busmode
);
83 seq_printf(seq
, ",listuid=%u", listuid
);
85 seq_printf(seq
, ",listgid=%u", listgid
);
86 if (listmode
!= USBFS_DEFAULT_LISTMODE
)
87 seq_printf(seq
, ",listmode=%o", listmode
);
93 Opt_devuid
, Opt_devgid
, Opt_devmode
,
94 Opt_busuid
, Opt_busgid
, Opt_busmode
,
95 Opt_listuid
, Opt_listgid
, Opt_listmode
,
99 static 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"},
112 static int parse_options(struct super_block
*s
, char *data
)
117 /* (re)set to defaults. */
124 devmode
= USBFS_DEFAULT_DEVMODE
;
125 busmode
= USBFS_DEFAULT_BUSMODE
;
126 listmode
= USBFS_DEFAULT_LISTMODE
;
128 while ((p
= strsep(&data
, ",")) != NULL
) {
129 substring_t args
[MAX_OPT_ARGS
];
134 token
= match_token(p
, tokens
, args
);
137 if (match_int(&args
[0], &option
))
142 if (match_int(&args
[0], &option
))
147 if (match_octal(&args
[0], &option
))
149 devmode
= option
& S_IRWXUGO
;
152 if (match_int(&args
[0], &option
))
157 if (match_int(&args
[0], &option
))
162 if (match_octal(&args
[0], &option
))
164 busmode
= option
& S_IRWXUGO
;
167 if (match_int(&args
[0], &option
))
172 if (match_int(&args
[0], &option
))
177 if (match_octal(&args
[0], &option
))
179 listmode
= option
& S_IRWXUGO
;
182 printk(KERN_ERR
"usbfs: unrecognised mount option "
183 "\"%s\" or missing value\n", p
);
191 static void update_special(struct dentry
*special
)
193 special
->d_inode
->i_uid
= listuid
;
194 special
->d_inode
->i_gid
= listgid
;
195 special
->d_inode
->i_mode
= S_IFREG
| listmode
;
198 static void update_dev(struct dentry
*dev
)
200 dev
->d_inode
->i_uid
= devuid
;
201 dev
->d_inode
->i_gid
= devgid
;
202 dev
->d_inode
->i_mode
= S_IFREG
| devmode
;
205 static void update_bus(struct dentry
*bus
)
207 struct dentry
*dev
= NULL
;
209 bus
->d_inode
->i_uid
= busuid
;
210 bus
->d_inode
->i_gid
= busgid
;
211 bus
->d_inode
->i_mode
= S_IFDIR
| busmode
;
213 mutex_lock(&bus
->d_inode
->i_mutex
);
215 list_for_each_entry(dev
, &bus
->d_subdirs
, d_u
.d_child
)
219 mutex_unlock(&bus
->d_inode
->i_mutex
);
222 static void update_sb(struct super_block
*sb
)
224 struct dentry
*root
= sb
->s_root
;
225 struct dentry
*bus
= NULL
;
230 mutex_lock_nested(&root
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
232 list_for_each_entry(bus
, &root
->d_subdirs
, d_u
.d_child
) {
234 switch (S_IFMT
& bus
->d_inode
->i_mode
) {
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
);
250 mutex_unlock(&root
->d_inode
->i_mutex
);
253 static int remount(struct super_block
*sb
, int *flags
, char *data
)
255 /* If this is not a real mount,
256 * i.e. it's a simple_pin_fs from create_special_files,
262 if (parse_options(sb
, data
)) {
263 printk(KERN_WARNING
"usbfs: mount parameter error.\n");
267 if (usbfs_mount
&& usbfs_mount
->mnt_sb
)
268 update_sb(usbfs_mount
->mnt_sb
);
273 static struct inode
*usbfs_get_inode (struct super_block
*sb
, int mode
, dev_t dev
)
275 struct inode
*inode
= new_inode(sb
);
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
) {
285 init_special_inode(inode
, mode
, dev
);
288 inode
->i_fop
= &default_file_operations
;
291 inode
->i_op
= &simple_dir_inode_operations
;
292 inode
->i_fop
= &simple_dir_operations
;
294 /* directory inodes start off with i_nlink == 2 (for "." entry) */
303 static int usbfs_mknod (struct inode
*dir
, struct dentry
*dentry
, int mode
,
306 struct inode
*inode
= usbfs_get_inode(dir
->i_sb
, mode
, dev
);
313 d_instantiate(dentry
, inode
);
320 static int usbfs_mkdir (struct inode
*dir
, struct dentry
*dentry
, int mode
)
324 mode
= (mode
& (S_IRWXUGO
| S_ISVTX
)) | S_IFDIR
;
325 res
= usbfs_mknod (dir
, dentry
, mode
, 0);
331 static int usbfs_create (struct inode
*dir
, struct dentry
*dentry
, int mode
)
333 mode
= (mode
& S_IALLUGO
) | S_IFREG
;
334 return usbfs_mknod (dir
, dentry
, mode
, 0);
337 static inline int usbfs_positive (struct dentry
*dentry
)
339 return dentry
->d_inode
&& !d_unhashed(dentry
);
342 static int usbfs_empty (struct dentry
*dentry
)
344 struct list_head
*list
;
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
);
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
);
356 spin_unlock(&de
->d_lock
);
358 spin_unlock(&dentry
->d_lock
);
362 static int usbfs_unlink (struct inode
*dir
, struct dentry
*dentry
)
364 struct inode
*inode
= dentry
->d_inode
;
365 mutex_lock(&inode
->i_mutex
);
366 drop_nlink(dentry
->d_inode
);
368 mutex_unlock(&inode
->i_mutex
);
373 static int usbfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
375 int error
= -ENOTEMPTY
;
376 struct inode
* inode
= dentry
->d_inode
;
378 mutex_lock(&inode
->i_mutex
);
379 dentry_unhash(dentry
);
380 if (usbfs_empty(dentry
)) {
382 drop_nlink(dentry
->d_inode
);
383 drop_nlink(dentry
->d_inode
);
385 inode
->i_flags
|= S_DEAD
;
389 mutex_unlock(&inode
->i_mutex
);
397 /* default file operations */
398 static ssize_t
default_read_file (struct file
*file
, char __user
*buf
,
399 size_t count
, loff_t
*ppos
)
404 static ssize_t
default_write_file (struct file
*file
, const char __user
*buf
,
405 size_t count
, loff_t
*ppos
)
410 static loff_t
default_file_lseek (struct file
*file
, loff_t offset
, int orig
)
412 loff_t retval
= -EINVAL
;
414 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
418 file
->f_pos
= offset
;
419 retval
= file
->f_pos
;
423 if ((offset
+ file
->f_pos
) > 0) {
424 file
->f_pos
+= offset
;
425 retval
= file
->f_pos
;
431 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
435 static int default_open (struct inode
*inode
, struct file
*file
)
437 if (inode
->i_private
)
438 file
->private_data
= inode
->i_private
;
443 static 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
,
450 static 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
,
457 static int usbfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
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
;
467 inode
= usbfs_get_inode(sb
, S_IFDIR
| 0755, 0);
470 dbg("%s: could not get inode!",__func__
);
474 root
= d_alloc_root(inode
);
476 dbg("%s: could not get root dentry!",__func__
);
485 * fs_create_by_name - create a file, given a name
486 * @name: name of file
487 * @mode: type of file
488 * @parent: dentry of directory to create it in
489 * @dentry: resulting dentry of file
491 * This function handles both regular files and directories.
493 static int fs_create_by_name (const char *name
, mode_t mode
,
494 struct dentry
*parent
, struct dentry
**dentry
)
498 /* If the parent is not specified, we create it in the root.
499 * We need the root dentry to do this, which is in the super
500 * block. A pointer to that is in the struct vfsmount that we
504 if (usbfs_mount
&& usbfs_mount
->mnt_sb
) {
505 parent
= usbfs_mount
->mnt_sb
->s_root
;
510 dbg("Ah! can not find a parent!");
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
);
521 error
= usbfs_create (parent
->d_inode
, *dentry
, mode
);
523 error
= PTR_ERR(*dentry
);
524 mutex_unlock(&parent
->d_inode
->i_mutex
);
529 static 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
)
534 struct dentry
*dentry
;
537 dbg("creating file '%s'",name
);
539 error
= fs_create_by_name (name
, mode
, parent
, &dentry
);
543 if (dentry
->d_inode
) {
545 dentry
->d_inode
->i_private
= data
;
547 dentry
->d_inode
->i_fop
= fops
;
548 dentry
->d_inode
->i_uid
= uid
;
549 dentry
->d_inode
->i_gid
= gid
;
556 static void fs_remove_file (struct dentry
*dentry
)
558 struct dentry
*parent
= dentry
->d_parent
;
560 if (!parent
|| !parent
->d_inode
)
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
);
569 usbfs_unlink(parent
->d_inode
, dentry
);
573 mutex_unlock(&parent
->d_inode
->i_mutex
);
576 /* --------------------------------------------------------------------- */
578 static struct dentry
*usb_mount(struct file_system_type
*fs_type
,
579 int flags
, const char *dev_name
, void *data
)
581 return mount_single(fs_type
, flags
, data
, usbfs_fill_super
);
584 static struct file_system_type usb_fs_type
= {
585 .owner
= THIS_MODULE
,
588 .kill_sb
= kill_litter_super
,
591 /* --------------------------------------------------------------------- */
593 static int create_special_files (void)
595 struct dentry
*parent
;
598 /* the simple_pin_fs calls will call remount with no options
599 * without this flag that would overwrite the real mount options (if any)
603 /* create the devices special file */
604 retval
= simple_pin_fs(&usb_fs_type
, &usbfs_mount
, &usbfs_mount_count
);
606 printk(KERN_ERR
"Unable to get usbfs mount\n");
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
,
617 if (devices_usbfs_dentry
== NULL
) {
618 printk(KERN_ERR
"Unable to create devices usbfs file\n");
620 goto error_clean_mounts
;
626 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
631 static void remove_special_files (void)
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
);
639 void usbfs_update_special (void)
643 if (devices_usbfs_dentry
) {
644 inode
= devices_usbfs_dentry
->d_inode
;
646 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
650 static void usbfs_add_bus(struct usb_bus
*bus
)
652 struct dentry
*parent
;
656 /* create the special files if this is the first bus added */
657 if (num_buses
== 0) {
658 retval
= create_special_files();
664 sprintf (name
, "%03d", bus
->busnum
);
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");
675 static void usbfs_remove_bus(struct usb_bus
*bus
)
677 if (bus
->usbfs_dentry
) {
678 fs_remove_file (bus
->usbfs_dentry
);
679 bus
->usbfs_dentry
= NULL
;
683 if (num_buses
<= 0) {
684 remove_special_files();
689 static void usbfs_add_device(struct usb_device
*dev
)
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
,
700 if (dev
->usbfs_dentry
== NULL
) {
701 printk(KERN_ERR
"Error creating usbfs device entry\n");
705 /* Set the size of the device's file to be
706 * equal to the size of the device descriptors. */
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
);
713 if (dev
->usbfs_dentry
->d_inode
)
714 dev
->usbfs_dentry
->d_inode
->i_size
= i_size
;
717 static void usbfs_remove_device(struct usb_device
*dev
)
719 if (dev
->usbfs_dentry
) {
720 fs_remove_file (dev
->usbfs_dentry
);
721 dev
->usbfs_dentry
= NULL
;
725 static int usbfs_notify(struct notifier_block
*self
, unsigned long action
, void *dev
)
729 usbfs_add_device(dev
);
731 case USB_DEVICE_REMOVE
:
732 usbfs_remove_device(dev
);
738 usbfs_remove_bus(dev
);
741 usbfs_update_special();
742 usbfs_conn_disc_event();
746 static struct notifier_block usbfs_nb
= {
747 .notifier_call
= usbfs_notify
,
750 /* --------------------------------------------------------------------- */
752 static struct proc_dir_entry
*usbdir
= NULL
;
754 int __init
usbfs_init(void)
758 retval
= register_filesystem(&usb_fs_type
);
762 usb_register_notify(&usbfs_nb
);
764 /* create mount point for usbfs */
765 usbdir
= proc_mkdir("bus/usb", NULL
);
770 void usbfs_cleanup(void)
772 usb_unregister_notify(&usbfs_nb
);
773 unregister_filesystem(&usb_fs_type
);
775 remove_proc_entry("bus/usb", NULL
);