2005-04-17 06:20:36 +08:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inode.c -- Inode/Dentry functions for the USB device file system.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
|
|
|
|
* Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
* 0.1 04.01.2000 Created
|
|
|
|
* 0.2 10.12.2001 converted to use the vfs layer better
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/usb.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
#include <linux/parser.h>
|
2005-06-21 12:15:16 +08:00
|
|
|
#include <linux/notifier.h>
|
2008-01-25 03:34:07 +08:00
|
|
|
#include <linux/seq_file.h>
|
2010-04-25 05:21:52 +08:00
|
|
|
#include <linux/usb/hcd.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include "usb.h"
|
|
|
|
|
2008-01-25 03:34:07 +08:00
|
|
|
#define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
|
|
|
|
#define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
|
|
|
|
#define USBFS_DEFAULT_LISTMODE S_IRUGO
|
|
|
|
|
2006-08-06 07:37:11 +08:00
|
|
|
static const struct file_operations default_file_operations;
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct vfsmount *usbfs_mount;
|
|
|
|
static int usbfs_mount_count; /* = 0 */
|
|
|
|
|
|
|
|
static struct dentry *devices_usbfs_dentry;
|
|
|
|
static int num_buses; /* = 0 */
|
|
|
|
|
|
|
|
static uid_t devuid; /* = 0 */
|
|
|
|
static uid_t busuid; /* = 0 */
|
|
|
|
static uid_t listuid; /* = 0 */
|
|
|
|
static gid_t devgid; /* = 0 */
|
|
|
|
static gid_t busgid; /* = 0 */
|
|
|
|
static gid_t listgid; /* = 0 */
|
2008-01-25 03:34:07 +08:00
|
|
|
static umode_t devmode = USBFS_DEFAULT_DEVMODE;
|
|
|
|
static umode_t busmode = USBFS_DEFAULT_BUSMODE;
|
|
|
|
static umode_t listmode = USBFS_DEFAULT_LISTMODE;
|
|
|
|
|
2011-12-09 10:32:45 +08:00
|
|
|
static int usbfs_show_options(struct seq_file *seq, struct dentry *root)
|
2008-01-25 03:34:07 +08:00
|
|
|
{
|
|
|
|
if (devuid != 0)
|
|
|
|
seq_printf(seq, ",devuid=%u", devuid);
|
|
|
|
if (devgid != 0)
|
|
|
|
seq_printf(seq, ",devgid=%u", devgid);
|
|
|
|
if (devmode != USBFS_DEFAULT_DEVMODE)
|
|
|
|
seq_printf(seq, ",devmode=%o", devmode);
|
|
|
|
if (busuid != 0)
|
|
|
|
seq_printf(seq, ",busuid=%u", busuid);
|
|
|
|
if (busgid != 0)
|
|
|
|
seq_printf(seq, ",busgid=%u", busgid);
|
|
|
|
if (busmode != USBFS_DEFAULT_BUSMODE)
|
|
|
|
seq_printf(seq, ",busmode=%o", busmode);
|
|
|
|
if (listuid != 0)
|
|
|
|
seq_printf(seq, ",listuid=%u", listuid);
|
|
|
|
if (listgid != 0)
|
|
|
|
seq_printf(seq, ",listgid=%u", listgid);
|
|
|
|
if (listmode != USBFS_DEFAULT_LISTMODE)
|
|
|
|
seq_printf(seq, ",listmode=%o", listmode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
Opt_devuid, Opt_devgid, Opt_devmode,
|
|
|
|
Opt_busuid, Opt_busgid, Opt_busmode,
|
|
|
|
Opt_listuid, Opt_listgid, Opt_listmode,
|
|
|
|
Opt_err,
|
|
|
|
};
|
|
|
|
|
2008-10-13 17:46:57 +08:00
|
|
|
static const match_table_t tokens = {
|
2005-04-17 06:20:36 +08:00
|
|
|
{Opt_devuid, "devuid=%u"},
|
|
|
|
{Opt_devgid, "devgid=%u"},
|
|
|
|
{Opt_devmode, "devmode=%o"},
|
|
|
|
{Opt_busuid, "busuid=%u"},
|
|
|
|
{Opt_busgid, "busgid=%u"},
|
|
|
|
{Opt_busmode, "busmode=%o"},
|
|
|
|
{Opt_listuid, "listuid=%u"},
|
|
|
|
{Opt_listgid, "listgid=%u"},
|
|
|
|
{Opt_listmode, "listmode=%o"},
|
|
|
|
{Opt_err, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int parse_options(struct super_block *s, char *data)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int option;
|
|
|
|
|
|
|
|
/* (re)set to defaults. */
|
|
|
|
devuid = 0;
|
|
|
|
busuid = 0;
|
|
|
|
listuid = 0;
|
|
|
|
devgid = 0;
|
|
|
|
busgid = 0;
|
|
|
|
listgid = 0;
|
2008-01-25 03:34:07 +08:00
|
|
|
devmode = USBFS_DEFAULT_DEVMODE;
|
|
|
|
busmode = USBFS_DEFAULT_BUSMODE;
|
|
|
|
listmode = USBFS_DEFAULT_LISTMODE;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
while ((p = strsep(&data, ",")) != NULL) {
|
|
|
|
substring_t args[MAX_OPT_ARGS];
|
|
|
|
int token;
|
|
|
|
if (!*p)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
token = match_token(p, tokens, args);
|
|
|
|
switch (token) {
|
|
|
|
case Opt_devuid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
devuid = option;
|
|
|
|
break;
|
|
|
|
case Opt_devgid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
devgid = option;
|
|
|
|
break;
|
|
|
|
case Opt_devmode:
|
|
|
|
if (match_octal(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
devmode = option & S_IRWXUGO;
|
|
|
|
break;
|
|
|
|
case Opt_busuid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
busuid = option;
|
|
|
|
break;
|
|
|
|
case Opt_busgid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
busgid = option;
|
|
|
|
break;
|
|
|
|
case Opt_busmode:
|
|
|
|
if (match_octal(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
busmode = option & S_IRWXUGO;
|
|
|
|
break;
|
|
|
|
case Opt_listuid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
listuid = option;
|
|
|
|
break;
|
|
|
|
case Opt_listgid:
|
|
|
|
if (match_int(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
listgid = option;
|
|
|
|
break;
|
|
|
|
case Opt_listmode:
|
|
|
|
if (match_octal(&args[0], &option))
|
|
|
|
return -EINVAL;
|
|
|
|
listmode = option & S_IRWXUGO;
|
|
|
|
break;
|
|
|
|
default:
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_ERR "usbfs: unrecognised mount option "
|
|
|
|
"\"%s\" or missing value\n", p);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_special(struct dentry *special)
|
|
|
|
{
|
|
|
|
special->d_inode->i_uid = listuid;
|
|
|
|
special->d_inode->i_gid = listgid;
|
|
|
|
special->d_inode->i_mode = S_IFREG | listmode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_dev(struct dentry *dev)
|
|
|
|
{
|
|
|
|
dev->d_inode->i_uid = devuid;
|
|
|
|
dev->d_inode->i_gid = devgid;
|
|
|
|
dev->d_inode->i_mode = S_IFREG | devmode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_bus(struct dentry *bus)
|
|
|
|
{
|
|
|
|
struct dentry *dev = NULL;
|
|
|
|
|
|
|
|
bus->d_inode->i_uid = busuid;
|
|
|
|
bus->d_inode->i_gid = busgid;
|
|
|
|
bus->d_inode->i_mode = S_IFDIR | busmode;
|
|
|
|
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_lock(&bus->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
[PATCH] shrink dentry struct
Some long time ago, dentry struct was carefully tuned so that on 32 bits
UP, sizeof(struct dentry) was exactly 128, ie a power of 2, and a multiple
of memory cache lines.
Then RCU was added and dentry struct enlarged by two pointers, with nice
results for SMP, but not so good on UP, because breaking the above tuning
(128 + 8 = 136 bytes)
This patch reverts this unwanted side effect, by using an union (d_u),
where d_rcu and d_child are placed so that these two fields can share their
memory needs.
At the time d_free() is called (and d_rcu is really used), d_child is known
to be empty and not touched by the dentry freeing.
Lockless lookups only access d_name, d_parent, d_lock, d_op, d_flags (so
the previous content of d_child is not needed if said dentry was unhashed
but still accessed by a CPU because of RCU constraints)
As dentry cache easily contains millions of entries, a size reduction is
worth the extra complexity of the ugly C union.
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Cc: Dipankar Sarma <dipankar@in.ibm.com>
Cc: Maneesh Soni <maneesh@in.ibm.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: "Paul E. McKenney" <paulmck@us.ibm.com>
Cc: Ian Kent <raven@themaw.net>
Cc: Paul Jackson <pj@sgi.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Trond Myklebust <trond.myklebust@fys.uio.no>
Cc: Neil Brown <neilb@cse.unsw.edu.au>
Cc: James Morris <jmorris@namei.org>
Cc: Stephen Smalley <sds@epoch.ncsc.mil>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-08 17:03:32 +08:00
|
|
|
list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (dev->d_inode)
|
|
|
|
update_dev(dev);
|
|
|
|
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&bus->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_sb(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct dentry *root = sb->s_root;
|
|
|
|
struct dentry *bus = NULL;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return;
|
|
|
|
|
2006-07-03 15:25:05 +08:00
|
|
|
mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
[PATCH] shrink dentry struct
Some long time ago, dentry struct was carefully tuned so that on 32 bits
UP, sizeof(struct dentry) was exactly 128, ie a power of 2, and a multiple
of memory cache lines.
Then RCU was added and dentry struct enlarged by two pointers, with nice
results for SMP, but not so good on UP, because breaking the above tuning
(128 + 8 = 136 bytes)
This patch reverts this unwanted side effect, by using an union (d_u),
where d_rcu and d_child are placed so that these two fields can share their
memory needs.
At the time d_free() is called (and d_rcu is really used), d_child is known
to be empty and not touched by the dentry freeing.
Lockless lookups only access d_name, d_parent, d_lock, d_op, d_flags (so
the previous content of d_child is not needed if said dentry was unhashed
but still accessed by a CPU because of RCU constraints)
As dentry cache easily contains millions of entries, a size reduction is
worth the extra complexity of the ugly C union.
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Cc: Dipankar Sarma <dipankar@in.ibm.com>
Cc: Maneesh Soni <maneesh@in.ibm.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: "Paul E. McKenney" <paulmck@us.ibm.com>
Cc: Ian Kent <raven@themaw.net>
Cc: Paul Jackson <pj@sgi.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Trond Myklebust <trond.myklebust@fys.uio.no>
Cc: Neil Brown <neilb@cse.unsw.edu.au>
Cc: James Morris <jmorris@namei.org>
Cc: Stephen Smalley <sds@epoch.ncsc.mil>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-08 17:03:32 +08:00
|
|
|
list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (bus->d_inode) {
|
|
|
|
switch (S_IFMT & bus->d_inode->i_mode) {
|
|
|
|
case S_IFDIR:
|
|
|
|
update_bus(bus);
|
|
|
|
break;
|
|
|
|
case S_IFREG:
|
|
|
|
update_special(bus);
|
|
|
|
break;
|
|
|
|
default:
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_WARNING "usbfs: Unknown node %s "
|
|
|
|
"mode %x found on remount!\n",
|
|
|
|
bus->d_name.name, bus->d_inode->i_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&root->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int remount(struct super_block *sb, int *flags, char *data)
|
|
|
|
{
|
|
|
|
/* If this is not a real mount,
|
|
|
|
* i.e. it's a simple_pin_fs from create_special_files,
|
|
|
|
* then ignore it.
|
|
|
|
*/
|
2012-03-17 14:17:32 +08:00
|
|
|
if (*flags & MS_KERNMOUNT)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (parse_options(sb, data)) {
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_WARNING "usbfs: mount parameter error.\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-12-09 12:01:06 +08:00
|
|
|
if (usbfs_mount)
|
2005-04-17 06:20:36 +08:00
|
|
|
update_sb(usbfs_mount->mnt_sb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-25 11:36:12 +08:00
|
|
|
static struct inode *usbfs_get_inode (struct super_block *sb, umode_t mode, dev_t dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct inode *inode = new_inode(sb);
|
|
|
|
|
|
|
|
if (inode) {
|
2010-10-23 23:19:54 +08:00
|
|
|
inode->i_ino = get_next_ino();
|
2011-07-25 11:36:12 +08:00
|
|
|
inode_init_owner(inode, NULL, mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
|
|
|
switch (mode & S_IFMT) {
|
|
|
|
default:
|
|
|
|
init_special_inode(inode, mode, dev);
|
|
|
|
break;
|
|
|
|
case S_IFREG:
|
|
|
|
inode->i_fop = &default_file_operations;
|
|
|
|
break;
|
|
|
|
case S_IFDIR:
|
2005-11-18 01:47:07 +08:00
|
|
|
inode->i_op = &simple_dir_inode_operations;
|
2005-04-17 06:20:36 +08:00
|
|
|
inode->i_fop = &simple_dir_operations;
|
|
|
|
|
|
|
|
/* directory inodes start off with i_nlink == 2 (for "." entry) */
|
2006-10-01 14:29:04 +08:00
|
|
|
inc_nlink(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SMP-safe */
|
2011-07-25 11:36:12 +08:00
|
|
|
static int usbfs_mknod (struct inode *dir, struct dentry *dentry, umode_t mode,
|
2005-04-17 06:20:36 +08:00
|
|
|
dev_t dev)
|
|
|
|
{
|
|
|
|
struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
|
|
|
|
int error = -EPERM;
|
|
|
|
|
|
|
|
if (dentry->d_inode)
|
|
|
|
return -EEXIST;
|
|
|
|
|
|
|
|
if (inode) {
|
|
|
|
d_instantiate(dentry, inode);
|
|
|
|
dget(dentry);
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-07-25 11:36:12 +08:00
|
|
|
static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, umode_t mode)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
|
|
|
|
res = usbfs_mknod (dir, dentry, mode, 0);
|
|
|
|
if (!res)
|
2006-10-01 14:29:04 +08:00
|
|
|
inc_nlink(dir);
|
2005-04-17 06:20:36 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-07-25 11:36:12 +08:00
|
|
|
static int usbfs_create (struct inode *dir, struct dentry *dentry, umode_t mode)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
mode = (mode & S_IALLUGO) | S_IFREG;
|
|
|
|
return usbfs_mknod (dir, dentry, mode, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int usbfs_positive (struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return dentry->d_inode && !d_unhashed(dentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbfs_empty (struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct list_head *list;
|
|
|
|
|
2011-01-07 14:49:34 +08:00
|
|
|
spin_lock(&dentry->d_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
list_for_each(list, &dentry->d_subdirs) {
|
[PATCH] shrink dentry struct
Some long time ago, dentry struct was carefully tuned so that on 32 bits
UP, sizeof(struct dentry) was exactly 128, ie a power of 2, and a multiple
of memory cache lines.
Then RCU was added and dentry struct enlarged by two pointers, with nice
results for SMP, but not so good on UP, because breaking the above tuning
(128 + 8 = 136 bytes)
This patch reverts this unwanted side effect, by using an union (d_u),
where d_rcu and d_child are placed so that these two fields can share their
memory needs.
At the time d_free() is called (and d_rcu is really used), d_child is known
to be empty and not touched by the dentry freeing.
Lockless lookups only access d_name, d_parent, d_lock, d_op, d_flags (so
the previous content of d_child is not needed if said dentry was unhashed
but still accessed by a CPU because of RCU constraints)
As dentry cache easily contains millions of entries, a size reduction is
worth the extra complexity of the ugly C union.
Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Cc: Dipankar Sarma <dipankar@in.ibm.com>
Cc: Maneesh Soni <maneesh@in.ibm.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: "Paul E. McKenney" <paulmck@us.ibm.com>
Cc: Ian Kent <raven@themaw.net>
Cc: Paul Jackson <pj@sgi.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Trond Myklebust <trond.myklebust@fys.uio.no>
Cc: Neil Brown <neilb@cse.unsw.edu.au>
Cc: James Morris <jmorris@namei.org>
Cc: Stephen Smalley <sds@epoch.ncsc.mil>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-01-08 17:03:32 +08:00
|
|
|
struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
|
2011-01-07 14:49:34 +08:00
|
|
|
|
|
|
|
spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (usbfs_positive(de)) {
|
2011-01-07 14:49:33 +08:00
|
|
|
spin_unlock(&de->d_lock);
|
2011-01-07 14:49:34 +08:00
|
|
|
spin_unlock(&dentry->d_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2011-01-07 14:49:33 +08:00
|
|
|
spin_unlock(&de->d_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2011-01-07 14:49:34 +08:00
|
|
|
spin_unlock(&dentry->d_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct inode *inode = dentry->d_inode;
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_lock(&inode->i_mutex);
|
2006-10-01 14:29:03 +08:00
|
|
|
drop_nlink(dentry->d_inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
dput(dentry);
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
d_delete(dentry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
int error = -ENOTEMPTY;
|
|
|
|
struct inode * inode = dentry->d_inode;
|
|
|
|
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_lock(&inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
dentry_unhash(dentry);
|
|
|
|
if (usbfs_empty(dentry)) {
|
2010-05-01 05:17:09 +08:00
|
|
|
dont_mount(dentry);
|
2006-10-01 14:29:03 +08:00
|
|
|
drop_nlink(dentry->d_inode);
|
|
|
|
drop_nlink(dentry->d_inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
dput(dentry);
|
|
|
|
inode->i_flags |= S_DEAD;
|
2006-10-01 14:29:03 +08:00
|
|
|
drop_nlink(dir);
|
2005-04-17 06:20:36 +08:00
|
|
|
error = 0;
|
|
|
|
}
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!error)
|
|
|
|
d_delete(dentry);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* default file operations */
|
|
|
|
static ssize_t default_read_file (struct file *file, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t default_write_file (struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
|
|
|
|
{
|
|
|
|
loff_t retval = -EINVAL;
|
|
|
|
|
2006-12-08 18:37:46 +08:00
|
|
|
mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
switch(orig) {
|
|
|
|
case 0:
|
|
|
|
if (offset > 0) {
|
|
|
|
file->f_pos = offset;
|
|
|
|
retval = file->f_pos;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if ((offset + file->f_pos) > 0) {
|
|
|
|
file->f_pos += offset;
|
|
|
|
retval = file->f_pos;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-12-08 18:37:46 +08:00
|
|
|
mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-08-06 07:37:11 +08:00
|
|
|
static const struct file_operations default_file_operations = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.read = default_read_file,
|
|
|
|
.write = default_write_file,
|
2012-04-06 05:25:11 +08:00
|
|
|
.open = simple_open,
|
2005-04-17 06:20:36 +08:00
|
|
|
.llseek = default_file_lseek,
|
|
|
|
};
|
|
|
|
|
2009-09-22 08:01:09 +08:00
|
|
|
static const struct super_operations usbfs_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.statfs = simple_statfs,
|
|
|
|
.drop_inode = generic_delete_inode,
|
|
|
|
.remount_fs = remount,
|
2008-01-25 03:34:07 +08:00
|
|
|
.show_options = usbfs_show_options,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
sb->s_blocksize = PAGE_CACHE_SIZE;
|
|
|
|
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
|
|
|
|
sb->s_magic = USBDEVICE_SUPER_MAGIC;
|
|
|
|
sb->s_op = &usbfs_ops;
|
|
|
|
sb->s_time_gran = 1;
|
|
|
|
inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
|
2012-02-13 11:08:01 +08:00
|
|
|
sb->s_root = d_make_root(inode);
|
|
|
|
if (!sb->s_root) {
|
2008-03-04 08:08:34 +08:00
|
|
|
dbg("%s: could not get root dentry!",__func__);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fs_create_by_name - create a file, given a name
|
|
|
|
* @name: name of file
|
|
|
|
* @mode: type of file
|
|
|
|
* @parent: dentry of directory to create it in
|
|
|
|
* @dentry: resulting dentry of file
|
|
|
|
*
|
|
|
|
* This function handles both regular files and directories.
|
|
|
|
*/
|
2011-07-25 11:36:12 +08:00
|
|
|
static int fs_create_by_name (const char *name, umode_t mode,
|
2005-04-17 06:20:36 +08:00
|
|
|
struct dentry *parent, struct dentry **dentry)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* If the parent is not specified, we create it in the root.
|
|
|
|
* We need the root dentry to do this, which is in the super
|
|
|
|
* block. A pointer to that is in the struct vfsmount that we
|
|
|
|
* have around.
|
|
|
|
*/
|
|
|
|
if (!parent ) {
|
2011-12-09 12:01:06 +08:00
|
|
|
if (usbfs_mount)
|
2011-12-08 07:21:57 +08:00
|
|
|
parent = usbfs_mount->mnt_root;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
dbg("Ah! can not find a parent!");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dentry = NULL;
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_lock(&parent->d_inode->i_mutex);
|
2005-06-23 15:09:12 +08:00
|
|
|
*dentry = lookup_one_len(name, parent, strlen(name));
|
2010-04-22 18:00:52 +08:00
|
|
|
if (!IS_ERR(*dentry)) {
|
2011-07-25 11:36:12 +08:00
|
|
|
if (S_ISDIR(mode))
|
2005-04-17 06:20:36 +08:00
|
|
|
error = usbfs_mkdir (parent->d_inode, *dentry, mode);
|
|
|
|
else
|
|
|
|
error = usbfs_create (parent->d_inode, *dentry, mode);
|
|
|
|
} else
|
2010-04-22 18:00:52 +08:00
|
|
|
error = PTR_ERR(*dentry);
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&parent->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-07-25 11:36:12 +08:00
|
|
|
static struct dentry *fs_create_file (const char *name, umode_t mode,
|
2005-04-17 06:20:36 +08:00
|
|
|
struct dentry *parent, void *data,
|
2006-08-06 07:37:11 +08:00
|
|
|
const struct file_operations *fops,
|
2005-04-17 06:20:36 +08:00
|
|
|
uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
dbg("creating file '%s'",name);
|
|
|
|
|
|
|
|
error = fs_create_by_name (name, mode, parent, &dentry);
|
|
|
|
if (error) {
|
|
|
|
dentry = NULL;
|
|
|
|
} else {
|
|
|
|
if (dentry->d_inode) {
|
|
|
|
if (data)
|
2006-09-27 16:50:46 +08:00
|
|
|
dentry->d_inode->i_private = data;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (fops)
|
|
|
|
dentry->d_inode->i_fop = fops;
|
|
|
|
dentry->d_inode->i_uid = uid;
|
|
|
|
dentry->d_inode->i_gid = gid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fs_remove_file (struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct dentry *parent = dentry->d_parent;
|
|
|
|
|
|
|
|
if (!parent || !parent->d_inode)
|
|
|
|
return;
|
|
|
|
|
2006-07-03 15:25:21 +08:00
|
|
|
mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (usbfs_positive(dentry)) {
|
|
|
|
if (dentry->d_inode) {
|
|
|
|
if (S_ISDIR(dentry->d_inode->i_mode))
|
|
|
|
usbfs_rmdir(parent->d_inode, dentry);
|
|
|
|
else
|
|
|
|
usbfs_unlink(parent->d_inode, dentry);
|
|
|
|
dput(dentry);
|
|
|
|
}
|
|
|
|
}
|
2006-01-10 07:59:24 +08:00
|
|
|
mutex_unlock(&parent->d_inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2010-07-25 05:48:30 +08:00
|
|
|
static struct dentry *usb_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-07-25 05:48:30 +08:00
|
|
|
return mount_single(fs_type, flags, data, usbfs_fill_super);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type usb_fs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "usbfs",
|
2010-07-25 05:48:30 +08:00
|
|
|
.mount = usb_mount,
|
2005-04-17 06:20:36 +08:00
|
|
|
.kill_sb = kill_litter_super,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int create_special_files (void)
|
|
|
|
{
|
|
|
|
struct dentry *parent;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* create the devices special file */
|
2006-06-09 21:34:16 +08:00
|
|
|
retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (retval) {
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_ERR "Unable to get usbfs mount\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-12-08 07:21:57 +08:00
|
|
|
parent = usbfs_mount->mnt_root;
|
2005-04-17 06:20:36 +08:00
|
|
|
devices_usbfs_dentry = fs_create_file ("devices",
|
|
|
|
listmode | S_IFREG, parent,
|
|
|
|
NULL, &usbfs_devices_fops,
|
|
|
|
listuid, listgid);
|
|
|
|
if (devices_usbfs_dentry == NULL) {
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_ERR "Unable to create devices usbfs file\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto error_clean_mounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
error_clean_mounts:
|
|
|
|
simple_release_fs(&usbfs_mount, &usbfs_mount_count);
|
|
|
|
exit:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_special_files (void)
|
|
|
|
{
|
|
|
|
if (devices_usbfs_dentry)
|
|
|
|
fs_remove_file (devices_usbfs_dentry);
|
|
|
|
devices_usbfs_dentry = NULL;
|
|
|
|
simple_release_fs(&usbfs_mount, &usbfs_mount_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbfs_update_special (void)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
if (devices_usbfs_dentry) {
|
|
|
|
inode = devices_usbfs_dentry->d_inode;
|
|
|
|
if (inode)
|
|
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
static void usbfs_add_bus(struct usb_bus *bus)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dentry *parent;
|
|
|
|
char name[8];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* create the special files if this is the first bus added */
|
|
|
|
if (num_buses == 0) {
|
|
|
|
retval = create_special_files();
|
|
|
|
if (retval)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++num_buses;
|
|
|
|
|
|
|
|
sprintf (name, "%03d", bus->busnum);
|
|
|
|
|
2011-12-08 07:21:57 +08:00
|
|
|
parent = usbfs_mount->mnt_root;
|
2005-04-17 06:20:36 +08:00
|
|
|
bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
|
|
|
|
bus, NULL, busuid, busgid);
|
|
|
|
if (bus->usbfs_dentry == NULL) {
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_ERR "Error creating usbfs bus entry\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
static void usbfs_remove_bus(struct usb_bus *bus)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (bus->usbfs_dentry) {
|
|
|
|
fs_remove_file (bus->usbfs_dentry);
|
|
|
|
bus->usbfs_dentry = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
--num_buses;
|
|
|
|
if (num_buses <= 0) {
|
|
|
|
remove_special_files();
|
|
|
|
num_buses = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
static void usbfs_add_device(struct usb_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
char name[8];
|
|
|
|
int i;
|
|
|
|
int i_size;
|
|
|
|
|
|
|
|
sprintf (name, "%03d", dev->devnum);
|
|
|
|
dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
|
|
|
|
dev->bus->usbfs_dentry, dev,
|
2007-03-13 22:59:31 +08:00
|
|
|
&usbdev_file_operations,
|
2005-04-17 06:20:36 +08:00
|
|
|
devuid, devgid);
|
|
|
|
if (dev->usbfs_dentry == NULL) {
|
2008-08-15 00:37:34 +08:00
|
|
|
printk(KERN_ERR "Error creating usbfs device entry\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the size of the device's file to be
|
|
|
|
* equal to the size of the device descriptors. */
|
|
|
|
i_size = sizeof (struct usb_device_descriptor);
|
|
|
|
for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
|
|
|
|
struct usb_config_descriptor *config =
|
|
|
|
(struct usb_config_descriptor *)dev->rawdescriptors[i];
|
|
|
|
i_size += le16_to_cpu(config->wTotalLength);
|
|
|
|
}
|
|
|
|
if (dev->usbfs_dentry->d_inode)
|
|
|
|
dev->usbfs_dentry->d_inode->i_size = i_size;
|
|
|
|
}
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
static void usbfs_remove_device(struct usb_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (dev->usbfs_dentry) {
|
|
|
|
fs_remove_file (dev->usbfs_dentry);
|
|
|
|
dev->usbfs_dentry = NULL;
|
|
|
|
}
|
2005-06-21 12:15:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
|
|
|
|
{
|
|
|
|
switch (action) {
|
|
|
|
case USB_DEVICE_ADD:
|
|
|
|
usbfs_add_device(dev);
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_REMOVE:
|
|
|
|
usbfs_remove_device(dev);
|
|
|
|
break;
|
|
|
|
case USB_BUS_ADD:
|
|
|
|
usbfs_add_bus(dev);
|
|
|
|
break;
|
|
|
|
case USB_BUS_REMOVE:
|
|
|
|
usbfs_remove_bus(dev);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
usbfs_update_special();
|
|
|
|
usbfs_conn_disc_event();
|
2005-06-21 12:15:16 +08:00
|
|
|
return NOTIFY_OK;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
static struct notifier_block usbfs_nb = {
|
|
|
|
.notifier_call = usbfs_notify,
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static struct proc_dir_entry *usbdir = NULL;
|
|
|
|
|
|
|
|
int __init usbfs_init(void)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = register_filesystem(&usb_fs_type);
|
2005-07-31 07:05:53 +08:00
|
|
|
if (retval)
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
|
2005-06-21 12:15:16 +08:00
|
|
|
usb_register_notify(&usbfs_nb);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* create mount point for usbfs */
|
2008-04-29 16:01:41 +08:00
|
|
|
usbdir = proc_mkdir("bus/usb", NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbfs_cleanup(void)
|
|
|
|
{
|
2005-06-21 12:15:16 +08:00
|
|
|
usb_unregister_notify(&usbfs_nb);
|
2005-04-17 06:20:36 +08:00
|
|
|
unregister_filesystem(&usb_fs_type);
|
|
|
|
if (usbdir)
|
2008-04-29 16:01:41 +08:00
|
|
|
remove_proc_entry("bus/usb", NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|