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/smp_lock.h>
40 #include <linux/parser.h>
41 #include <linux/notifier.h>
42 #include <asm/byteorder.h>
46 static struct super_operations usbfs_ops
;
47 static struct file_operations default_file_operations
;
48 static struct vfsmount
*usbfs_mount
;
49 static int usbfs_mount_count
; /* = 0 */
50 static int ignore_mount
= 0;
52 static struct dentry
*devices_usbfs_dentry
;
53 static int num_buses
; /* = 0 */
55 static uid_t devuid
; /* = 0 */
56 static uid_t busuid
; /* = 0 */
57 static uid_t listuid
; /* = 0 */
58 static gid_t devgid
; /* = 0 */
59 static gid_t busgid
; /* = 0 */
60 static gid_t listgid
; /* = 0 */
61 static umode_t devmode
= S_IWUSR
| S_IRUGO
;
62 static umode_t busmode
= S_IXUGO
| S_IRUGO
;
63 static umode_t listmode
= S_IRUGO
;
66 Opt_devuid
, Opt_devgid
, Opt_devmode
,
67 Opt_busuid
, Opt_busgid
, Opt_busmode
,
68 Opt_listuid
, Opt_listgid
, Opt_listmode
,
72 static match_table_t tokens
= {
73 {Opt_devuid
, "devuid=%u"},
74 {Opt_devgid
, "devgid=%u"},
75 {Opt_devmode
, "devmode=%o"},
76 {Opt_busuid
, "busuid=%u"},
77 {Opt_busgid
, "busgid=%u"},
78 {Opt_busmode
, "busmode=%o"},
79 {Opt_listuid
, "listuid=%u"},
80 {Opt_listgid
, "listgid=%u"},
81 {Opt_listmode
, "listmode=%o"},
85 static int parse_options(struct super_block
*s
, char *data
)
90 /* (re)set to defaults. */
97 devmode
= S_IWUSR
| S_IRUGO
;
98 busmode
= S_IXUGO
| S_IRUGO
;
101 while ((p
= strsep(&data
, ",")) != NULL
) {
102 substring_t args
[MAX_OPT_ARGS
];
107 token
= match_token(p
, tokens
, args
);
110 if (match_int(&args
[0], &option
))
115 if (match_int(&args
[0], &option
))
120 if (match_octal(&args
[0], &option
))
122 devmode
= option
& S_IRWXUGO
;
125 if (match_int(&args
[0], &option
))
130 if (match_int(&args
[0], &option
))
135 if (match_octal(&args
[0], &option
))
137 busmode
= option
& S_IRWXUGO
;
140 if (match_int(&args
[0], &option
))
145 if (match_int(&args
[0], &option
))
150 if (match_octal(&args
[0], &option
))
152 listmode
= option
& S_IRWXUGO
;
155 err("usbfs: unrecognised mount option \"%s\" "
156 "or missing value\n", p
);
164 static void update_special(struct dentry
*special
)
166 special
->d_inode
->i_uid
= listuid
;
167 special
->d_inode
->i_gid
= listgid
;
168 special
->d_inode
->i_mode
= S_IFREG
| listmode
;
171 static void update_dev(struct dentry
*dev
)
173 dev
->d_inode
->i_uid
= devuid
;
174 dev
->d_inode
->i_gid
= devgid
;
175 dev
->d_inode
->i_mode
= S_IFREG
| devmode
;
178 static void update_bus(struct dentry
*bus
)
180 struct dentry
*dev
= NULL
;
182 bus
->d_inode
->i_uid
= busuid
;
183 bus
->d_inode
->i_gid
= busgid
;
184 bus
->d_inode
->i_mode
= S_IFDIR
| busmode
;
186 mutex_lock(&bus
->d_inode
->i_mutex
);
188 list_for_each_entry(dev
, &bus
->d_subdirs
, d_u
.d_child
)
192 mutex_unlock(&bus
->d_inode
->i_mutex
);
195 static void update_sb(struct super_block
*sb
)
197 struct dentry
*root
= sb
->s_root
;
198 struct dentry
*bus
= NULL
;
203 mutex_lock_nested(&root
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
205 list_for_each_entry(bus
, &root
->d_subdirs
, d_u
.d_child
) {
207 switch (S_IFMT
& bus
->d_inode
->i_mode
) {
215 warn("Unknown node %s mode %x found on remount!\n",bus
->d_name
.name
,bus
->d_inode
->i_mode
);
221 mutex_unlock(&root
->d_inode
->i_mutex
);
224 static int remount(struct super_block
*sb
, int *flags
, char *data
)
226 /* If this is not a real mount,
227 * i.e. it's a simple_pin_fs from create_special_files,
233 if (parse_options(sb
, data
)) {
234 warn("usbfs: mount parameter error:");
238 if (usbfs_mount
&& usbfs_mount
->mnt_sb
)
239 update_sb(usbfs_mount
->mnt_sb
);
244 static struct inode
*usbfs_get_inode (struct super_block
*sb
, int mode
, dev_t dev
)
246 struct inode
*inode
= new_inode(sb
);
249 inode
->i_mode
= mode
;
250 inode
->i_uid
= current
->fsuid
;
251 inode
->i_gid
= current
->fsgid
;
252 inode
->i_blksize
= PAGE_CACHE_SIZE
;
254 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
255 switch (mode
& S_IFMT
) {
257 init_special_inode(inode
, mode
, dev
);
260 inode
->i_fop
= &default_file_operations
;
263 inode
->i_op
= &simple_dir_inode_operations
;
264 inode
->i_fop
= &simple_dir_operations
;
266 /* directory inodes start off with i_nlink == 2 (for "." entry) */
275 static int usbfs_mknod (struct inode
*dir
, struct dentry
*dentry
, int mode
,
278 struct inode
*inode
= usbfs_get_inode(dir
->i_sb
, mode
, dev
);
285 d_instantiate(dentry
, inode
);
292 static int usbfs_mkdir (struct inode
*dir
, struct dentry
*dentry
, int mode
)
296 mode
= (mode
& (S_IRWXUGO
| S_ISVTX
)) | S_IFDIR
;
297 res
= usbfs_mknod (dir
, dentry
, mode
, 0);
303 static int usbfs_create (struct inode
*dir
, struct dentry
*dentry
, int mode
)
305 mode
= (mode
& S_IALLUGO
) | S_IFREG
;
306 return usbfs_mknod (dir
, dentry
, mode
, 0);
309 static inline int usbfs_positive (struct dentry
*dentry
)
311 return dentry
->d_inode
&& !d_unhashed(dentry
);
314 static int usbfs_empty (struct dentry
*dentry
)
316 struct list_head
*list
;
318 spin_lock(&dcache_lock
);
320 list_for_each(list
, &dentry
->d_subdirs
) {
321 struct dentry
*de
= list_entry(list
, struct dentry
, d_u
.d_child
);
322 if (usbfs_positive(de
)) {
323 spin_unlock(&dcache_lock
);
328 spin_unlock(&dcache_lock
);
332 static int usbfs_unlink (struct inode
*dir
, struct dentry
*dentry
)
334 struct inode
*inode
= dentry
->d_inode
;
335 mutex_lock(&inode
->i_mutex
);
336 dentry
->d_inode
->i_nlink
--;
338 mutex_unlock(&inode
->i_mutex
);
343 static int usbfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
345 int error
= -ENOTEMPTY
;
346 struct inode
* inode
= dentry
->d_inode
;
348 mutex_lock(&inode
->i_mutex
);
349 dentry_unhash(dentry
);
350 if (usbfs_empty(dentry
)) {
351 dentry
->d_inode
->i_nlink
-= 2;
353 inode
->i_flags
|= S_DEAD
;
357 mutex_unlock(&inode
->i_mutex
);
365 /* default file operations */
366 static ssize_t
default_read_file (struct file
*file
, char __user
*buf
,
367 size_t count
, loff_t
*ppos
)
372 static ssize_t
default_write_file (struct file
*file
, const char __user
*buf
,
373 size_t count
, loff_t
*ppos
)
378 static loff_t
default_file_lseek (struct file
*file
, loff_t offset
, int orig
)
380 loff_t retval
= -EINVAL
;
382 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
386 file
->f_pos
= offset
;
387 retval
= file
->f_pos
;
391 if ((offset
+ file
->f_pos
) > 0) {
392 file
->f_pos
+= offset
;
393 retval
= file
->f_pos
;
399 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
403 static int default_open (struct inode
*inode
, struct file
*file
)
405 if (inode
->u
.generic_ip
)
406 file
->private_data
= inode
->u
.generic_ip
;
411 static struct file_operations default_file_operations
= {
412 .read
= default_read_file
,
413 .write
= default_write_file
,
414 .open
= default_open
,
415 .llseek
= default_file_lseek
,
418 static struct super_operations usbfs_ops
= {
419 .statfs
= simple_statfs
,
420 .drop_inode
= generic_delete_inode
,
421 .remount_fs
= remount
,
424 static int usbfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
429 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
430 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
431 sb
->s_magic
= USBDEVICE_SUPER_MAGIC
;
432 sb
->s_op
= &usbfs_ops
;
434 inode
= usbfs_get_inode(sb
, S_IFDIR
| 0755, 0);
437 dbg("%s: could not get inode!",__FUNCTION__
);
441 root
= d_alloc_root(inode
);
443 dbg("%s: could not get root dentry!",__FUNCTION__
);
452 * fs_create_by_name - create a file, given a name
453 * @name: name of file
454 * @mode: type of file
455 * @parent: dentry of directory to create it in
456 * @dentry: resulting dentry of file
458 * This function handles both regular files and directories.
460 static int fs_create_by_name (const char *name
, mode_t mode
,
461 struct dentry
*parent
, struct dentry
**dentry
)
465 /* If the parent is not specified, we create it in the root.
466 * We need the root dentry to do this, which is in the super
467 * block. A pointer to that is in the struct vfsmount that we
471 if (usbfs_mount
&& usbfs_mount
->mnt_sb
) {
472 parent
= usbfs_mount
->mnt_sb
->s_root
;
477 dbg("Ah! can not find a parent!");
482 mutex_lock(&parent
->d_inode
->i_mutex
);
483 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
484 if (!IS_ERR(dentry
)) {
485 if ((mode
& S_IFMT
) == S_IFDIR
)
486 error
= usbfs_mkdir (parent
->d_inode
, *dentry
, mode
);
488 error
= usbfs_create (parent
->d_inode
, *dentry
, mode
);
490 error
= PTR_ERR(dentry
);
491 mutex_unlock(&parent
->d_inode
->i_mutex
);
496 static struct dentry
*fs_create_file (const char *name
, mode_t mode
,
497 struct dentry
*parent
, void *data
,
498 struct file_operations
*fops
,
499 uid_t uid
, gid_t gid
)
501 struct dentry
*dentry
;
504 dbg("creating file '%s'",name
);
506 error
= fs_create_by_name (name
, mode
, parent
, &dentry
);
510 if (dentry
->d_inode
) {
512 dentry
->d_inode
->u
.generic_ip
= data
;
514 dentry
->d_inode
->i_fop
= fops
;
515 dentry
->d_inode
->i_uid
= uid
;
516 dentry
->d_inode
->i_gid
= gid
;
523 static void fs_remove_file (struct dentry
*dentry
)
525 struct dentry
*parent
= dentry
->d_parent
;
527 if (!parent
|| !parent
->d_inode
)
530 mutex_lock_nested(&parent
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
531 if (usbfs_positive(dentry
)) {
532 if (dentry
->d_inode
) {
533 if (S_ISDIR(dentry
->d_inode
->i_mode
))
534 usbfs_rmdir(parent
->d_inode
, dentry
);
536 usbfs_unlink(parent
->d_inode
, dentry
);
540 mutex_unlock(&parent
->d_inode
->i_mutex
);
543 /* --------------------------------------------------------------------- */
545 static int usb_get_sb(struct file_system_type
*fs_type
,
546 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
548 return get_sb_single(fs_type
, flags
, data
, usbfs_fill_super
, mnt
);
551 static struct file_system_type usb_fs_type
= {
552 .owner
= THIS_MODULE
,
554 .get_sb
= usb_get_sb
,
555 .kill_sb
= kill_litter_super
,
558 /* --------------------------------------------------------------------- */
560 static int create_special_files (void)
562 struct dentry
*parent
;
565 /* the simple_pin_fs calls will call remount with no options
566 * without this flag that would overwrite the real mount options (if any)
570 /* create the devices special file */
571 retval
= simple_pin_fs(&usb_fs_type
, &usbfs_mount
, &usbfs_mount_count
);
573 err ("Unable to get usbfs mount");
579 parent
= usbfs_mount
->mnt_sb
->s_root
;
580 devices_usbfs_dentry
= fs_create_file ("devices",
581 listmode
| S_IFREG
, parent
,
582 NULL
, &usbfs_devices_fops
,
584 if (devices_usbfs_dentry
== NULL
) {
585 err ("Unable to create devices usbfs file");
587 goto error_clean_mounts
;
593 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
598 static void remove_special_files (void)
600 if (devices_usbfs_dentry
)
601 fs_remove_file (devices_usbfs_dentry
);
602 devices_usbfs_dentry
= NULL
;
603 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
606 void usbfs_update_special (void)
610 if (devices_usbfs_dentry
) {
611 inode
= devices_usbfs_dentry
->d_inode
;
613 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
617 static void usbfs_add_bus(struct usb_bus
*bus
)
619 struct dentry
*parent
;
623 /* create the special files if this is the first bus added */
624 if (num_buses
== 0) {
625 retval
= create_special_files();
631 sprintf (name
, "%03d", bus
->busnum
);
633 parent
= usbfs_mount
->mnt_sb
->s_root
;
634 bus
->usbfs_dentry
= fs_create_file (name
, busmode
| S_IFDIR
, parent
,
635 bus
, NULL
, busuid
, busgid
);
636 if (bus
->usbfs_dentry
== NULL
) {
637 err ("error creating usbfs bus entry");
642 static void usbfs_remove_bus(struct usb_bus
*bus
)
644 if (bus
->usbfs_dentry
) {
645 fs_remove_file (bus
->usbfs_dentry
);
646 bus
->usbfs_dentry
= NULL
;
650 if (num_buses
<= 0) {
651 remove_special_files();
656 static void usbfs_add_device(struct usb_device
*dev
)
662 sprintf (name
, "%03d", dev
->devnum
);
663 dev
->usbfs_dentry
= fs_create_file (name
, devmode
| S_IFREG
,
664 dev
->bus
->usbfs_dentry
, dev
,
665 &usbfs_device_file_operations
,
667 if (dev
->usbfs_dentry
== NULL
) {
668 err ("error creating usbfs device entry");
672 /* Set the size of the device's file to be
673 * equal to the size of the device descriptors. */
674 i_size
= sizeof (struct usb_device_descriptor
);
675 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; ++i
) {
676 struct usb_config_descriptor
*config
=
677 (struct usb_config_descriptor
*)dev
->rawdescriptors
[i
];
678 i_size
+= le16_to_cpu(config
->wTotalLength
);
680 if (dev
->usbfs_dentry
->d_inode
)
681 dev
->usbfs_dentry
->d_inode
->i_size
= i_size
;
684 static void usbfs_remove_device(struct usb_device
*dev
)
686 struct dev_state
*ds
;
687 struct siginfo sinfo
;
689 if (dev
->usbfs_dentry
) {
690 fs_remove_file (dev
->usbfs_dentry
);
691 dev
->usbfs_dentry
= NULL
;
693 while (!list_empty(&dev
->filelist
)) {
694 ds
= list_entry(dev
->filelist
.next
, struct dev_state
, list
);
695 wake_up_all(&ds
->wait
);
696 list_del_init(&ds
->list
);
698 sinfo
.si_signo
= ds
->discsignr
;
699 sinfo
.si_errno
= EPIPE
;
700 sinfo
.si_code
= SI_ASYNCIO
;
701 sinfo
.si_addr
= ds
->disccontext
;
702 kill_proc_info_as_uid(ds
->discsignr
, &sinfo
, ds
->disc_pid
, ds
->disc_uid
, ds
->disc_euid
, ds
->secid
);
707 static int usbfs_notify(struct notifier_block
*self
, unsigned long action
, void *dev
)
711 usbfs_add_device(dev
);
713 case USB_DEVICE_REMOVE
:
714 usbfs_remove_device(dev
);
720 usbfs_remove_bus(dev
);
723 usbfs_update_special();
724 usbfs_conn_disc_event();
728 static struct notifier_block usbfs_nb
= {
729 .notifier_call
= usbfs_notify
,
732 /* --------------------------------------------------------------------- */
734 static struct proc_dir_entry
*usbdir
= NULL
;
736 int __init
usbfs_init(void)
740 retval
= register_filesystem(&usb_fs_type
);
744 usb_register_notify(&usbfs_nb
);
746 /* create mount point for usbfs */
747 usbdir
= proc_mkdir("usb", proc_bus
);
752 void usbfs_cleanup(void)
754 usb_unregister_notify(&usbfs_nb
);
755 unregister_filesystem(&usb_fs_type
);
757 remove_proc_entry("usb", proc_bus
);