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 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>
44 static struct super_operations usbfs_ops;
45 static struct file_operations default_file_operations;
46 static struct inode_operations usbfs_dir_inode_operations;
47 static struct vfsmount *usbdevfs_mount;
48 static struct vfsmount *usbfs_mount;
49 static int usbdevfs_mount_count; /* = 0 */
50 static int usbfs_mount_count; /* = 0 */
51 static int ignore_mount = 0;
53 static struct dentry *devices_usbdevfs_dentry;
54 static struct dentry *devices_usbfs_dentry;
55 static int num_buses; /* = 0 */
57 static uid_t devuid; /* = 0 */
58 static uid_t busuid; /* = 0 */
59 static uid_t listuid; /* = 0 */
60 static gid_t devgid; /* = 0 */
61 static gid_t busgid; /* = 0 */
62 static gid_t listgid; /* = 0 */
63 static umode_t devmode = S_IWUSR | S_IRUGO;
64 static umode_t busmode = S_IXUGO | S_IRUGO;
65 static umode_t listmode = S_IRUGO;
68 Opt_devuid, Opt_devgid, Opt_devmode,
69 Opt_busuid, Opt_busgid, Opt_busmode,
70 Opt_listuid, Opt_listgid, Opt_listmode,
74 static match_table_t tokens = {
75 {Opt_devuid, "devuid=%u"},
76 {Opt_devgid, "devgid=%u"},
77 {Opt_devmode, "devmode=%o"},
78 {Opt_busuid, "busuid=%u"},
79 {Opt_busgid, "busgid=%u"},
80 {Opt_busmode, "busmode=%o"},
81 {Opt_listuid, "listuid=%u"},
82 {Opt_listgid, "listgid=%u"},
83 {Opt_listmode, "listmode=%o"},
87 static int parse_options(struct super_block *s, char *data)
92 /* (re)set to defaults. */
99 devmode = S_IWUSR | S_IRUGO;
100 busmode = S_IXUGO | S_IRUGO;
103 while ((p = strsep(&data, ",")) != NULL) {
104 substring_t args[MAX_OPT_ARGS];
109 token = match_token(p, tokens, args);
112 if (match_int(&args[0], &option))
117 if (match_int(&args[0], &option))
122 if (match_octal(&args[0], &option))
124 devmode = option & S_IRWXUGO;
127 if (match_int(&args[0], &option))
132 if (match_int(&args[0], &option))
137 if (match_octal(&args[0], &option))
139 busmode = option & S_IRWXUGO;
142 if (match_int(&args[0], &option))
147 if (match_int(&args[0], &option))
152 if (match_octal(&args[0], &option))
154 listmode = option & S_IRWXUGO;
157 err("usbfs: unrecognised mount option \"%s\" "
158 "or missing value\n", p);
166 static void update_special(struct dentry *special)
168 special->d_inode->i_uid = listuid;
169 special->d_inode->i_gid = listgid;
170 special->d_inode->i_mode = S_IFREG | listmode;
173 static void update_dev(struct dentry *dev)
175 dev->d_inode->i_uid = devuid;
176 dev->d_inode->i_gid = devgid;
177 dev->d_inode->i_mode = S_IFREG | devmode;
180 static void update_bus(struct dentry *bus)
182 struct dentry *dev = NULL;
184 bus->d_inode->i_uid = busuid;
185 bus->d_inode->i_gid = busgid;
186 bus->d_inode->i_mode = S_IFDIR | busmode;
188 down(&bus->d_inode->i_sem);
190 list_for_each_entry(dev, &bus->d_subdirs, d_child)
194 up(&bus->d_inode->i_sem);
197 static void update_sb(struct super_block *sb)
199 struct dentry *root = sb->s_root;
200 struct dentry *bus = NULL;
205 down(&root->d_inode->i_sem);
207 list_for_each_entry(bus, &root->d_subdirs, d_child) {
209 switch (S_IFMT & bus->d_inode->i_mode) {
217 warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode);
223 up(&root->d_inode->i_sem);
226 static int remount(struct super_block *sb, int *flags, char *data)
228 /* If this is not a real mount,
229 * i.e. it's a simple_pin_fs from create_special_files,
235 if (parse_options(sb, data)) {
236 warn("usbfs: mount parameter error:");
240 if (usbfs_mount && usbfs_mount->mnt_sb)
241 update_sb(usbfs_mount->mnt_sb);
243 if (usbdevfs_mount && usbdevfs_mount->mnt_sb)
244 update_sb(usbdevfs_mount->mnt_sb);
249 static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
251 struct inode *inode = new_inode(sb);
254 inode->i_mode = mode;
255 inode->i_uid = current->fsuid;
256 inode->i_gid = current->fsgid;
257 inode->i_blksize = PAGE_CACHE_SIZE;
259 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
260 switch (mode & S_IFMT) {
262 init_special_inode(inode, mode, dev);
265 inode->i_fop = &default_file_operations;
268 inode->i_op = &usbfs_dir_inode_operations;
269 inode->i_fop = &simple_dir_operations;
271 /* directory inodes start off with i_nlink == 2 (for "." entry) */
280 static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
283 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
290 d_instantiate(dentry, inode);
297 static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
301 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
302 res = usbfs_mknod (dir, dentry, mode, 0);
308 static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
310 mode = (mode & S_IALLUGO) | S_IFREG;
311 return usbfs_mknod (dir, dentry, mode, 0);
314 static inline int usbfs_positive (struct dentry *dentry)
316 return dentry->d_inode && !d_unhashed(dentry);
319 static int usbfs_empty (struct dentry *dentry)
321 struct list_head *list;
323 spin_lock(&dcache_lock);
325 list_for_each(list, &dentry->d_subdirs) {
326 struct dentry *de = list_entry(list, struct dentry, d_child);
327 if (usbfs_positive(de)) {
328 spin_unlock(&dcache_lock);
333 spin_unlock(&dcache_lock);
337 static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
339 struct inode *inode = dentry->d_inode;
341 dentry->d_inode->i_nlink--;
348 static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
350 int error = -ENOTEMPTY;
351 struct inode * inode = dentry->d_inode;
354 dentry_unhash(dentry);
355 if (usbfs_empty(dentry)) {
356 dentry->d_inode->i_nlink -= 2;
358 inode->i_flags |= S_DEAD;
370 /* default file operations */
371 static ssize_t default_read_file (struct file *file, char __user *buf,
372 size_t count, loff_t *ppos)
377 static ssize_t default_write_file (struct file *file, const char __user *buf,
378 size_t count, loff_t *ppos)
383 static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
385 loff_t retval = -EINVAL;
387 down(&file->f_dentry->d_inode->i_sem);
391 file->f_pos = offset;
392 retval = file->f_pos;
396 if ((offset + file->f_pos) > 0) {
397 file->f_pos += offset;
398 retval = file->f_pos;
404 up(&file->f_dentry->d_inode->i_sem);
408 static int default_open (struct inode *inode, struct file *file)
410 if (inode->u.generic_ip)
411 file->private_data = inode->u.generic_ip;
416 static struct file_operations default_file_operations = {
417 .read = default_read_file,
418 .write = default_write_file,
419 .open = default_open,
420 .llseek = default_file_lseek,
423 static struct inode_operations usbfs_dir_inode_operations = {
424 .lookup = simple_lookup,
427 static struct super_operations usbfs_ops = {
428 .statfs = simple_statfs,
429 .drop_inode = generic_delete_inode,
430 .remount_fs = remount,
433 static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
438 sb->s_blocksize = PAGE_CACHE_SIZE;
439 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
440 sb->s_magic = USBDEVICE_SUPER_MAGIC;
441 sb->s_op = &usbfs_ops;
442 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
445 dbg("%s: could not get inode!",__FUNCTION__);
449 root = d_alloc_root(inode);
451 dbg("%s: could not get root dentry!",__FUNCTION__);
459 static struct dentry * get_dentry(struct dentry *parent, const char *name)
464 qstr.len = strlen(name);
465 qstr.hash = full_name_hash(name,qstr.len);
466 return lookup_hash(&qstr,parent);
471 * fs_create_by_name - create a file, given a name
472 * @name: name of file
473 * @mode: type of file
474 * @parent: dentry of directory to create it in
475 * @dentry: resulting dentry of file
477 * This function handles both regular files and directories.
479 static int fs_create_by_name (const char *name, mode_t mode,
480 struct dentry *parent, struct dentry **dentry)
484 /* If the parent is not specified, we create it in the root.
485 * We need the root dentry to do this, which is in the super
486 * block. A pointer to that is in the struct vfsmount that we
490 if (usbfs_mount && usbfs_mount->mnt_sb) {
491 parent = usbfs_mount->mnt_sb->s_root;
496 dbg("Ah! can not find a parent!");
501 down(&parent->d_inode->i_sem);
502 *dentry = get_dentry (parent, name);
503 if (!IS_ERR(dentry)) {
504 if ((mode & S_IFMT) == S_IFDIR)
505 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
507 error = usbfs_create (parent->d_inode, *dentry, mode);
509 error = PTR_ERR(dentry);
510 up(&parent->d_inode->i_sem);
515 static struct dentry *fs_create_file (const char *name, mode_t mode,
516 struct dentry *parent, void *data,
517 struct file_operations *fops,
518 uid_t uid, gid_t gid)
520 struct dentry *dentry;
523 dbg("creating file '%s'",name);
525 error = fs_create_by_name (name, mode, parent, &dentry);
529 if (dentry->d_inode) {
531 dentry->d_inode->u.generic_ip = data;
533 dentry->d_inode->i_fop = fops;
534 dentry->d_inode->i_uid = uid;
535 dentry->d_inode->i_gid = gid;
542 static void fs_remove_file (struct dentry *dentry)
544 struct dentry *parent = dentry->d_parent;
546 if (!parent || !parent->d_inode)
549 down(&parent->d_inode->i_sem);
550 if (usbfs_positive(dentry)) {
551 if (dentry->d_inode) {
552 if (S_ISDIR(dentry->d_inode->i_mode))
553 usbfs_rmdir(parent->d_inode, dentry);
555 usbfs_unlink(parent->d_inode, dentry);
559 up(&parent->d_inode->i_sem);
562 /* --------------------------------------------------------------------- */
567 * The usbdevfs name is now deprecated (as of 2.5.1).
568 * It will be removed when the 2.7.x development cycle is started.
569 * You have been warned :)
571 static struct file_system_type usbdevice_fs_type;
573 static struct super_block *usb_get_sb(struct file_system_type *fs_type,
574 int flags, const char *dev_name, void *data)
576 return get_sb_single(fs_type, flags, data, usbfs_fill_super);
579 static struct file_system_type usbdevice_fs_type = {
580 .owner = THIS_MODULE,
582 .get_sb = usb_get_sb,
583 .kill_sb = kill_litter_super,
586 static struct file_system_type usb_fs_type = {
587 .owner = THIS_MODULE,
589 .get_sb = usb_get_sb,
590 .kill_sb = kill_litter_super,
593 /* --------------------------------------------------------------------- */
595 static int create_special_files (void)
597 struct dentry *parent;
600 /* the simple_pin_fs calls will call remount with no options
601 * without this flag that would overwrite the real mount options (if any)
605 /* create the devices special file */
606 retval = simple_pin_fs("usbdevfs", &usbdevfs_mount, &usbdevfs_mount_count);
608 err ("Unable to get usbdevfs mount");
612 retval = simple_pin_fs("usbfs", &usbfs_mount, &usbfs_mount_count);
614 err ("Unable to get usbfs mount");
615 goto error_clean_usbdevfs_mount;
620 parent = usbfs_mount->mnt_sb->s_root;
621 devices_usbfs_dentry = fs_create_file ("devices",
622 listmode | S_IFREG, parent,
623 NULL, &usbdevfs_devices_fops,
625 if (devices_usbfs_dentry == NULL) {
626 err ("Unable to create devices usbfs file");
628 goto error_clean_mounts;
631 parent = usbdevfs_mount->mnt_sb->s_root;
632 devices_usbdevfs_dentry = fs_create_file ("devices",
633 listmode | S_IFREG, parent,
634 NULL, &usbdevfs_devices_fops,
636 if (devices_usbdevfs_dentry == NULL) {
637 err ("Unable to create devices usbfs file");
639 goto error_remove_file;
645 fs_remove_file (devices_usbfs_dentry);
646 devices_usbfs_dentry = NULL;
649 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
651 error_clean_usbdevfs_mount:
652 simple_release_fs(&usbdevfs_mount, &usbdevfs_mount_count);
658 static void remove_special_files (void)
660 if (devices_usbdevfs_dentry)
661 fs_remove_file (devices_usbdevfs_dentry);
662 if (devices_usbfs_dentry)
663 fs_remove_file (devices_usbfs_dentry);
664 devices_usbdevfs_dentry = NULL;
665 devices_usbfs_dentry = NULL;
666 simple_release_fs(&usbdevfs_mount, &usbdevfs_mount_count);
667 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
670 void usbfs_update_special (void)
674 if (devices_usbdevfs_dentry) {
675 inode = devices_usbdevfs_dentry->d_inode;
677 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
679 if (devices_usbfs_dentry) {
680 inode = devices_usbfs_dentry->d_inode;
682 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
686 void usbfs_add_bus(struct usb_bus *bus)
688 struct dentry *parent;
692 /* create the special files if this is the first bus added */
693 if (num_buses == 0) {
694 retval = create_special_files();
700 sprintf (name, "%03d", bus->busnum);
702 parent = usbfs_mount->mnt_sb->s_root;
703 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
704 bus, NULL, busuid, busgid);
705 if (bus->usbfs_dentry == NULL) {
706 err ("error creating usbfs bus entry");
710 parent = usbdevfs_mount->mnt_sb->s_root;
711 bus->usbdevfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
712 bus, NULL, busuid, busgid);
713 if (bus->usbdevfs_dentry == NULL) {
714 err ("error creating usbdevfs bus entry");
718 usbfs_update_special();
719 usbdevfs_conn_disc_event();
723 void usbfs_remove_bus(struct usb_bus *bus)
725 if (bus->usbfs_dentry) {
726 fs_remove_file (bus->usbfs_dentry);
727 bus->usbfs_dentry = NULL;
729 if (bus->usbdevfs_dentry) {
730 fs_remove_file (bus->usbdevfs_dentry);
731 bus->usbdevfs_dentry = NULL;
735 if (num_buses <= 0) {
736 remove_special_files();
740 usbfs_update_special();
741 usbdevfs_conn_disc_event();
744 void usbfs_add_device(struct usb_device *dev)
750 sprintf (name, "%03d", dev->devnum);
751 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
752 dev->bus->usbfs_dentry, dev,
753 &usbdevfs_device_file_operations,
755 if (dev->usbfs_dentry == NULL) {
756 err ("error creating usbfs device entry");
759 dev->usbdevfs_dentry = fs_create_file (name, devmode | S_IFREG,
760 dev->bus->usbdevfs_dentry, dev,
761 &usbdevfs_device_file_operations,
763 if (dev->usbdevfs_dentry == NULL) {
764 err ("error creating usbdevfs device entry");
768 /* Set the size of the device's file to be
769 * equal to the size of the device descriptors. */
770 i_size = sizeof (struct usb_device_descriptor);
771 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
772 struct usb_config_descriptor *config =
773 (struct usb_config_descriptor *)dev->rawdescriptors[i];
774 i_size += le16_to_cpu (config->wTotalLength);
776 if (dev->usbfs_dentry->d_inode)
777 dev->usbfs_dentry->d_inode->i_size = i_size;
778 if (dev->usbdevfs_dentry->d_inode)
779 dev->usbdevfs_dentry->d_inode->i_size = i_size;
781 usbfs_update_special();
782 usbdevfs_conn_disc_event();
785 void usbfs_remove_device(struct usb_device *dev)
787 struct dev_state *ds;
788 struct siginfo sinfo;
790 if (dev->usbfs_dentry) {
791 fs_remove_file (dev->usbfs_dentry);
792 dev->usbfs_dentry = NULL;
794 if (dev->usbdevfs_dentry) {
795 fs_remove_file (dev->usbdevfs_dentry);
796 dev->usbdevfs_dentry = NULL;
798 while (!list_empty(&dev->filelist)) {
799 ds = list_entry(dev->filelist.next, struct dev_state, list);
800 list_del_init(&ds->list);
802 sinfo.si_signo = SIGPIPE;
803 sinfo.si_errno = EPIPE;
804 sinfo.si_code = SI_ASYNCIO;
805 sinfo.si_addr = ds->disccontext;
806 send_sig_info(ds->discsignr, &sinfo, ds->disctask);
809 usbfs_update_special();
810 usbdevfs_conn_disc_event();
813 /* --------------------------------------------------------------------- */
815 #ifdef CONFIG_PROC_FS
816 static struct proc_dir_entry *usbdir = NULL;
819 int __init usbfs_init(void)
823 retval = usb_register(&usbdevfs_driver);
827 retval = register_filesystem(&usb_fs_type);
829 usb_deregister(&usbdevfs_driver);
832 retval = register_filesystem(&usbdevice_fs_type);
834 unregister_filesystem(&usb_fs_type);
835 usb_deregister(&usbdevfs_driver);
839 #ifdef CONFIG_PROC_FS
840 /* create mount point for usbdevfs */
841 usbdir = proc_mkdir("usb", proc_bus);
847 void usbfs_cleanup(void)
849 usb_deregister(&usbdevfs_driver);
850 unregister_filesystem(&usb_fs_type);
851 unregister_filesystem(&usbdevice_fs_type);
852 #ifdef CONFIG_PROC_FS
854 remove_proc_entry("usb", proc_bus);