blob: 2278dad886e2e8b028b2809eb39ccba8c09dff46 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2
3/*
4 * inode.c -- Inode/Dentry functions for the USB device file system.
5 *
6 * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7 * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
8 *
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.
13 *
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.
18 *
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.
22 *
23 * History:
24 * 0.1 04.01.2000 Created
25 * 0.2 10.12.2001 converted to use the vfs layer better
26 */
27
28/*****************************************************************************/
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/fs.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/parser.h>
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -070040#include <linux/notifier.h>
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010041#include <linux/seq_file.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020042#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/byteorder.h>
44#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010046#define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
47#define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
48#define USBFS_DEFAULT_LISTMODE S_IRUGO
49
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -030050static const struct file_operations default_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct vfsmount *usbfs_mount;
52static int usbfs_mount_count; /* = 0 */
53static int ignore_mount = 0;
54
55static struct dentry *devices_usbfs_dentry;
56static int num_buses; /* = 0 */
57
58static uid_t devuid; /* = 0 */
59static uid_t busuid; /* = 0 */
60static uid_t listuid; /* = 0 */
61static gid_t devgid; /* = 0 */
62static gid_t busgid; /* = 0 */
63static gid_t listgid; /* = 0 */
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010064static umode_t devmode = USBFS_DEFAULT_DEVMODE;
65static umode_t busmode = USBFS_DEFAULT_BUSMODE;
66static umode_t listmode = USBFS_DEFAULT_LISTMODE;
67
68static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
69{
70 if (devuid != 0)
71 seq_printf(seq, ",devuid=%u", devuid);
72 if (devgid != 0)
73 seq_printf(seq, ",devgid=%u", devgid);
74 if (devmode != USBFS_DEFAULT_DEVMODE)
75 seq_printf(seq, ",devmode=%o", devmode);
76 if (busuid != 0)
77 seq_printf(seq, ",busuid=%u", busuid);
78 if (busgid != 0)
79 seq_printf(seq, ",busgid=%u", busgid);
80 if (busmode != USBFS_DEFAULT_BUSMODE)
81 seq_printf(seq, ",busmode=%o", busmode);
82 if (listuid != 0)
83 seq_printf(seq, ",listuid=%u", listuid);
84 if (listgid != 0)
85 seq_printf(seq, ",listgid=%u", listgid);
86 if (listmode != USBFS_DEFAULT_LISTMODE)
87 seq_printf(seq, ",listmode=%o", listmode);
88
89 return 0;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92enum {
93 Opt_devuid, Opt_devgid, Opt_devmode,
94 Opt_busuid, Opt_busgid, Opt_busmode,
95 Opt_listuid, Opt_listgid, Opt_listmode,
96 Opt_err,
97};
98
Steven Whitehousea447c092008-10-13 10:46:57 +010099static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 {Opt_devuid, "devuid=%u"},
101 {Opt_devgid, "devgid=%u"},
102 {Opt_devmode, "devmode=%o"},
103 {Opt_busuid, "busuid=%u"},
104 {Opt_busgid, "busgid=%u"},
105 {Opt_busmode, "busmode=%o"},
106 {Opt_listuid, "listuid=%u"},
107 {Opt_listgid, "listgid=%u"},
108 {Opt_listmode, "listmode=%o"},
109 {Opt_err, NULL}
110};
111
112static int parse_options(struct super_block *s, char *data)
113{
114 char *p;
115 int option;
116
117 /* (re)set to defaults. */
118 devuid = 0;
119 busuid = 0;
120 listuid = 0;
121 devgid = 0;
122 busgid = 0;
123 listgid = 0;
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +0100124 devmode = USBFS_DEFAULT_DEVMODE;
125 busmode = USBFS_DEFAULT_BUSMODE;
126 listmode = USBFS_DEFAULT_LISTMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 while ((p = strsep(&data, ",")) != NULL) {
129 substring_t args[MAX_OPT_ARGS];
130 int token;
131 if (!*p)
132 continue;
133
134 token = match_token(p, tokens, args);
135 switch (token) {
136 case Opt_devuid:
137 if (match_int(&args[0], &option))
138 return -EINVAL;
139 devuid = option;
140 break;
141 case Opt_devgid:
142 if (match_int(&args[0], &option))
143 return -EINVAL;
144 devgid = option;
145 break;
146 case Opt_devmode:
147 if (match_octal(&args[0], &option))
148 return -EINVAL;
149 devmode = option & S_IRWXUGO;
150 break;
151 case Opt_busuid:
152 if (match_int(&args[0], &option))
153 return -EINVAL;
154 busuid = option;
155 break;
156 case Opt_busgid:
157 if (match_int(&args[0], &option))
158 return -EINVAL;
159 busgid = option;
160 break;
161 case Opt_busmode:
162 if (match_octal(&args[0], &option))
163 return -EINVAL;
164 busmode = option & S_IRWXUGO;
165 break;
166 case Opt_listuid:
167 if (match_int(&args[0], &option))
168 return -EINVAL;
169 listuid = option;
170 break;
171 case Opt_listgid:
172 if (match_int(&args[0], &option))
173 return -EINVAL;
174 listgid = option;
175 break;
176 case Opt_listmode:
177 if (match_octal(&args[0], &option))
178 return -EINVAL;
179 listmode = option & S_IRWXUGO;
180 break;
181 default:
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700182 printk(KERN_ERR "usbfs: unrecognised mount option "
183 "\"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
186 }
187
188 return 0;
189}
190
191static void update_special(struct dentry *special)
192{
193 special->d_inode->i_uid = listuid;
194 special->d_inode->i_gid = listgid;
195 special->d_inode->i_mode = S_IFREG | listmode;
196}
197
198static void update_dev(struct dentry *dev)
199{
200 dev->d_inode->i_uid = devuid;
201 dev->d_inode->i_gid = devgid;
202 dev->d_inode->i_mode = S_IFREG | devmode;
203}
204
205static void update_bus(struct dentry *bus)
206{
207 struct dentry *dev = NULL;
208
209 bus->d_inode->i_uid = busuid;
210 bus->d_inode->i_gid = busgid;
211 bus->d_inode->i_mode = S_IFDIR | busmode;
212
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800213 mutex_lock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Eric Dumazet5160ee62006-01-08 01:03:32 -0800215 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (dev->d_inode)
217 update_dev(dev);
218
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800219 mutex_unlock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static void update_sb(struct super_block *sb)
223{
224 struct dentry *root = sb->s_root;
225 struct dentry *bus = NULL;
226
227 if (!root)
228 return;
229
Ingo Molnarf2eace22006-07-03 00:25:05 -0700230 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Eric Dumazet5160ee62006-01-08 01:03:32 -0800232 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (bus->d_inode) {
234 switch (S_IFMT & bus->d_inode->i_mode) {
235 case S_IFDIR:
236 update_bus(bus);
237 break;
238 case S_IFREG:
239 update_special(bus);
240 break;
241 default:
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700242 printk(KERN_WARNING "usbfs: Unknown node %s "
243 "mode %x found on remount!\n",
244 bus->d_name.name, bus->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 }
247 }
248 }
249
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800250 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static int remount(struct super_block *sb, int *flags, char *data)
254{
255 /* If this is not a real mount,
256 * i.e. it's a simple_pin_fs from create_special_files,
257 * then ignore it.
258 */
259 if (ignore_mount)
260 return 0;
261
262 if (parse_options(sb, data)) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700263 printk(KERN_WARNING "usbfs: mount parameter error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -EINVAL;
265 }
266
267 if (usbfs_mount && usbfs_mount->mnt_sb)
268 update_sb(usbfs_mount->mnt_sb);
269
270 return 0;
271}
272
273static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
274{
275 struct inode *inode = new_inode(sb);
276
277 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400278 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 inode->i_mode = mode;
David Howellscd80ca82008-11-14 10:38:43 +1100280 inode->i_uid = current_fsuid();
281 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
283 switch (mode & S_IFMT) {
284 default:
285 init_special_inode(inode, mode, dev);
286 break;
287 case S_IFREG:
288 inode->i_fop = &default_file_operations;
289 break;
290 case S_IFDIR:
OGAWA Hirofumibc7cb322005-11-17 09:47:07 -0800291 inode->i_op = &simple_dir_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 inode->i_fop = &simple_dir_operations;
293
294 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700295 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298 }
299 return inode;
300}
301
302/* SMP-safe */
303static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
304 dev_t dev)
305{
306 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
307 int error = -EPERM;
308
309 if (dentry->d_inode)
310 return -EEXIST;
311
312 if (inode) {
313 d_instantiate(dentry, inode);
314 dget(dentry);
315 error = 0;
316 }
317 return error;
318}
319
320static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
321{
322 int res;
323
324 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
325 res = usbfs_mknod (dir, dentry, mode, 0);
326 if (!res)
Dave Hansend8c76e62006-09-30 23:29:04 -0700327 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return res;
329}
330
331static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
332{
333 mode = (mode & S_IALLUGO) | S_IFREG;
334 return usbfs_mknod (dir, dentry, mode, 0);
335}
336
337static inline int usbfs_positive (struct dentry *dentry)
338{
339 return dentry->d_inode && !d_unhashed(dentry);
340}
341
342static int usbfs_empty (struct dentry *dentry)
343{
344 struct list_head *list;
345
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100346 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 list_for_each(list, &dentry->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800348 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100349
350 spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (usbfs_positive(de)) {
Nick Pigginda502952011-01-07 17:49:33 +1100352 spin_unlock(&de->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100353 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
Nick Pigginda502952011-01-07 17:49:33 +1100356 spin_unlock(&de->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100358 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 1;
360}
361
362static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
363{
364 struct inode *inode = dentry->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800365 mutex_lock(&inode->i_mutex);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700366 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 dput(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800368 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 d_delete(dentry);
370 return 0;
371}
372
373static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
374{
375 int error = -ENOTEMPTY;
376 struct inode * inode = dentry->d_inode;
377
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800378 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dentry_unhash(dentry);
380 if (usbfs_empty(dentry)) {
Al Virod83c49f2010-04-30 17:17:09 -0400381 dont_mount(dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700382 drop_nlink(dentry->d_inode);
383 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dput(dentry);
385 inode->i_flags |= S_DEAD;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700386 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 error = 0;
388 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800389 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!error)
391 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return error;
393}
394
395
396/* default file operations */
397static ssize_t default_read_file (struct file *file, char __user *buf,
398 size_t count, loff_t *ppos)
399{
400 return 0;
401}
402
403static ssize_t default_write_file (struct file *file, const char __user *buf,
404 size_t count, loff_t *ppos)
405{
406 return count;
407}
408
409static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
410{
411 loff_t retval = -EINVAL;
412
Josef Sipek33cb8992006-12-08 02:37:46 -0800413 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 switch(orig) {
415 case 0:
416 if (offset > 0) {
417 file->f_pos = offset;
418 retval = file->f_pos;
419 }
420 break;
421 case 1:
422 if ((offset + file->f_pos) > 0) {
423 file->f_pos += offset;
424 retval = file->f_pos;
425 }
426 break;
427 default:
428 break;
429 }
Josef Sipek33cb8992006-12-08 02:37:46 -0800430 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return retval;
432}
433
434static int default_open (struct inode *inode, struct file *file)
435{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700436 if (inode->i_private)
437 file->private_data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return 0;
440}
441
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300442static const struct file_operations default_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .read = default_read_file,
444 .write = default_write_file,
445 .open = default_open,
446 .llseek = default_file_lseek,
447};
448
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700449static const struct super_operations usbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .statfs = simple_statfs,
451 .drop_inode = generic_delete_inode,
452 .remount_fs = remount,
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +0100453 .show_options = usbfs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
456static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
457{
458 struct inode *inode;
459 struct dentry *root;
460
461 sb->s_blocksize = PAGE_CACHE_SIZE;
462 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
463 sb->s_magic = USBDEVICE_SUPER_MAGIC;
464 sb->s_op = &usbfs_ops;
465 sb->s_time_gran = 1;
466 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
467
468 if (!inode) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800469 dbg("%s: could not get inode!",__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
471 }
472
473 root = d_alloc_root(inode);
474 if (!root) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800475 dbg("%s: could not get root dentry!",__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 iput(inode);
477 return -ENOMEM;
478 }
479 sb->s_root = root;
480 return 0;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
484 * fs_create_by_name - create a file, given a name
485 * @name: name of file
486 * @mode: type of file
487 * @parent: dentry of directory to create it in
488 * @dentry: resulting dentry of file
489 *
490 * This function handles both regular files and directories.
491 */
492static int fs_create_by_name (const char *name, mode_t mode,
493 struct dentry *parent, struct dentry **dentry)
494{
495 int error = 0;
496
497 /* If the parent is not specified, we create it in the root.
498 * We need the root dentry to do this, which is in the super
499 * block. A pointer to that is in the struct vfsmount that we
500 * have around.
501 */
502 if (!parent ) {
503 if (usbfs_mount && usbfs_mount->mnt_sb) {
504 parent = usbfs_mount->mnt_sb->s_root;
505 }
506 }
507
508 if (!parent) {
509 dbg("Ah! can not find a parent!");
510 return -EFAULT;
511 }
512
513 *dentry = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800514 mutex_lock(&parent->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700515 *dentry = lookup_one_len(name, parent, strlen(name));
Dan Carpenterfa7fe7a2010-04-22 12:00:52 +0200516 if (!IS_ERR(*dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if ((mode & S_IFMT) == S_IFDIR)
518 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
519 else
520 error = usbfs_create (parent->d_inode, *dentry, mode);
521 } else
Dan Carpenterfa7fe7a2010-04-22 12:00:52 +0200522 error = PTR_ERR(*dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800523 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return error;
526}
527
528static struct dentry *fs_create_file (const char *name, mode_t mode,
529 struct dentry *parent, void *data,
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300530 const struct file_operations *fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 uid_t uid, gid_t gid)
532{
533 struct dentry *dentry;
534 int error;
535
536 dbg("creating file '%s'",name);
537
538 error = fs_create_by_name (name, mode, parent, &dentry);
539 if (error) {
540 dentry = NULL;
541 } else {
542 if (dentry->d_inode) {
543 if (data)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700544 dentry->d_inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (fops)
546 dentry->d_inode->i_fop = fops;
547 dentry->d_inode->i_uid = uid;
548 dentry->d_inode->i_gid = gid;
549 }
550 }
551
552 return dentry;
553}
554
555static void fs_remove_file (struct dentry *dentry)
556{
557 struct dentry *parent = dentry->d_parent;
558
559 if (!parent || !parent->d_inode)
560 return;
561
Arjan van de Ven8e7795e2006-07-03 00:25:21 -0700562 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (usbfs_positive(dentry)) {
564 if (dentry->d_inode) {
565 if (S_ISDIR(dentry->d_inode->i_mode))
566 usbfs_rmdir(parent->d_inode, dentry);
567 else
568 usbfs_unlink(parent->d_inode, dentry);
569 dput(dentry);
570 }
571 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800572 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/* --------------------------------------------------------------------- */
576
Al Virofc14f2f2010-07-25 01:48:30 +0400577static struct dentry *usb_mount(struct file_system_type *fs_type,
578 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Al Virofc14f2f2010-07-25 01:48:30 +0400580 return mount_single(fs_type, flags, data, usbfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static struct file_system_type usb_fs_type = {
584 .owner = THIS_MODULE,
585 .name = "usbfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400586 .mount = usb_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .kill_sb = kill_litter_super,
588};
589
590/* --------------------------------------------------------------------- */
591
592static int create_special_files (void)
593{
594 struct dentry *parent;
595 int retval;
596
597 /* the simple_pin_fs calls will call remount with no options
598 * without this flag that would overwrite the real mount options (if any)
599 */
600 ignore_mount = 1;
601
602 /* create the devices special file */
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400603 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700605 printk(KERN_ERR "Unable to get usbfs mount\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto exit;
607 }
608
609 ignore_mount = 0;
610
611 parent = usbfs_mount->mnt_sb->s_root;
612 devices_usbfs_dentry = fs_create_file ("devices",
613 listmode | S_IFREG, parent,
614 NULL, &usbfs_devices_fops,
615 listuid, listgid);
616 if (devices_usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700617 printk(KERN_ERR "Unable to create devices usbfs file\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 retval = -ENODEV;
619 goto error_clean_mounts;
620 }
621
622 goto exit;
623
624error_clean_mounts:
625 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
626exit:
627 return retval;
628}
629
630static void remove_special_files (void)
631{
632 if (devices_usbfs_dentry)
633 fs_remove_file (devices_usbfs_dentry);
634 devices_usbfs_dentry = NULL;
635 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
636}
637
638void usbfs_update_special (void)
639{
640 struct inode *inode;
641
642 if (devices_usbfs_dentry) {
643 inode = devices_usbfs_dentry->d_inode;
644 if (inode)
645 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
646 }
647}
648
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700649static void usbfs_add_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct dentry *parent;
652 char name[8];
653 int retval;
654
655 /* create the special files if this is the first bus added */
656 if (num_buses == 0) {
657 retval = create_special_files();
658 if (retval)
659 return;
660 }
661 ++num_buses;
662
663 sprintf (name, "%03d", bus->busnum);
664
665 parent = usbfs_mount->mnt_sb->s_root;
666 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
667 bus, NULL, busuid, busgid);
668 if (bus->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700669 printk(KERN_ERR "Error creating usbfs bus entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700674static void usbfs_remove_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 if (bus->usbfs_dentry) {
677 fs_remove_file (bus->usbfs_dentry);
678 bus->usbfs_dentry = NULL;
679 }
680
681 --num_buses;
682 if (num_buses <= 0) {
683 remove_special_files();
684 num_buses = 0;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700688static void usbfs_add_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 char name[8];
691 int i;
692 int i_size;
693
694 sprintf (name, "%03d", dev->devnum);
695 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
696 dev->bus->usbfs_dentry, dev,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100697 &usbdev_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 devuid, devgid);
699 if (dev->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700700 printk(KERN_ERR "Error creating usbfs device entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
702 }
703
704 /* Set the size of the device's file to be
705 * equal to the size of the device descriptors. */
706 i_size = sizeof (struct usb_device_descriptor);
707 for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
708 struct usb_config_descriptor *config =
709 (struct usb_config_descriptor *)dev->rawdescriptors[i];
710 i_size += le16_to_cpu(config->wTotalLength);
711 }
712 if (dev->usbfs_dentry->d_inode)
713 dev->usbfs_dentry->d_inode->i_size = i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700716static void usbfs_remove_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (dev->usbfs_dentry) {
719 fs_remove_file (dev->usbfs_dentry);
720 dev->usbfs_dentry = NULL;
721 }
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700722}
723
724static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
725{
726 switch (action) {
727 case USB_DEVICE_ADD:
728 usbfs_add_device(dev);
729 break;
730 case USB_DEVICE_REMOVE:
731 usbfs_remove_device(dev);
732 break;
733 case USB_BUS_ADD:
734 usbfs_add_bus(dev);
735 break;
736 case USB_BUS_REMOVE:
737 usbfs_remove_bus(dev);
738 }
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 usbfs_update_special();
741 usbfs_conn_disc_event();
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700742 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700745static struct notifier_block usbfs_nb = {
746 .notifier_call = usbfs_notify,
747};
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/* --------------------------------------------------------------------- */
750
751static struct proc_dir_entry *usbdir = NULL;
752
753int __init usbfs_init(void)
754{
755 int retval;
756
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200757 retval = register_filesystem(&usb_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (retval)
759 return retval;
760
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700761 usb_register_notify(&usbfs_nb);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* create mount point for usbfs */
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700764 usbdir = proc_mkdir("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 return 0;
767}
768
769void usbfs_cleanup(void)
770{
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700771 usb_unregister_notify(&usbfs_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unregister_filesystem(&usb_fs_type);
773 if (usbdir)
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700774 remove_proc_entry("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776