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/config.h>
31 #include <linux/module.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/init.h>
36 #include <linux/proc_fs.h>
37 #include <linux/usb.h>
38 #include <linux/namei.h>
39 #include <linux/usbdevice_fs.h>
40 #include <linux/smp_lock.h>
41 #include <linux/parser.h>
42 #include <asm/byteorder.h>
46 static struct super_operations usbfs_ops
;
47 static struct file_operations default_file_operations
;
48 static struct inode_operations usbfs_dir_inode_operations
;
49 static struct vfsmount
*usbfs_mount
;
50 static int usbfs_mount_count
; /* = 0 */
51 static int ignore_mount
= 0;
53 static struct dentry
*devices_usbfs_dentry
;
54 static int num_buses
; /* = 0 */
56 static uid_t devuid
; /* = 0 */
57 static uid_t busuid
; /* = 0 */
58 static uid_t listuid
; /* = 0 */
59 static gid_t devgid
; /* = 0 */
60 static gid_t busgid
; /* = 0 */
61 static gid_t listgid
; /* = 0 */
62 static umode_t devmode
= S_IWUSR
| S_IRUGO
;
63 static umode_t busmode
= S_IXUGO
| S_IRUGO
;
64 static umode_t listmode
= S_IRUGO
;
67 Opt_devuid
, Opt_devgid
, Opt_devmode
,
68 Opt_busuid
, Opt_busgid
, Opt_busmode
,
69 Opt_listuid
, Opt_listgid
, Opt_listmode
,
73 static match_table_t tokens
= {
74 {Opt_devuid
, "devuid=%u"},
75 {Opt_devgid
, "devgid=%u"},
76 {Opt_devmode
, "devmode=%o"},
77 {Opt_busuid
, "busuid=%u"},
78 {Opt_busgid
, "busgid=%u"},
79 {Opt_busmode
, "busmode=%o"},
80 {Opt_listuid
, "listuid=%u"},
81 {Opt_listgid
, "listgid=%u"},
82 {Opt_listmode
, "listmode=%o"},
86 static int parse_options(struct super_block
*s
, char *data
)
91 /* (re)set to defaults. */
98 devmode
= S_IWUSR
| S_IRUGO
;
99 busmode
= S_IXUGO
| S_IRUGO
;
102 while ((p
= strsep(&data
, ",")) != NULL
) {
103 substring_t args
[MAX_OPT_ARGS
];
108 token
= match_token(p
, tokens
, args
);
111 if (match_int(&args
[0], &option
))
116 if (match_int(&args
[0], &option
))
121 if (match_octal(&args
[0], &option
))
123 devmode
= option
& S_IRWXUGO
;
126 if (match_int(&args
[0], &option
))
131 if (match_int(&args
[0], &option
))
136 if (match_octal(&args
[0], &option
))
138 busmode
= option
& S_IRWXUGO
;
141 if (match_int(&args
[0], &option
))
146 if (match_int(&args
[0], &option
))
151 if (match_octal(&args
[0], &option
))
153 listmode
= option
& S_IRWXUGO
;
156 err("usbfs: unrecognised mount option \"%s\" "
157 "or missing value\n", p
);
165 static void update_special(struct dentry
*special
)
167 special
->d_inode
->i_uid
= listuid
;
168 special
->d_inode
->i_gid
= listgid
;
169 special
->d_inode
->i_mode
= S_IFREG
| listmode
;
172 static void update_dev(struct dentry
*dev
)
174 dev
->d_inode
->i_uid
= devuid
;
175 dev
->d_inode
->i_gid
= devgid
;
176 dev
->d_inode
->i_mode
= S_IFREG
| devmode
;
179 static void update_bus(struct dentry
*bus
)
181 struct dentry
*dev
= NULL
;
183 bus
->d_inode
->i_uid
= busuid
;
184 bus
->d_inode
->i_gid
= busgid
;
185 bus
->d_inode
->i_mode
= S_IFDIR
| busmode
;
187 down(&bus
->d_inode
->i_sem
);
189 list_for_each_entry(dev
, &bus
->d_subdirs
, d_child
)
193 up(&bus
->d_inode
->i_sem
);
196 static void update_sb(struct super_block
*sb
)
198 struct dentry
*root
= sb
->s_root
;
199 struct dentry
*bus
= NULL
;
204 down(&root
->d_inode
->i_sem
);
206 list_for_each_entry(bus
, &root
->d_subdirs
, d_child
) {
208 switch (S_IFMT
& bus
->d_inode
->i_mode
) {
216 warn("Unknown node %s mode %x found on remount!\n",bus
->d_name
.name
,bus
->d_inode
->i_mode
);
222 up(&root
->d_inode
->i_sem
);
225 static int remount(struct super_block
*sb
, int *flags
, char *data
)
227 /* If this is not a real mount,
228 * i.e. it's a simple_pin_fs from create_special_files,
234 if (parse_options(sb
, data
)) {
235 warn("usbfs: mount parameter error:");
239 if (usbfs_mount
&& usbfs_mount
->mnt_sb
)
240 update_sb(usbfs_mount
->mnt_sb
);
245 static struct inode
*usbfs_get_inode (struct super_block
*sb
, int mode
, dev_t dev
)
247 struct inode
*inode
= new_inode(sb
);
250 inode
->i_mode
= mode
;
251 inode
->i_uid
= current
->fsuid
;
252 inode
->i_gid
= current
->fsgid
;
253 inode
->i_blksize
= PAGE_CACHE_SIZE
;
255 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
256 switch (mode
& S_IFMT
) {
258 init_special_inode(inode
, mode
, dev
);
261 inode
->i_fop
= &default_file_operations
;
264 inode
->i_op
= &usbfs_dir_inode_operations
;
265 inode
->i_fop
= &simple_dir_operations
;
267 /* directory inodes start off with i_nlink == 2 (for "." entry) */
276 static int usbfs_mknod (struct inode
*dir
, struct dentry
*dentry
, int mode
,
279 struct inode
*inode
= usbfs_get_inode(dir
->i_sb
, mode
, dev
);
286 d_instantiate(dentry
, inode
);
293 static int usbfs_mkdir (struct inode
*dir
, struct dentry
*dentry
, int mode
)
297 mode
= (mode
& (S_IRWXUGO
| S_ISVTX
)) | S_IFDIR
;
298 res
= usbfs_mknod (dir
, dentry
, mode
, 0);
304 static int usbfs_create (struct inode
*dir
, struct dentry
*dentry
, int mode
)
306 mode
= (mode
& S_IALLUGO
) | S_IFREG
;
307 return usbfs_mknod (dir
, dentry
, mode
, 0);
310 static inline int usbfs_positive (struct dentry
*dentry
)
312 return dentry
->d_inode
&& !d_unhashed(dentry
);
315 static int usbfs_empty (struct dentry
*dentry
)
317 struct list_head
*list
;
319 spin_lock(&dcache_lock
);
321 list_for_each(list
, &dentry
->d_subdirs
) {
322 struct dentry
*de
= list_entry(list
, struct dentry
, d_child
);
323 if (usbfs_positive(de
)) {
324 spin_unlock(&dcache_lock
);
329 spin_unlock(&dcache_lock
);
333 static int usbfs_unlink (struct inode
*dir
, struct dentry
*dentry
)
335 struct inode
*inode
= dentry
->d_inode
;
337 dentry
->d_inode
->i_nlink
--;
344 static int usbfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
346 int error
= -ENOTEMPTY
;
347 struct inode
* inode
= dentry
->d_inode
;
350 dentry_unhash(dentry
);
351 if (usbfs_empty(dentry
)) {
352 dentry
->d_inode
->i_nlink
-= 2;
354 inode
->i_flags
|= S_DEAD
;
366 /* default file operations */
367 static ssize_t
default_read_file (struct file
*file
, char __user
*buf
,
368 size_t count
, loff_t
*ppos
)
373 static ssize_t
default_write_file (struct file
*file
, const char __user
*buf
,
374 size_t count
, loff_t
*ppos
)
379 static loff_t
default_file_lseek (struct file
*file
, loff_t offset
, int orig
)
381 loff_t retval
= -EINVAL
;
383 down(&file
->f_dentry
->d_inode
->i_sem
);
387 file
->f_pos
= offset
;
388 retval
= file
->f_pos
;
392 if ((offset
+ file
->f_pos
) > 0) {
393 file
->f_pos
+= offset
;
394 retval
= file
->f_pos
;
400 up(&file
->f_dentry
->d_inode
->i_sem
);
404 static int default_open (struct inode
*inode
, struct file
*file
)
406 if (inode
->u
.generic_ip
)
407 file
->private_data
= inode
->u
.generic_ip
;
412 static struct file_operations default_file_operations
= {
413 .read
= default_read_file
,
414 .write
= default_write_file
,
415 .open
= default_open
,
416 .llseek
= default_file_lseek
,
419 static struct inode_operations usbfs_dir_inode_operations
= {
420 .lookup
= simple_lookup
,
423 static struct super_operations usbfs_ops
= {
424 .statfs
= simple_statfs
,
425 .drop_inode
= generic_delete_inode
,
426 .remount_fs
= remount
,
429 static int usbfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
434 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
435 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
436 sb
->s_magic
= USBDEVICE_SUPER_MAGIC
;
437 sb
->s_op
= &usbfs_ops
;
439 inode
= usbfs_get_inode(sb
, S_IFDIR
| 0755, 0);
442 dbg("%s: could not get inode!",__FUNCTION__
);
446 root
= d_alloc_root(inode
);
448 dbg("%s: could not get root dentry!",__FUNCTION__
);
457 * fs_create_by_name - create a file, given a name
458 * @name: name of file
459 * @mode: type of file
460 * @parent: dentry of directory to create it in
461 * @dentry: resulting dentry of file
463 * This function handles both regular files and directories.
465 static int fs_create_by_name (const char *name
, mode_t mode
,
466 struct dentry
*parent
, struct dentry
**dentry
)
470 /* If the parent is not specified, we create it in the root.
471 * We need the root dentry to do this, which is in the super
472 * block. A pointer to that is in the struct vfsmount that we
476 if (usbfs_mount
&& usbfs_mount
->mnt_sb
) {
477 parent
= usbfs_mount
->mnt_sb
->s_root
;
482 dbg("Ah! can not find a parent!");
487 down(&parent
->d_inode
->i_sem
);
488 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
489 if (!IS_ERR(dentry
)) {
490 if ((mode
& S_IFMT
) == S_IFDIR
)
491 error
= usbfs_mkdir (parent
->d_inode
, *dentry
, mode
);
493 error
= usbfs_create (parent
->d_inode
, *dentry
, mode
);
495 error
= PTR_ERR(dentry
);
496 up(&parent
->d_inode
->i_sem
);
501 static struct dentry
*fs_create_file (const char *name
, mode_t mode
,
502 struct dentry
*parent
, void *data
,
503 struct file_operations
*fops
,
504 uid_t uid
, gid_t gid
)
506 struct dentry
*dentry
;
509 dbg("creating file '%s'",name
);
511 error
= fs_create_by_name (name
, mode
, parent
, &dentry
);
515 if (dentry
->d_inode
) {
517 dentry
->d_inode
->u
.generic_ip
= data
;
519 dentry
->d_inode
->i_fop
= fops
;
520 dentry
->d_inode
->i_uid
= uid
;
521 dentry
->d_inode
->i_gid
= gid
;
528 static void fs_remove_file (struct dentry
*dentry
)
530 struct dentry
*parent
= dentry
->d_parent
;
532 if (!parent
|| !parent
->d_inode
)
535 down(&parent
->d_inode
->i_sem
);
536 if (usbfs_positive(dentry
)) {
537 if (dentry
->d_inode
) {
538 if (S_ISDIR(dentry
->d_inode
->i_mode
))
539 usbfs_rmdir(parent
->d_inode
, dentry
);
541 usbfs_unlink(parent
->d_inode
, dentry
);
545 up(&parent
->d_inode
->i_sem
);
548 /* --------------------------------------------------------------------- */
550 static struct super_block
*usb_get_sb(struct file_system_type
*fs_type
,
551 int flags
, const char *dev_name
, void *data
)
553 return get_sb_single(fs_type
, flags
, data
, usbfs_fill_super
);
556 static struct file_system_type usb_fs_type
= {
557 .owner
= THIS_MODULE
,
559 .get_sb
= usb_get_sb
,
560 .kill_sb
= kill_litter_super
,
563 /* --------------------------------------------------------------------- */
565 static int create_special_files (void)
567 struct dentry
*parent
;
570 /* the simple_pin_fs calls will call remount with no options
571 * without this flag that would overwrite the real mount options (if any)
575 /* create the devices special file */
576 retval
= simple_pin_fs("usbfs", &usbfs_mount
, &usbfs_mount_count
);
578 err ("Unable to get usbfs mount");
584 parent
= usbfs_mount
->mnt_sb
->s_root
;
585 devices_usbfs_dentry
= fs_create_file ("devices",
586 listmode
| S_IFREG
, parent
,
587 NULL
, &usbfs_devices_fops
,
589 if (devices_usbfs_dentry
== NULL
) {
590 err ("Unable to create devices usbfs file");
592 goto error_clean_mounts
;
598 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
603 static void remove_special_files (void)
605 if (devices_usbfs_dentry
)
606 fs_remove_file (devices_usbfs_dentry
);
607 devices_usbfs_dentry
= NULL
;
608 simple_release_fs(&usbfs_mount
, &usbfs_mount_count
);
611 void usbfs_update_special (void)
615 if (devices_usbfs_dentry
) {
616 inode
= devices_usbfs_dentry
->d_inode
;
618 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
622 void usbfs_add_bus(struct usb_bus
*bus
)
624 struct dentry
*parent
;
628 /* create the special files if this is the first bus added */
629 if (num_buses
== 0) {
630 retval
= create_special_files();
636 sprintf (name
, "%03d", bus
->busnum
);
638 parent
= usbfs_mount
->mnt_sb
->s_root
;
639 bus
->usbfs_dentry
= fs_create_file (name
, busmode
| S_IFDIR
, parent
,
640 bus
, NULL
, busuid
, busgid
);
641 if (bus
->usbfs_dentry
== NULL
) {
642 err ("error creating usbfs bus entry");
646 usbfs_update_special();
647 usbfs_conn_disc_event();
650 void usbfs_remove_bus(struct usb_bus
*bus
)
652 if (bus
->usbfs_dentry
) {
653 fs_remove_file (bus
->usbfs_dentry
);
654 bus
->usbfs_dentry
= NULL
;
658 if (num_buses
<= 0) {
659 remove_special_files();
663 usbfs_update_special();
664 usbfs_conn_disc_event();
667 void usbfs_add_device(struct usb_device
*dev
)
673 sprintf (name
, "%03d", dev
->devnum
);
674 dev
->usbfs_dentry
= fs_create_file (name
, devmode
| S_IFREG
,
675 dev
->bus
->usbfs_dentry
, dev
,
676 &usbfs_device_file_operations
,
678 if (dev
->usbfs_dentry
== NULL
) {
679 err ("error creating usbfs device entry");
683 /* Set the size of the device's file to be
684 * equal to the size of the device descriptors. */
685 i_size
= sizeof (struct usb_device_descriptor
);
686 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; ++i
) {
687 struct usb_config_descriptor
*config
=
688 (struct usb_config_descriptor
*)dev
->rawdescriptors
[i
];
689 i_size
+= le16_to_cpu(config
->wTotalLength
);
691 if (dev
->usbfs_dentry
->d_inode
)
692 dev
->usbfs_dentry
->d_inode
->i_size
= i_size
;
694 usbfs_update_special();
695 usbfs_conn_disc_event();
698 void usbfs_remove_device(struct usb_device
*dev
)
700 struct dev_state
*ds
;
701 struct siginfo sinfo
;
703 if (dev
->usbfs_dentry
) {
704 fs_remove_file (dev
->usbfs_dentry
);
705 dev
->usbfs_dentry
= NULL
;
707 while (!list_empty(&dev
->filelist
)) {
708 ds
= list_entry(dev
->filelist
.next
, struct dev_state
, list
);
709 wake_up_all(&ds
->wait
);
710 list_del_init(&ds
->list
);
712 sinfo
.si_signo
= SIGPIPE
;
713 sinfo
.si_errno
= EPIPE
;
714 sinfo
.si_code
= SI_ASYNCIO
;
715 sinfo
.si_addr
= ds
->disccontext
;
716 send_sig_info(ds
->discsignr
, &sinfo
, ds
->disctask
);
719 usbfs_update_special();
720 usbfs_conn_disc_event();
723 /* --------------------------------------------------------------------- */
725 static struct proc_dir_entry
*usbdir
= NULL
;
727 int __init
usbfs_init(void)
731 retval
= usb_register(&usbfs_driver
);
735 retval
= register_filesystem(&usb_fs_type
);
737 usb_deregister(&usbfs_driver
);
741 /* create mount point for usbfs */
742 usbdir
= proc_mkdir("usb", proc_bus
);
747 void usbfs_cleanup(void)
749 usb_deregister(&usbfs_driver
);
750 unregister_filesystem(&usb_fs_type
);
752 remove_proc_entry("usb", proc_bus
);