blob: cefa0c8b5b6ab983468b36ceed1cac3f66cbcb0e [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static struct dentry *devices_usbfs_dentry;
55static int num_buses; /* = 0 */
56
57static uid_t devuid; /* = 0 */
58static uid_t busuid; /* = 0 */
59static uid_t listuid; /* = 0 */
60static gid_t devgid; /* = 0 */
61static gid_t busgid; /* = 0 */
62static gid_t listgid; /* = 0 */
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010063static umode_t devmode = USBFS_DEFAULT_DEVMODE;
64static umode_t busmode = USBFS_DEFAULT_BUSMODE;
65static umode_t listmode = USBFS_DEFAULT_LISTMODE;
66
Al Viro34c80b12011-12-08 21:32:45 -050067static int usbfs_show_options(struct seq_file *seq, struct dentry *root)
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +010068{
69 if (devuid != 0)
70 seq_printf(seq, ",devuid=%u", devuid);
71 if (devgid != 0)
72 seq_printf(seq, ",devgid=%u", devgid);
73 if (devmode != USBFS_DEFAULT_DEVMODE)
74 seq_printf(seq, ",devmode=%o", devmode);
75 if (busuid != 0)
76 seq_printf(seq, ",busuid=%u", busuid);
77 if (busgid != 0)
78 seq_printf(seq, ",busgid=%u", busgid);
79 if (busmode != USBFS_DEFAULT_BUSMODE)
80 seq_printf(seq, ",busmode=%o", busmode);
81 if (listuid != 0)
82 seq_printf(seq, ",listuid=%u", listuid);
83 if (listgid != 0)
84 seq_printf(seq, ",listgid=%u", listgid);
85 if (listmode != USBFS_DEFAULT_LISTMODE)
86 seq_printf(seq, ",listmode=%o", listmode);
87
88 return 0;
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91enum {
92 Opt_devuid, Opt_devgid, Opt_devmode,
93 Opt_busuid, Opt_busgid, Opt_busmode,
94 Opt_listuid, Opt_listgid, Opt_listmode,
95 Opt_err,
96};
97
Steven Whitehousea447c092008-10-13 10:46:57 +010098static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 {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"},
108 {Opt_err, NULL}
109};
110
111static int parse_options(struct super_block *s, char *data)
112{
113 char *p;
114 int option;
115
116 /* (re)set to defaults. */
117 devuid = 0;
118 busuid = 0;
119 listuid = 0;
120 devgid = 0;
121 busgid = 0;
122 listgid = 0;
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +0100123 devmode = USBFS_DEFAULT_DEVMODE;
124 busmode = USBFS_DEFAULT_BUSMODE;
125 listmode = USBFS_DEFAULT_LISTMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 while ((p = strsep(&data, ",")) != NULL) {
128 substring_t args[MAX_OPT_ARGS];
129 int token;
130 if (!*p)
131 continue;
132
133 token = match_token(p, tokens, args);
134 switch (token) {
135 case Opt_devuid:
136 if (match_int(&args[0], &option))
137 return -EINVAL;
138 devuid = option;
139 break;
140 case Opt_devgid:
141 if (match_int(&args[0], &option))
142 return -EINVAL;
143 devgid = option;
144 break;
145 case Opt_devmode:
146 if (match_octal(&args[0], &option))
147 return -EINVAL;
148 devmode = option & S_IRWXUGO;
149 break;
150 case Opt_busuid:
151 if (match_int(&args[0], &option))
152 return -EINVAL;
153 busuid = option;
154 break;
155 case Opt_busgid:
156 if (match_int(&args[0], &option))
157 return -EINVAL;
158 busgid = option;
159 break;
160 case Opt_busmode:
161 if (match_octal(&args[0], &option))
162 return -EINVAL;
163 busmode = option & S_IRWXUGO;
164 break;
165 case Opt_listuid:
166 if (match_int(&args[0], &option))
167 return -EINVAL;
168 listuid = option;
169 break;
170 case Opt_listgid:
171 if (match_int(&args[0], &option))
172 return -EINVAL;
173 listgid = option;
174 break;
175 case Opt_listmode:
176 if (match_octal(&args[0], &option))
177 return -EINVAL;
178 listmode = option & S_IRWXUGO;
179 break;
180 default:
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700181 printk(KERN_ERR "usbfs: unrecognised mount option "
182 "\"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184 }
185 }
186
187 return 0;
188}
189
190static void update_special(struct dentry *special)
191{
192 special->d_inode->i_uid = listuid;
193 special->d_inode->i_gid = listgid;
194 special->d_inode->i_mode = S_IFREG | listmode;
195}
196
197static void update_dev(struct dentry *dev)
198{
199 dev->d_inode->i_uid = devuid;
200 dev->d_inode->i_gid = devgid;
201 dev->d_inode->i_mode = S_IFREG | devmode;
202}
203
204static void update_bus(struct dentry *bus)
205{
206 struct dentry *dev = NULL;
207
208 bus->d_inode->i_uid = busuid;
209 bus->d_inode->i_gid = busgid;
210 bus->d_inode->i_mode = S_IFDIR | busmode;
211
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800212 mutex_lock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Eric Dumazet5160ee62006-01-08 01:03:32 -0800214 list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (dev->d_inode)
216 update_dev(dev);
217
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800218 mutex_unlock(&bus->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void update_sb(struct super_block *sb)
222{
223 struct dentry *root = sb->s_root;
224 struct dentry *bus = NULL;
225
226 if (!root)
227 return;
228
Ingo Molnarf2eace22006-07-03 00:25:05 -0700229 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Eric Dumazet5160ee62006-01-08 01:03:32 -0800231 list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (bus->d_inode) {
233 switch (S_IFMT & bus->d_inode->i_mode) {
234 case S_IFDIR:
235 update_bus(bus);
236 break;
237 case S_IFREG:
238 update_special(bus);
239 break;
240 default:
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700241 printk(KERN_WARNING "usbfs: Unknown node %s "
242 "mode %x found on remount!\n",
243 bus->d_name.name, bus->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 }
246 }
247 }
248
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800249 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static int remount(struct super_block *sb, int *flags, char *data)
253{
254 /* If this is not a real mount,
255 * i.e. it's a simple_pin_fs from create_special_files,
256 * then ignore it.
257 */
Al Viro01e0fe02012-03-17 02:17:32 -0400258 if (*flags & MS_KERNMOUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260
261 if (parse_options(sb, data)) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700262 printk(KERN_WARNING "usbfs: mount parameter error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EINVAL;
264 }
265
Al Viroc972b4b2011-12-08 23:01:06 -0500266 if (usbfs_mount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 update_sb(usbfs_mount->mnt_sb);
268
269 return 0;
270}
271
Al Viro5b91aca2011-07-24 23:36:12 -0400272static struct inode *usbfs_get_inode (struct super_block *sb, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct inode *inode = new_inode(sb);
275
276 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400277 inode->i_ino = get_next_ino();
Al Viro5b91aca2011-07-24 23:36:12 -0400278 inode_init_owner(inode, NULL, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
280 switch (mode & S_IFMT) {
281 default:
282 init_special_inode(inode, mode, dev);
283 break;
284 case S_IFREG:
285 inode->i_fop = &default_file_operations;
286 break;
287 case S_IFDIR:
OGAWA Hirofumibc7cb322005-11-17 09:47:07 -0800288 inode->i_op = &simple_dir_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 inode->i_fop = &simple_dir_operations;
290
291 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700292 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 }
295 }
296 return inode;
297}
298
299/* SMP-safe */
Al Viro5b91aca2011-07-24 23:36:12 -0400300static int usbfs_mknod (struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 dev_t dev)
302{
303 struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
304 int error = -EPERM;
305
306 if (dentry->d_inode)
307 return -EEXIST;
308
309 if (inode) {
310 d_instantiate(dentry, inode);
311 dget(dentry);
312 error = 0;
313 }
314 return error;
315}
316
Al Viro5b91aca2011-07-24 23:36:12 -0400317static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int res;
320
321 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
322 res = usbfs_mknod (dir, dentry, mode, 0);
323 if (!res)
Dave Hansend8c76e62006-09-30 23:29:04 -0700324 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return res;
326}
327
Al Viro5b91aca2011-07-24 23:36:12 -0400328static int usbfs_create (struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 mode = (mode & S_IALLUGO) | S_IFREG;
331 return usbfs_mknod (dir, dentry, mode, 0);
332}
333
334static inline int usbfs_positive (struct dentry *dentry)
335{
336 return dentry->d_inode && !d_unhashed(dentry);
337}
338
339static int usbfs_empty (struct dentry *dentry)
340{
341 struct list_head *list;
342
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100343 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 list_for_each(list, &dentry->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800345 struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100346
347 spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (usbfs_positive(de)) {
Nick Pigginda502952011-01-07 17:49:33 +1100349 spin_unlock(&de->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100350 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352 }
Nick Pigginda502952011-01-07 17:49:33 +1100353 spin_unlock(&de->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100355 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 1;
357}
358
359static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
360{
361 struct inode *inode = dentry->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800362 mutex_lock(&inode->i_mutex);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700363 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dput(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800365 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 d_delete(dentry);
367 return 0;
368}
369
370static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
371{
372 int error = -ENOTEMPTY;
373 struct inode * inode = dentry->d_inode;
374
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800375 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dentry_unhash(dentry);
377 if (usbfs_empty(dentry)) {
Al Virod83c49f2010-04-30 17:17:09 -0400378 dont_mount(dentry);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700379 drop_nlink(dentry->d_inode);
380 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dput(dentry);
382 inode->i_flags |= S_DEAD;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700383 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 error = 0;
385 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800386 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!error)
388 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return error;
390}
391
392
393/* default file operations */
394static ssize_t default_read_file (struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
396{
397 return 0;
398}
399
400static ssize_t default_write_file (struct file *file, const char __user *buf,
401 size_t count, loff_t *ppos)
402{
403 return count;
404}
405
406static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
407{
408 loff_t retval = -EINVAL;
409
Josef Sipek33cb8992006-12-08 02:37:46 -0800410 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 switch(orig) {
412 case 0:
413 if (offset > 0) {
414 file->f_pos = offset;
415 retval = file->f_pos;
416 }
417 break;
418 case 1:
419 if ((offset + file->f_pos) > 0) {
420 file->f_pos += offset;
421 retval = file->f_pos;
422 }
423 break;
424 default:
425 break;
426 }
Josef Sipek33cb8992006-12-08 02:37:46 -0800427 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return retval;
429}
430
431static int default_open (struct inode *inode, struct file *file)
432{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700433 if (inode->i_private)
434 file->private_data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return 0;
437}
438
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300439static const struct file_operations default_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .read = default_read_file,
441 .write = default_write_file,
442 .open = default_open,
443 .llseek = default_file_lseek,
444};
445
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700446static const struct super_operations usbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 .statfs = simple_statfs,
448 .drop_inode = generic_delete_inode,
449 .remount_fs = remount,
Miklos Szeredi2e4f3c02008-01-24 20:34:07 +0100450 .show_options = usbfs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
453static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
454{
455 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
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;
461 sb->s_time_gran = 1;
462 inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
Al Viro318ceed2012-02-12 22:08:01 -0500463 sb->s_root = d_make_root(inode);
464 if (!sb->s_root) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s: could not get root dentry!",__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -ENOMEM;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
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
477 *
478 * This function handles both regular files and directories.
479 */
Al Viro5b91aca2011-07-24 23:36:12 -0400480static int fs_create_by_name (const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct dentry *parent, struct dentry **dentry)
482{
483 int error = 0;
484
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
488 * have around.
489 */
490 if (!parent ) {
Al Viroc972b4b2011-12-08 23:01:06 -0500491 if (usbfs_mount)
Al Viro4c1d5a62011-12-07 18:21:57 -0500492 parent = usbfs_mount->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 if (!parent) {
496 dbg("Ah! can not find a parent!");
497 return -EFAULT;
498 }
499
500 *dentry = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800501 mutex_lock(&parent->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700502 *dentry = lookup_one_len(name, parent, strlen(name));
Dan Carpenterfa7fe7a2010-04-22 12:00:52 +0200503 if (!IS_ERR(*dentry)) {
Al Viro5b91aca2011-07-24 23:36:12 -0400504 if (S_ISDIR(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 error = usbfs_mkdir (parent->d_inode, *dentry, mode);
506 else
507 error = usbfs_create (parent->d_inode, *dentry, mode);
508 } else
Dan Carpenterfa7fe7a2010-04-22 12:00:52 +0200509 error = PTR_ERR(*dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800510 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return error;
513}
514
Al Viro5b91aca2011-07-24 23:36:12 -0400515static struct dentry *fs_create_file (const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct dentry *parent, void *data,
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300517 const struct file_operations *fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 uid_t uid, gid_t gid)
519{
520 struct dentry *dentry;
521 int error;
522
523 dbg("creating file '%s'",name);
524
525 error = fs_create_by_name (name, mode, parent, &dentry);
526 if (error) {
527 dentry = NULL;
528 } else {
529 if (dentry->d_inode) {
530 if (data)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700531 dentry->d_inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (fops)
533 dentry->d_inode->i_fop = fops;
534 dentry->d_inode->i_uid = uid;
535 dentry->d_inode->i_gid = gid;
536 }
537 }
538
539 return dentry;
540}
541
542static void fs_remove_file (struct dentry *dentry)
543{
544 struct dentry *parent = dentry->d_parent;
545
546 if (!parent || !parent->d_inode)
547 return;
548
Arjan van de Ven8e7795e2006-07-03 00:25:21 -0700549 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 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);
554 else
555 usbfs_unlink(parent->d_inode, dentry);
556 dput(dentry);
557 }
558 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800559 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/* --------------------------------------------------------------------- */
563
Al Virofc14f2f2010-07-25 01:48:30 +0400564static struct dentry *usb_mount(struct file_system_type *fs_type,
565 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Al Virofc14f2f2010-07-25 01:48:30 +0400567 return mount_single(fs_type, flags, data, usbfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static struct file_system_type usb_fs_type = {
571 .owner = THIS_MODULE,
572 .name = "usbfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400573 .mount = usb_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .kill_sb = kill_litter_super,
575};
576
577/* --------------------------------------------------------------------- */
578
579static int create_special_files (void)
580{
581 struct dentry *parent;
582 int retval;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* create the devices special file */
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400585 retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700587 printk(KERN_ERR "Unable to get usbfs mount\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto exit;
589 }
590
Al Viro4c1d5a62011-12-07 18:21:57 -0500591 parent = usbfs_mount->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 devices_usbfs_dentry = fs_create_file ("devices",
593 listmode | S_IFREG, parent,
594 NULL, &usbfs_devices_fops,
595 listuid, listgid);
596 if (devices_usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700597 printk(KERN_ERR "Unable to create devices usbfs file\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 retval = -ENODEV;
599 goto error_clean_mounts;
600 }
601
602 goto exit;
603
604error_clean_mounts:
605 simple_release_fs(&usbfs_mount, &usbfs_mount_count);
606exit:
607 return retval;
608}
609
610static void remove_special_files (void)
611{
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);
616}
617
618void usbfs_update_special (void)
619{
620 struct inode *inode;
621
622 if (devices_usbfs_dentry) {
623 inode = devices_usbfs_dentry->d_inode;
624 if (inode)
625 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
626 }
627}
628
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700629static void usbfs_add_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct dentry *parent;
632 char name[8];
633 int retval;
634
635 /* create the special files if this is the first bus added */
636 if (num_buses == 0) {
637 retval = create_special_files();
638 if (retval)
639 return;
640 }
641 ++num_buses;
642
643 sprintf (name, "%03d", bus->busnum);
644
Al Viro4c1d5a62011-12-07 18:21:57 -0500645 parent = usbfs_mount->mnt_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
647 bus, NULL, busuid, busgid);
648 if (bus->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700649 printk(KERN_ERR "Error creating usbfs bus entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700654static void usbfs_remove_bus(struct usb_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 if (bus->usbfs_dentry) {
657 fs_remove_file (bus->usbfs_dentry);
658 bus->usbfs_dentry = NULL;
659 }
660
661 --num_buses;
662 if (num_buses <= 0) {
663 remove_special_files();
664 num_buses = 0;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700668static void usbfs_add_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 char name[8];
671 int i;
672 int i_size;
673
674 sprintf (name, "%03d", dev->devnum);
675 dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
676 dev->bus->usbfs_dentry, dev,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100677 &usbdev_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 devuid, devgid);
679 if (dev->usbfs_dentry == NULL) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700680 printk(KERN_ERR "Error creating usbfs device entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682 }
683
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);
691 }
692 if (dev->usbfs_dentry->d_inode)
693 dev->usbfs_dentry->d_inode->i_size = i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700696static void usbfs_remove_device(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (dev->usbfs_dentry) {
699 fs_remove_file (dev->usbfs_dentry);
700 dev->usbfs_dentry = NULL;
701 }
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700702}
703
704static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
705{
706 switch (action) {
707 case USB_DEVICE_ADD:
708 usbfs_add_device(dev);
709 break;
710 case USB_DEVICE_REMOVE:
711 usbfs_remove_device(dev);
712 break;
713 case USB_BUS_ADD:
714 usbfs_add_bus(dev);
715 break;
716 case USB_BUS_REMOVE:
717 usbfs_remove_bus(dev);
718 }
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 usbfs_update_special();
721 usbfs_conn_disc_event();
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700722 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700725static struct notifier_block usbfs_nb = {
726 .notifier_call = usbfs_notify,
727};
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/* --------------------------------------------------------------------- */
730
731static struct proc_dir_entry *usbdir = NULL;
732
733int __init usbfs_init(void)
734{
735 int retval;
736
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200737 retval = register_filesystem(&usb_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (retval)
739 return retval;
740
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700741 usb_register_notify(&usbfs_nb);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* create mount point for usbfs */
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700744 usbdir = proc_mkdir("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return 0;
747}
748
749void usbfs_cleanup(void)
750{
Greg Kroah-Hartman54a5c4c2005-06-20 21:15:16 -0700751 usb_unregister_notify(&usbfs_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 unregister_filesystem(&usb_fs_type);
753 if (usbdir)
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700754 remove_proc_entry("bus/usb", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756