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 */
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 = USBFS_DEFAULT_DEVMODE;
64 static umode_t busmode = USBFS_DEFAULT_BUSMODE;
65 static umode_t listmode = USBFS_DEFAULT_LISTMODE;
67 static int usbfs_show_options(struct seq_file *seq, struct dentry *root)
70 seq_printf(seq, ",devuid=%u", devuid);
72 seq_printf(seq, ",devgid=%u", devgid);
73 if (devmode != USBFS_DEFAULT_DEVMODE)
74 seq_printf(seq, ",devmode=%o", devmode);
76 seq_printf(seq, ",busuid=%u", busuid);
78 seq_printf(seq, ",busgid=%u", busgid);
79 if (busmode != USBFS_DEFAULT_BUSMODE)
80 seq_printf(seq, ",busmode=%o", busmode);
82 seq_printf(seq, ",listuid=%u", listuid);
84 seq_printf(seq, ",listgid=%u", listgid);
85 if (listmode != USBFS_DEFAULT_LISTMODE)
86 seq_printf(seq, ",listmode=%o", listmode);
92 Opt_devuid, Opt_devgid, Opt_devmode,
93 Opt_busuid, Opt_busgid, Opt_busmode,
94 Opt_listuid, Opt_listgid, Opt_listmode,
98 static const match_table_t tokens = {
99 {Opt_devuid, "devuid=%u"},
100 {Opt_devgid, "devgid=%u"},
101 {Opt_devmode, "devmode=%o"},
102 {Opt_busuid, "busuid=%u"},
103 {Opt_busgid, "busgid=%u"},
104 {Opt_busmode, "busmode=%o"},
105 {Opt_listuid, "listuid=%u"},
106 {Opt_listgid, "listgid=%u"},
107 {Opt_listmode, "listmode=%o"},
111 static int parse_options(struct super_block *s, char *data)
116 /* (re)set to defaults. */
123 devmode = USBFS_DEFAULT_DEVMODE;
124 busmode = USBFS_DEFAULT_BUSMODE;
125 listmode = USBFS_DEFAULT_LISTMODE;
127 while ((p = strsep(&data, ",")) != NULL) {
128 substring_t args[MAX_OPT_ARGS];
133 token = match_token(p, tokens, args);
136 if (match_int(&args[0], &option))
141 if (match_int(&args[0], &option))
146 if (match_octal(&args[0], &option))
148 devmode = option & S_IRWXUGO;
151 if (match_int(&args[0], &option))
156 if (match_int(&args[0], &option))
161 if (match_octal(&args[0], &option))
163 busmode = option & S_IRWXUGO;
166 if (match_int(&args[0], &option))
171 if (match_int(&args[0], &option))
176 if (match_octal(&args[0], &option))
178 listmode = option & S_IRWXUGO;
181 printk(KERN_ERR "usbfs: unrecognised mount option "
182 "\"%s\" or missing value\n", p);
190 static void update_special(struct dentry *special)
192 special->d_inode->i_uid = listuid;
193 special->d_inode->i_gid = listgid;
194 special->d_inode->i_mode = S_IFREG | listmode;
197 static void update_dev(struct dentry *dev)
199 dev->d_inode->i_uid = devuid;
200 dev->d_inode->i_gid = devgid;
201 dev->d_inode->i_mode = S_IFREG | devmode;
204 static void update_bus(struct dentry *bus)
206 struct dentry *dev = NULL;
208 bus->d_inode->i_uid = busuid;
209 bus->d_inode->i_gid = busgid;
210 bus->d_inode->i_mode = S_IFDIR | busmode;
212 mutex_lock(&bus->d_inode->i_mutex);
214 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
218 mutex_unlock(&bus->d_inode->i_mutex);
221 static void update_sb(struct super_block *sb)
223 struct dentry *root = sb->s_root;
224 struct dentry *bus = NULL;
229 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
231 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
233 switch (S_IFMT & bus->d_inode->i_mode) {
241 printk(KERN_WARNING "usbfs: Unknown node %s "
242 "mode %x found on remount!\n",
243 bus->d_name.name, bus->d_inode->i_mode);
249 mutex_unlock(&root->d_inode->i_mutex);
252 static int remount(struct super_block *sb, int *flags, char *data)
254 /* If this is not a real mount,
255 * i.e. it's a simple_pin_fs from create_special_files,
258 if (*flags & MS_KERNMOUNT)
261 if (parse_options(sb, data)) {
262 printk(KERN_WARNING "usbfs: mount parameter error.\n");
267 update_sb(usbfs_mount->mnt_sb);
272 static struct inode *usbfs_get_inode (struct super_block *sb, umode_t mode, dev_t dev)
274 struct inode *inode = new_inode(sb);
277 inode->i_ino = get_next_ino();
278 inode_init_owner(inode, NULL, mode);
279 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
280 switch (mode & S_IFMT) {
282 init_special_inode(inode, mode, dev);
285 inode->i_fop = &default_file_operations;
288 inode->i_op = &simple_dir_inode_operations;
289 inode->i_fop = &simple_dir_operations;
291 /* directory inodes start off with i_nlink == 2 (for "." entry) */
300 static int usbfs_mknod (struct inode *dir, struct dentry *dentry, umode_t mode,
303 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
310 d_instantiate(dentry, inode);
317 static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, umode_t mode)
321 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
322 res = usbfs_mknod (dir, dentry, mode, 0);
328 static int usbfs_create (struct inode *dir, struct dentry *dentry, umode_t mode)
330 mode = (mode & S_IALLUGO) | S_IFREG;
331 return usbfs_mknod (dir, dentry, mode, 0);
334 static inline int usbfs_positive (struct dentry *dentry)
336 return dentry->d_inode && !d_unhashed(dentry);
339 static int usbfs_empty (struct dentry *dentry)
341 struct list_head *list;
343 spin_lock(&dentry->d_lock);
344 list_for_each(list, &dentry->d_subdirs) {
345 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
347 spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
348 if (usbfs_positive(de)) {
349 spin_unlock(&de->d_lock);
350 spin_unlock(&dentry->d_lock);
353 spin_unlock(&de->d_lock);
355 spin_unlock(&dentry->d_lock);
359 static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
361 struct inode *inode = dentry->d_inode;
362 mutex_lock(&inode->i_mutex);
363 drop_nlink(dentry->d_inode);
365 mutex_unlock(&inode->i_mutex);
370 static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
372 int error = -ENOTEMPTY;
373 struct inode * inode = dentry->d_inode;
375 mutex_lock(&inode->i_mutex);
376 dentry_unhash(dentry);
377 if (usbfs_empty(dentry)) {
379 drop_nlink(dentry->d_inode);
380 drop_nlink(dentry->d_inode);
382 inode->i_flags |= S_DEAD;
386 mutex_unlock(&inode->i_mutex);
393 /* default file operations */
394 static ssize_t default_read_file (struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
400 static ssize_t default_write_file (struct file *file, const char __user *buf,
401 size_t count, loff_t *ppos)
406 static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
408 loff_t retval = -EINVAL;
410 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
414 file->f_pos = offset;
415 retval = file->f_pos;
419 if ((offset + file->f_pos) > 0) {
420 file->f_pos += offset;
421 retval = file->f_pos;
427 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
431 static int default_open (struct inode *inode, struct file *file)
433 if (inode->i_private)
434 file->private_data = inode->i_private;
439 static const struct file_operations default_file_operations = {
440 .read = default_read_file,
441 .write = default_write_file,
442 .open = default_open,
443 .llseek = default_file_lseek,
446 static const struct super_operations usbfs_ops = {
447 .statfs = simple_statfs,
448 .drop_inode = generic_delete_inode,
449 .remount_fs = remount,
450 .show_options = usbfs_show_options,
453 static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
457 sb->s_blocksize = PAGE_CACHE_SIZE;
458 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
459 sb->s_magic = USBDEVICE_SUPER_MAGIC;
460 sb->s_op = &usbfs_ops;
462 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
463 sb->s_root = d_make_root(inode);
465 dbg("%s: could not get root dentry!",__func__);
472 * fs_create_by_name - create a file, given a name
473 * @name: name of file
474 * @mode: type of file
475 * @parent: dentry of directory to create it in
476 * @dentry: resulting dentry of file
478 * This function handles both regular files and directories.
480 static int fs_create_by_name (const char *name, umode_t mode,
481 struct dentry *parent, struct dentry **dentry)
485 /* If the parent is not specified, we create it in the root.
486 * We need the root dentry to do this, which is in the super
487 * block. A pointer to that is in the struct vfsmount that we
492 parent = usbfs_mount->mnt_root;
496 dbg("Ah! can not find a parent!");
501 mutex_lock(&parent->d_inode->i_mutex);
502 *dentry = lookup_one_len(name, parent, strlen(name));
503 if (!IS_ERR(*dentry)) {
505 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
507 error = usbfs_create (parent->d_inode, *dentry, mode);
509 error = PTR_ERR(*dentry);
510 mutex_unlock(&parent->d_inode->i_mutex);
515 static struct dentry *fs_create_file (const char *name, umode_t mode,
516 struct dentry *parent, void *data,
517 const 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->i_private = 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 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
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 mutex_unlock(&parent->d_inode->i_mutex);
562 /* --------------------------------------------------------------------- */
564 static struct dentry *usb_mount(struct file_system_type *fs_type,
565 int flags, const char *dev_name, void *data)
567 return mount_single(fs_type, flags, data, usbfs_fill_super);
570 static struct file_system_type usb_fs_type = {
571 .owner = THIS_MODULE,
574 .kill_sb = kill_litter_super,
577 /* --------------------------------------------------------------------- */
579 static int create_special_files (void)
581 struct dentry *parent;
584 /* create the devices special file */
585 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
587 printk(KERN_ERR "Unable to get usbfs mount\n");
591 parent = usbfs_mount->mnt_root;
592 devices_usbfs_dentry = fs_create_file ("devices",
593 listmode | S_IFREG, parent,
594 NULL, &usbfs_devices_fops,
596 if (devices_usbfs_dentry == NULL) {
597 printk(KERN_ERR "Unable to create devices usbfs file\n");
599 goto error_clean_mounts;
605 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
610 static void remove_special_files (void)
612 if (devices_usbfs_dentry)
613 fs_remove_file (devices_usbfs_dentry);
614 devices_usbfs_dentry = NULL;
615 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
618 void usbfs_update_special (void)
622 if (devices_usbfs_dentry) {
623 inode = devices_usbfs_dentry->d_inode;
625 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
629 static void usbfs_add_bus(struct usb_bus *bus)
631 struct dentry *parent;
635 /* create the special files if this is the first bus added */
636 if (num_buses == 0) {
637 retval = create_special_files();
643 sprintf (name, "%03d", bus->busnum);
645 parent = usbfs_mount->mnt_root;
646 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
647 bus, NULL, busuid, busgid);
648 if (bus->usbfs_dentry == NULL) {
649 printk(KERN_ERR "Error creating usbfs bus entry\n");
654 static void usbfs_remove_bus(struct usb_bus *bus)
656 if (bus->usbfs_dentry) {
657 fs_remove_file (bus->usbfs_dentry);
658 bus->usbfs_dentry = NULL;
662 if (num_buses <= 0) {
663 remove_special_files();
668 static void usbfs_add_device(struct usb_device *dev)
674 sprintf (name, "%03d", dev->devnum);
675 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
676 dev->bus->usbfs_dentry, dev,
677 &usbdev_file_operations,
679 if (dev->usbfs_dentry == NULL) {
680 printk(KERN_ERR "Error creating usbfs device entry\n");
684 /* Set the size of the device's file to be
685 * equal to the size of the device descriptors. */
686 i_size = sizeof (struct usb_device_descriptor);
687 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
688 struct usb_config_descriptor *config =
689 (struct usb_config_descriptor *)dev->rawdescriptors[i];
690 i_size += le16_to_cpu(config->wTotalLength);
692 if (dev->usbfs_dentry->d_inode)
693 dev->usbfs_dentry->d_inode->i_size = i_size;
696 static void usbfs_remove_device(struct usb_device *dev)
698 if (dev->usbfs_dentry) {
699 fs_remove_file (dev->usbfs_dentry);
700 dev->usbfs_dentry = NULL;
704 static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
708 usbfs_add_device(dev);
710 case USB_DEVICE_REMOVE:
711 usbfs_remove_device(dev);
717 usbfs_remove_bus(dev);
720 usbfs_update_special();
721 usbfs_conn_disc_event();
725 static struct notifier_block usbfs_nb = {
726 .notifier_call = usbfs_notify,
729 /* --------------------------------------------------------------------- */
731 static struct proc_dir_entry *usbdir = NULL;
733 int __init usbfs_init(void)
737 retval = register_filesystem(&usb_fs_type);
741 usb_register_notify(&usbfs_nb);
743 /* create mount point for usbfs */
744 usbdir = proc_mkdir("bus/usb", NULL);
749 void usbfs_cleanup(void)
751 usb_unregister_notify(&usbfs_nb);
752 unregister_filesystem(&usb_fs_type);
754 remove_proc_entry("bus/usb", NULL);