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 <asm/byteorder.h>
45 static struct super_operations usbfs_ops
;
46 static const struct file_operations default_file_operations
;
47 static struct vfsmount
*usbfs_mount
;
48 static int usbfs_mount_count
; /* = 0 */
49 static int ignore_mount
= 0;
51 static struct dentry
*devices_usbfs_dentry
;
52 static int num_buses
; /* = 0 */
54 static uid_t devuid
; /* = 0 */
55 static uid_t busuid
; /* = 0 */
56 static uid_t listuid
; /* = 0 */
57 static gid_t devgid
; /* = 0 */
58 static gid_t busgid
; /* = 0 */
59 static gid_t listgid
; /* = 0 */
60 static umode_t devmode
= S_IWUSR
| S_IRUGO
;
61 static umode_t busmode
= S_IXUGO
| S_IRUGO
;
62 static umode_t listmode
= S_IRUGO
;
65 Opt_devuid
, Opt_devgid
, Opt_devmode
,
66 Opt_busuid
, Opt_busgid
, Opt_busmode
,
67 Opt_listuid
, Opt_listgid
, Opt_listmode
,
71 static match_table_t tokens
= {
72 {Opt_devuid
, "devuid=%u"},
73 {Opt_devgid
, "devgid=%u"},
74 {Opt_devmode
, "devmode=%o"},
75 {Opt_busuid
, "busuid=%u"},
76 {Opt_busgid
, "busgid=%u"},
77 {Opt_busmode
, "busmode=%o"},
78 {Opt_listuid
, "listuid=%u"},
79 {Opt_listgid
, "listgid=%u"},
80 {Opt_listmode
, "listmode=%o"},
84 static int parse_options(struct super_block
*s
, char *data
)
89 /* (re)set to defaults. */
96 devmode
= S_IWUSR
| S_IRUGO
;
97 busmode
= S_IXUGO
| S_IRUGO
;
100 while ((p
= strsep(&data
, ",")) != NULL
) {
101 substring_t args
[MAX_OPT_ARGS
];
106 token
= match_token(p
, tokens
, args
);
109 if (match_int(&args
[0], &option
))
114 if (match_int(&args
[0], &option
))
119 if (match_octal(&args
[0], &option
))
121 devmode
= option
& S_IRWXUGO
;
124 if (match_int(&args
[0], &option
))
129 if (match_int(&args
[0], &option
))
134 if (match_octal(&args
[0], &option
))
136 busmode
= option
& S_IRWXUGO
;
139 if (match_int(&args
[0], &option
))
144 if (match_int(&args
[0], &option
))
149 if (match_octal(&args
[0], &option
))
151 listmode
= option
& S_IRWXUGO
;
154 err("usbfs: unrecognised mount option \"%s\" "
155 "or missing value\n", p
);
163 static void update_special(struct dentry
*special
)
165 special
->d_inode
->i_uid
= listuid
;
166 special
->d_inode
->i_gid
= listgid
;
167 special
->d_inode
->i_mode
= S_IFREG
| listmode
;
170 static void update_dev(struct dentry
*dev
)
172 dev
->d_inode
->i_uid
= devuid
;
173 dev
->d_inode
->i_gid
= devgid
;
174 dev
->d_inode
->i_mode
= S_IFREG
| devmode
;
177 static void update_bus(struct dentry
*bus
)
179 struct dentry
*dev
= NULL
;
181 bus
->d_inode
->i_uid
= busuid
;
182 bus
->d_inode
->i_gid
= busgid
;
183 bus
->d_inode
->i_mode
= S_IFDIR
| busmode
;
185 mutex_lock(&bus
->d_inode
->i_mutex
);
187 list_for_each_entry(dev
, &bus
->d_subdirs
, d_u
.d_child
)
191 mutex_unlock(&bus
->d_inode
->i_mutex
);
194 static void update_sb(struct super_block
*sb
)
196 struct dentry
*root
= sb
->s_root
;
197 struct dentry
*bus
= NULL
;
202 mutex_lock_nested(&root
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
204 list_for_each_entry(bus
, &root
->d_subdirs
, d_u
.d_child
) {
206 switch (S_IFMT
& bus
->d_inode
->i_mode
) {
214 warn("Unknown node %s mode %x found on remount!\n",bus
->d_name
.name
,bus
->d_inode
->i_mode
);
220 mutex_unlock(&root
->d_inode
->i_mutex
);
223 static int remount(struct super_block
*sb
, int *flags
, char *data
)
225 /* If this is not a real mount,
226 * i.e. it's a simple_pin_fs from create_special_files,
232 if (parse_options(sb
, data
)) {
233 warn("usbfs: mount parameter error:");
237 if (usbfs_mount
&& usbfs_mount
->mnt_sb
)
238 update_sb(usbfs_mount
->mnt_sb
);
243 static struct inode
*usbfs_get_inode (struct super_block
*sb
, int mode
, dev_t dev
)
245 struct inode
*inode
= new_inode(sb
);
248 inode
->i_mode
= mode
;
249 inode
->i_uid
= current
->fsuid
;
250 inode
->i_gid
= current
->fsgid
;
252 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
253 switch (mode
& S_IFMT
) {
255 init_special_inode(inode
, mode
, dev
);
258 inode
->i_fop
= &default_file_operations
;
261 inode
->i_op
= &simple_dir_inode_operations
;
262 inode
->i_fop
= &simple_dir_operations
;
264 /* directory inodes start off with i_nlink == 2 (for "." entry) */
273 static int usbfs_mknod (struct inode
*dir
, struct dentry
*dentry
, int mode
,
276 struct inode
*inode
= usbfs_get_inode(dir
->i_sb
, mode
, dev
);
283 d_instantiate(dentry
, inode
);
290 static int usbfs_mkdir (struct inode
*dir
, struct dentry
*dentry
, int mode
)
294 mode
= (mode
& (S_IRWXUGO
| S_ISVTX
)) | S_IFDIR
;
295 res
= usbfs_mknod (dir
, dentry
, mode
, 0);
301 static int usbfs_create (struct inode
*dir
, struct dentry
*dentry
, int mode
)
303 mode
= (mode
& S_IALLUGO
) | S_IFREG
;
304 return usbfs_mknod (dir
, dentry
, mode
, 0);
307 static inline int usbfs_positive (struct dentry
*dentry
)
309 return dentry
->d_inode
&& !d_unhashed(dentry
);
312 static int usbfs_empty (struct dentry
*dentry
)
314 struct list_head
*list
;
316 spin_lock(&dcache_lock
);
318 list_for_each(list
, &dentry
->d_subdirs
) {
319 struct dentry
*de
= list_entry(list
, struct dentry
, d_u
.d_child
);
320 if (usbfs_positive(de
)) {
321 spin_unlock(&dcache_lock
);
326 spin_unlock(&dcache_lock
);
330 static int usbfs_unlink (struct inode
*dir
, struct dentry
*dentry
)
332 struct inode
*inode
= dentry
->d_inode
;
333 mutex_lock(&inode
->i_mutex
);
334 drop_nlink(dentry
->d_inode
);
336 mutex_unlock(&inode
->i_mutex
);
341 static int usbfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
343 int error
= -ENOTEMPTY
;
344 struct inode
* inode
= dentry
->d_inode
;
346 mutex_lock(&inode
->i_mutex
);
347 dentry_unhash(dentry
);
348 if (usbfs_empty(dentry
)) {
349 drop_nlink(dentry
->d_inode
);
350 drop_nlink(dentry
->d_inode
);
352 inode
->i_flags
|= S_DEAD
;
356 mutex_unlock(&inode
->i_mutex
);
364 /* default file operations */
365 static ssize_t
default_read_file (struct file
*file
, char __user
*buf
,
366 size_t count
, loff_t
*ppos
)
371 static ssize_t
default_write_file (struct file
*file
, const char __user
*buf
,
372 size_t count
, loff_t
*ppos
)
377 static loff_t
default_file_lseek (struct file
*file
, loff_t offset
, int orig
)
379 loff_t retval
= -EINVAL
;
381 mutex_lock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
385 file
->f_pos
= offset
;
386 retval
= file
->f_pos
;
390 if ((offset
+ file
->f_pos
) > 0) {
391 file
->f_pos
+= offset
;
392 retval
= file
->f_pos
;
398 mutex_unlock(&file
->f_path
.dentry
->d_inode
->i_mutex
);
402 static int default_open (struct inode
*inode
, struct file
*file
)
404 if (inode
->i_private
)
405 file
->private_data
= inode
->i_private
;
410 static const struct file_operations default_file_operations
= {
411 .read
= default_read_file
,
412 .write
= default_write_file
,
413 .open
= default_open
,
414 .llseek
= default_file_lseek
,
417 static struct super_operations usbfs_ops
= {
418 .statfs
= simple_statfs
,
419 .drop_inode
= generic_delete_inode
,
420 .remount_fs
= remount
,
423 static int usbfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
428 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
429 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
430 sb
->s_magic
= USBDEVICE_SUPER_MAGIC
;
431 sb
->s_op
= &usbfs_ops
;
433 inode
= usbfs_get_inode(sb
, S_IFDIR
| 0755, 0);
436 dbg("%s: could not get inode!",__FUNCTION__
);
440 root
= d_alloc_root(inode
);
442 dbg("%s: could not get root dentry!",__FUNCTION__
);
451 * fs_create_by_name - create a file, given a name
452 * @name: name of file
453 * @mode: type of file
454 * @parent: dentry of directory to create it in
455 * @dentry: resulting dentry of file
457 * This function handles both regular files and directories.
459 static int fs_create_by_name (const char *name
, mode_t mode
,
460 struct dentry
*parent
, struct dentry
**dentry
)
464 /* If the parent is not specified, we create it in the root.
465 * We need the root dentry to do this, which is in the super
466 * block. A pointer to that is in the struct vfsmount that we
470 if (usbfs_mount
&& usbfs_mount
->mnt_sb
) {
471 parent
= usbfs_mount
->mnt_sb
->s_root
;
476 dbg("Ah! can not find a parent!");
481 mutex_lock(&parent
->d_inode
->i_mutex
);
482 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
483 if (!IS_ERR(dentry
)) {
484 if ((mode
& S_IFMT
) == S_IFDIR
)
485 error
= usbfs_mkdir (parent
->d_inode
, *dentry
, mode
);
487 error
= usbfs_create (parent
->d_inode
, *dentry
, mode
);
489 error
= PTR_ERR(dentry
);
490 mutex_unlock(&parent
->d_inode
->i_mutex
);
495 static struct dentry
*fs_create_file (const char *name
, mode_t mode
,
496 struct dentry
*parent
, void *data
,
497 const struct file_operations
*fops
,
498 uid_t uid
, gid_t gid
)
500 struct dentry
*dentry
;
503 dbg("creating file '%s'",name
);
505 error
= fs_create_by_name (name
, mode
, parent
, &dentry
);
509 if (dentry
->d_inode
) {
511 dentry
->d_inode
->i_private
= data
;
513 dentry
->d_inode
->i_fop
= fops
;
514 dentry
->d_inode
->i_uid
= uid
;
515 dentry
->d_inode
->i_gid
= gid
;
522 static void fs_remove_file (struct dentry
*dentry
)
524 struct dentry
*parent
= dentry
->d_parent
;
526 if (!parent
|| !parent
->d_inode
)
529 mutex_lock_nested(&parent
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
530 if (usbfs_positive(dentry
)) {
531 if (dentry
->d_inode
) {
532 if (S_ISDIR(dentry
->d_inode
->i_mode
))
533 usbfs_rmdir(parent
->d_inode
, dentry
);
535 usbfs_unlink(parent
->d_inode
, dentry
);
539 mutex_unlock(&parent
->d_inode
->i_mutex
);
542 /* --------------------------------------------------------------------- */
544 static int usb_get_sb(struct file_system_type
*fs_type
,
545 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
547 return get_sb_single(fs_type
, flags
, data
, usbfs_fill_super
, mnt
);
550 static struct file_system_type usb_fs_type
= {
551 .owner
= THIS_MODULE
,
553 .get_sb
= usb_get_sb
,
554 .kill_sb
= kill_litter_super
,
557 /* --------------------------------------------------------------------- */
559 static int create_special_files (void)
561 struct dentry
*parent
;
564 /* the simple_pin_fs calls will call remount with no options
565 * without this flag that would overwrite the real mount options (if any)
569 /* create the devices special file */
570 retval
= simple_pin_fs(&usb_fs_type
, &usbfs_mount
, &usbfs_mount_count
);
572 err ("Unable to get usbfs mount");
578 parent
= usbfs_mount
->mnt_sb
->s_root
;
579 devices_usbfs_dentry
= fs_create_file ("devices",
580 listmode
| S_IFREG
, parent
,
581 NULL
, &usbfs_devices_fops
,
583 if (devices_usbfs_dentry
== NULL
) {
584 err ("Unable to create devices usbfs file");
586 goto error_clean_mounts
;
592 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
597 static void remove_special_files (void)
599 if (devices_usbfs_dentry
)
600 fs_remove_file (devices_usbfs_dentry
);
601 devices_usbfs_dentry
= NULL
;
602 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
605 void usbfs_update_special (void)
609 if (devices_usbfs_dentry
) {
610 inode
= devices_usbfs_dentry
->d_inode
;
612 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
616 static void usbfs_add_bus(struct usb_bus
*bus
)
618 struct dentry
*parent
;
622 /* create the special files if this is the first bus added */
623 if (num_buses
== 0) {
624 retval
= create_special_files();
630 sprintf (name
, "%03d", bus
->busnum
);
632 parent
= usbfs_mount
->mnt_sb
->s_root
;
633 bus
->usbfs_dentry
= fs_create_file (name
, busmode
| S_IFDIR
, parent
,
634 bus
, NULL
, busuid
, busgid
);
635 if (bus
->usbfs_dentry
== NULL
) {
636 err ("error creating usbfs bus entry");
641 static void usbfs_remove_bus(struct usb_bus
*bus
)
643 if (bus
->usbfs_dentry
) {
644 fs_remove_file (bus
->usbfs_dentry
);
645 bus
->usbfs_dentry
= NULL
;
649 if (num_buses
<= 0) {
650 remove_special_files();
655 static void usbfs_add_device(struct usb_device
*dev
)
661 sprintf (name
, "%03d", dev
->devnum
);
662 dev
->usbfs_dentry
= fs_create_file (name
, devmode
| S_IFREG
,
663 dev
->bus
->usbfs_dentry
, dev
,
664 &usbdev_file_operations
,
666 if (dev
->usbfs_dentry
== NULL
) {
667 err ("error creating usbfs device entry");
671 /* Set the size of the device's file to be
672 * equal to the size of the device descriptors. */
673 i_size
= sizeof (struct usb_device_descriptor
);
674 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; ++i
) {
675 struct usb_config_descriptor
*config
=
676 (struct usb_config_descriptor
*)dev
->rawdescriptors
[i
];
677 i_size
+= le16_to_cpu(config
->wTotalLength
);
679 if (dev
->usbfs_dentry
->d_inode
)
680 dev
->usbfs_dentry
->d_inode
->i_size
= i_size
;
683 static void usbfs_remove_device(struct usb_device
*dev
)
685 struct dev_state
*ds
;
686 struct siginfo sinfo
;
688 if (dev
->usbfs_dentry
) {
689 fs_remove_file (dev
->usbfs_dentry
);
690 dev
->usbfs_dentry
= NULL
;
692 while (!list_empty(&dev
->filelist
)) {
693 ds
= list_entry(dev
->filelist
.next
, struct dev_state
, list
);
694 wake_up_all(&ds
->wait
);
695 list_del_init(&ds
->list
);
697 sinfo
.si_signo
= ds
->discsignr
;
698 sinfo
.si_errno
= EPIPE
;
699 sinfo
.si_code
= SI_ASYNCIO
;
700 sinfo
.si_addr
= ds
->disccontext
;
701 kill_pid_info_as_uid(ds
->discsignr
, &sinfo
, ds
->disc_pid
, ds
->disc_uid
, ds
->disc_euid
, ds
->secid
);
706 static int usbfs_notify(struct notifier_block
*self
, unsigned long action
, void *dev
)
710 usbfs_add_device(dev
);
712 case USB_DEVICE_REMOVE
:
713 usbfs_remove_device(dev
);
719 usbfs_remove_bus(dev
);
722 usbfs_update_special();
723 usbfs_conn_disc_event();
727 static struct notifier_block usbfs_nb
= {
728 .notifier_call
= usbfs_notify
,
731 /* --------------------------------------------------------------------- */
733 static struct proc_dir_entry
*usbdir
= NULL
;
735 int __init
usbfs_init(void)
739 retval
= register_filesystem(&usb_fs_type
);
743 usb_register_notify(&usbfs_nb
);
745 /* create mount point for usbfs */
746 usbdir
= proc_mkdir("usb", proc_bus
);
751 void usbfs_cleanup(void)
753 usb_unregister_notify(&usbfs_nb
);
754 unregister_filesystem(&usb_fs_type
);
756 remove_proc_entry("usb", proc_bus
);