inode.c 17.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*****************************************************************************/

/*
 *	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>
40
#include <linux/notifier.h>
M
Miklos Szeredi 已提交
41
#include <linux/seq_file.h>
42
#include <linux/smp_lock.h>
L
Linus Torvalds 已提交
43 44
#include <asm/byteorder.h>
#include "usb.h"
45
#include "hcd.h"
L
Linus Torvalds 已提交
46

M
Miklos Szeredi 已提交
47 48 49 50
#define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
#define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
#define USBFS_DEFAULT_LISTMODE S_IRUGO

51
static const struct file_operations default_file_operations;
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
static struct vfsmount *usbfs_mount;
static int usbfs_mount_count;	/* = 0 */
static int ignore_mount = 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 */
M
Miklos Szeredi 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static umode_t devmode = USBFS_DEFAULT_DEVMODE;
static umode_t busmode = USBFS_DEFAULT_BUSMODE;
static umode_t listmode = USBFS_DEFAULT_LISTMODE;

static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
{
	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;
}
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99

enum {
	Opt_devuid, Opt_devgid, Opt_devmode,
	Opt_busuid, Opt_busgid, Opt_busmode,
	Opt_listuid, Opt_listgid, Opt_listmode,
	Opt_err,
};

100
static const match_table_t tokens = {
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	{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;
M
Miklos Szeredi 已提交
125 126 127
	devmode = USBFS_DEFAULT_DEVMODE;
	busmode = USBFS_DEFAULT_BUSMODE;
	listmode = USBFS_DEFAULT_LISTMODE;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	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:
183 184
			printk(KERN_ERR "usbfs: unrecognised mount option "
			       "\"%s\" or missing value\n", p);
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
			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;

214
	mutex_lock(&bus->d_inode->i_mutex);
L
Linus Torvalds 已提交
215

E
Eric Dumazet 已提交
216
	list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
L
Linus Torvalds 已提交
217 218 219
		if (dev->d_inode)
			update_dev(dev);

220
	mutex_unlock(&bus->d_inode->i_mutex);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230
}

static void update_sb(struct super_block *sb)
{
	struct dentry *root = sb->s_root;
	struct dentry *bus = NULL;

	if (!root)
		return;

I
Ingo Molnar 已提交
231
	mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
L
Linus Torvalds 已提交
232

E
Eric Dumazet 已提交
233
	list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242
		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:
243 244 245
				printk(KERN_WARNING "usbfs: Unknown node %s "
				       "mode %x found on remount!\n",
				       bus->d_name.name, bus->d_inode->i_mode);
L
Linus Torvalds 已提交
246 247 248 249 250
				break;
			}
		}
	}

251
	mutex_unlock(&root->d_inode->i_mutex);
L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259 260 261 262 263
}

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.
	 */
	if (ignore_mount)
		return 0;

	if (parse_options(sb, data)) {
264
		printk(KERN_WARNING "usbfs: mount parameter error.\n");
L
Linus Torvalds 已提交
265 266 267
		return -EINVAL;
	}

268 269
	lock_kernel();

L
Linus Torvalds 已提交
270 271 272
	if (usbfs_mount && usbfs_mount->mnt_sb)
		update_sb(usbfs_mount->mnt_sb);

273 274
	unlock_kernel();

L
Linus Torvalds 已提交
275 276 277 278 279 280 281 282 283
	return 0;
}

static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
{
	struct inode *inode = new_inode(sb);

	if (inode) {
		inode->i_mode = mode;
284 285
		inode->i_uid = current_fsuid();
		inode->i_gid = current_fsgid();
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293 294
		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:
295
			inode->i_op = &simple_dir_inode_operations;
L
Linus Torvalds 已提交
296 297 298
			inode->i_fop = &simple_dir_operations;

			/* directory inodes start off with i_nlink == 2 (for "." entry) */
299
			inc_nlink(inode);
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			break;
		}
	}
	return inode; 
}

/* SMP-safe */
static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
			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;
}

static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
{
	int res;

	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
	res = usbfs_mknod (dir, dentry, mode, 0);
	if (!res)
331
		inc_nlink(dir);
L
Linus Torvalds 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	return res;
}

static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
{
	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;

	spin_lock(&dcache_lock);

	list_for_each(list, &dentry->d_subdirs) {
E
Eric Dumazet 已提交
353
		struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366
		if (usbfs_positive(de)) {
			spin_unlock(&dcache_lock);
			return 0;
		}
	}

	spin_unlock(&dcache_lock);
	return 1;
}

static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
367
	mutex_lock(&inode->i_mutex);
368
	drop_nlink(dentry->d_inode);
L
Linus Torvalds 已提交
369
	dput(dentry);
370
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379
	d_delete(dentry);
	return 0;
}

static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
{
	int error = -ENOTEMPTY;
	struct inode * inode = dentry->d_inode;

380
	mutex_lock(&inode->i_mutex);
L
Linus Torvalds 已提交
381 382
	dentry_unhash(dentry);
	if (usbfs_empty(dentry)) {
383 384
		drop_nlink(dentry->d_inode);
		drop_nlink(dentry->d_inode);
L
Linus Torvalds 已提交
385 386
		dput(dentry);
		inode->i_flags |= S_DEAD;
387
		drop_nlink(dir);
L
Linus Torvalds 已提交
388 389
		error = 0;
	}
390
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	if (!error)
		d_delete(dentry);
	dput(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;

J
Josef Sipek 已提交
415
	mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	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;
	}
J
Josef Sipek 已提交
432
	mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
L
Linus Torvalds 已提交
433 434 435 436 437
	return retval;
}

static int default_open (struct inode *inode, struct file *file)
{
438 439
	if (inode->i_private)
		file->private_data = inode->i_private;
L
Linus Torvalds 已提交
440 441 442 443

	return 0;
}

444
static const struct file_operations default_file_operations = {
L
Linus Torvalds 已提交
445 446 447 448 449 450
	.read =		default_read_file,
	.write =	default_write_file,
	.open =		default_open,
	.llseek =	default_file_lseek,
};

451
static const struct super_operations usbfs_ops = {
L
Linus Torvalds 已提交
452 453 454
	.statfs =	simple_statfs,
	.drop_inode =	generic_delete_inode,
	.remount_fs =	remount,
M
Miklos Szeredi 已提交
455
	.show_options = usbfs_show_options,
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
};

static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct inode *inode;
	struct dentry *root;

	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);

	if (!inode) {
471
		dbg("%s: could not get inode!",__func__);
L
Linus Torvalds 已提交
472 473 474 475 476
		return -ENOMEM;
	}

	root = d_alloc_root(inode);
	if (!root) {
477
		dbg("%s: could not get root dentry!",__func__);
L
Linus Torvalds 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
		iput(inode);
		return -ENOMEM;
	}
	sb->s_root = root;
	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.
 */
static int fs_create_by_name (const char *name, mode_t mode,
			      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 ) {
		if (usbfs_mount && usbfs_mount->mnt_sb) {
			parent = usbfs_mount->mnt_sb->s_root;
		}
	}

	if (!parent) {
		dbg("Ah! can not find a parent!");
		return -EFAULT;
	}

	*dentry = NULL;
516
	mutex_lock(&parent->d_inode->i_mutex);
517
	*dentry = lookup_one_len(name, parent, strlen(name));
518
	if (!IS_ERR(*dentry)) {
L
Linus Torvalds 已提交
519 520 521 522 523
		if ((mode & S_IFMT) == S_IFDIR)
			error = usbfs_mkdir (parent->d_inode, *dentry, mode);
		else 
			error = usbfs_create (parent->d_inode, *dentry, mode);
	} else
524
		error = PTR_ERR(*dentry);
525
	mutex_unlock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
526 527 528 529 530 531

	return error;
}

static struct dentry *fs_create_file (const char *name, mode_t mode,
				      struct dentry *parent, void *data,
532
				      const struct file_operations *fops,
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545
				      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)
546
				dentry->d_inode->i_private = data;
L
Linus Torvalds 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
			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;

564
	mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
L
Linus Torvalds 已提交
565 566 567 568 569 570 571 572 573
	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);
		}
	}
574
	mutex_unlock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
575 576 577 578
}

/* --------------------------------------------------------------------- */

579 580
static int usb_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
581
{
582
	return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt);
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
}

static struct file_system_type usb_fs_type = {
	.owner =	THIS_MODULE,
	.name =		"usbfs",
	.get_sb =	usb_get_sb,
	.kill_sb =	kill_litter_super,
};

/* --------------------------------------------------------------------- */

static int create_special_files (void)
{
	struct dentry *parent;
	int retval;

	/* the simple_pin_fs calls will call remount with no options
	 * without this flag that would overwrite the real mount options (if any)
	 */
	ignore_mount = 1;

	/* create the devices special file */
605
	retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
L
Linus Torvalds 已提交
606
	if (retval) {
607
		printk(KERN_ERR "Unable to get usbfs mount\n");
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617 618
		goto exit;
	}

	ignore_mount = 0;

	parent = usbfs_mount->mnt_sb->s_root;
	devices_usbfs_dentry = fs_create_file ("devices",
					       listmode | S_IFREG, parent,
					       NULL, &usbfs_devices_fops,
					       listuid, listgid);
	if (devices_usbfs_dentry == NULL) {
619
		printk(KERN_ERR "Unable to create devices usbfs file\n");
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
		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;
	}
}

651
static void usbfs_add_bus(struct usb_bus *bus)
L
Linus Torvalds 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
{
	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);

	parent = usbfs_mount->mnt_sb->s_root;
	bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
					    bus, NULL, busuid, busgid);
	if (bus->usbfs_dentry == NULL) {
671
		printk(KERN_ERR "Error creating usbfs bus entry\n");
L
Linus Torvalds 已提交
672 673 674 675
		return;
	}
}

676
static void usbfs_remove_bus(struct usb_bus *bus)
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689
{
	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;
	}
}

690
static void usbfs_add_device(struct usb_device *dev)
L
Linus Torvalds 已提交
691 692 693 694 695 696 697 698
{
	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,
699
					    &usbdev_file_operations,
L
Linus Torvalds 已提交
700 701
					    devuid, devgid);
	if (dev->usbfs_dentry == NULL) {
702
		printk(KERN_ERR "Error creating usbfs device entry\n");
L
Linus Torvalds 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
		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;
}

718
static void usbfs_remove_device(struct usb_device *dev)
L
Linus Torvalds 已提交
719 720 721 722 723
{
	if (dev->usbfs_dentry) {
		fs_remove_file (dev->usbfs_dentry);
		dev->usbfs_dentry = NULL;
	}
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
}

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);
	}

L
Linus Torvalds 已提交
742 743
	usbfs_update_special();
	usbfs_conn_disc_event();
744
	return NOTIFY_OK;
L
Linus Torvalds 已提交
745 746
}

747 748 749 750
static struct notifier_block usbfs_nb = {
	.notifier_call = 	usbfs_notify,
};

L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759
/* --------------------------------------------------------------------- */

static struct proc_dir_entry *usbdir = NULL;

int __init usbfs_init(void)
{
	int retval;

	retval = register_filesystem(&usb_fs_type);
760
	if (retval)
L
Linus Torvalds 已提交
761 762
		return retval;

763 764
	usb_register_notify(&usbfs_nb);

L
Linus Torvalds 已提交
765
	/* create mount point for usbfs */
A
Alexey Dobriyan 已提交
766
	usbdir = proc_mkdir("bus/usb", NULL);
L
Linus Torvalds 已提交
767 768 769 770 771 772

	return 0;
}

void usbfs_cleanup(void)
{
773
	usb_unregister_notify(&usbfs_nb);
L
Linus Torvalds 已提交
774 775
	unregister_filesystem(&usb_fs_type);
	if (usbdir)
A
Alexey Dobriyan 已提交
776
		remove_proc_entry("bus/usb", NULL);
L
Linus Torvalds 已提交
777 778
}