Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /*****************************************************************************/ |
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 | ||
1da177e4 LT |
30 | #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> | |
1da177e4 | 39 | #include <linux/parser.h> |
54a5c4cd | 40 | #include <linux/notifier.h> |
2e4f3c02 | 41 | #include <linux/seq_file.h> |
1da177e4 LT |
42 | #include <asm/byteorder.h> |
43 | #include "usb.h" | |
6d5e8254 | 44 | #include "hcd.h" |
1da177e4 | 45 | |
2e4f3c02 MS |
46 | #define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO) |
47 | #define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO) | |
48 | #define USBFS_DEFAULT_LISTMODE S_IRUGO | |
49 | ||
1da177e4 | 50 | static struct super_operations usbfs_ops; |
066202dd | 51 | static const struct file_operations default_file_operations; |
1da177e4 LT |
52 | static struct vfsmount *usbfs_mount; |
53 | static int usbfs_mount_count; /* = 0 */ | |
54 | static int ignore_mount = 0; | |
55 | ||
56 | static struct dentry *devices_usbfs_dentry; | |
57 | static int num_buses; /* = 0 */ | |
58 | ||
59 | static uid_t devuid; /* = 0 */ | |
60 | static uid_t busuid; /* = 0 */ | |
61 | static uid_t listuid; /* = 0 */ | |
62 | static gid_t devgid; /* = 0 */ | |
63 | static gid_t busgid; /* = 0 */ | |
64 | static gid_t listgid; /* = 0 */ | |
2e4f3c02 MS |
65 | static umode_t devmode = USBFS_DEFAULT_DEVMODE; |
66 | static umode_t busmode = USBFS_DEFAULT_BUSMODE; | |
67 | static umode_t listmode = USBFS_DEFAULT_LISTMODE; | |
68 | ||
69 | static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt) | |
70 | { | |
71 | if (devuid != 0) | |
72 | seq_printf(seq, ",devuid=%u", devuid); | |
73 | if (devgid != 0) | |
74 | seq_printf(seq, ",devgid=%u", devgid); | |
75 | if (devmode != USBFS_DEFAULT_DEVMODE) | |
76 | seq_printf(seq, ",devmode=%o", devmode); | |
77 | if (busuid != 0) | |
78 | seq_printf(seq, ",busuid=%u", busuid); | |
79 | if (busgid != 0) | |
80 | seq_printf(seq, ",busgid=%u", busgid); | |
81 | if (busmode != USBFS_DEFAULT_BUSMODE) | |
82 | seq_printf(seq, ",busmode=%o", busmode); | |
83 | if (listuid != 0) | |
84 | seq_printf(seq, ",listuid=%u", listuid); | |
85 | if (listgid != 0) | |
86 | seq_printf(seq, ",listgid=%u", listgid); | |
87 | if (listmode != USBFS_DEFAULT_LISTMODE) | |
88 | seq_printf(seq, ",listmode=%o", listmode); | |
89 | ||
90 | return 0; | |
91 | } | |
1da177e4 LT |
92 | |
93 | enum { | |
94 | Opt_devuid, Opt_devgid, Opt_devmode, | |
95 | Opt_busuid, Opt_busgid, Opt_busmode, | |
96 | Opt_listuid, Opt_listgid, Opt_listmode, | |
97 | Opt_err, | |
98 | }; | |
99 | ||
a447c093 | 100 | static const match_table_t tokens = { |
1da177e4 LT |
101 | {Opt_devuid, "devuid=%u"}, |
102 | {Opt_devgid, "devgid=%u"}, | |
103 | {Opt_devmode, "devmode=%o"}, | |
104 | {Opt_busuid, "busuid=%u"}, | |
105 | {Opt_busgid, "busgid=%u"}, | |
106 | {Opt_busmode, "busmode=%o"}, | |
107 | {Opt_listuid, "listuid=%u"}, | |
108 | {Opt_listgid, "listgid=%u"}, | |
109 | {Opt_listmode, "listmode=%o"}, | |
110 | {Opt_err, NULL} | |
111 | }; | |
112 | ||
113 | static int parse_options(struct super_block *s, char *data) | |
114 | { | |
115 | char *p; | |
116 | int option; | |
117 | ||
118 | /* (re)set to defaults. */ | |
119 | devuid = 0; | |
120 | busuid = 0; | |
121 | listuid = 0; | |
122 | devgid = 0; | |
123 | busgid = 0; | |
124 | listgid = 0; | |
2e4f3c02 MS |
125 | devmode = USBFS_DEFAULT_DEVMODE; |
126 | busmode = USBFS_DEFAULT_BUSMODE; | |
127 | listmode = USBFS_DEFAULT_LISTMODE; | |
1da177e4 LT |
128 | |
129 | while ((p = strsep(&data, ",")) != NULL) { | |
130 | substring_t args[MAX_OPT_ARGS]; | |
131 | int token; | |
132 | if (!*p) | |
133 | continue; | |
134 | ||
135 | token = match_token(p, tokens, args); | |
136 | switch (token) { | |
137 | case Opt_devuid: | |
138 | if (match_int(&args[0], &option)) | |
139 | return -EINVAL; | |
140 | devuid = option; | |
141 | break; | |
142 | case Opt_devgid: | |
143 | if (match_int(&args[0], &option)) | |
144 | return -EINVAL; | |
145 | devgid = option; | |
146 | break; | |
147 | case Opt_devmode: | |
148 | if (match_octal(&args[0], &option)) | |
149 | return -EINVAL; | |
150 | devmode = option & S_IRWXUGO; | |
151 | break; | |
152 | case Opt_busuid: | |
153 | if (match_int(&args[0], &option)) | |
154 | return -EINVAL; | |
155 | busuid = option; | |
156 | break; | |
157 | case Opt_busgid: | |
158 | if (match_int(&args[0], &option)) | |
159 | return -EINVAL; | |
160 | busgid = option; | |
161 | break; | |
162 | case Opt_busmode: | |
163 | if (match_octal(&args[0], &option)) | |
164 | return -EINVAL; | |
165 | busmode = option & S_IRWXUGO; | |
166 | break; | |
167 | case Opt_listuid: | |
168 | if (match_int(&args[0], &option)) | |
169 | return -EINVAL; | |
170 | listuid = option; | |
171 | break; | |
172 | case Opt_listgid: | |
173 | if (match_int(&args[0], &option)) | |
174 | return -EINVAL; | |
175 | listgid = option; | |
176 | break; | |
177 | case Opt_listmode: | |
178 | if (match_octal(&args[0], &option)) | |
179 | return -EINVAL; | |
180 | listmode = option & S_IRWXUGO; | |
181 | break; | |
182 | default: | |
183 | err("usbfs: unrecognised mount option \"%s\" " | |
184 | "or missing value\n", p); | |
185 | return -EINVAL; | |
186 | } | |
187 | } | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | static void update_special(struct dentry *special) | |
193 | { | |
194 | special->d_inode->i_uid = listuid; | |
195 | special->d_inode->i_gid = listgid; | |
196 | special->d_inode->i_mode = S_IFREG | listmode; | |
197 | } | |
198 | ||
199 | static void update_dev(struct dentry *dev) | |
200 | { | |
201 | dev->d_inode->i_uid = devuid; | |
202 | dev->d_inode->i_gid = devgid; | |
203 | dev->d_inode->i_mode = S_IFREG | devmode; | |
204 | } | |
205 | ||
206 | static void update_bus(struct dentry *bus) | |
207 | { | |
208 | struct dentry *dev = NULL; | |
209 | ||
210 | bus->d_inode->i_uid = busuid; | |
211 | bus->d_inode->i_gid = busgid; | |
212 | bus->d_inode->i_mode = S_IFDIR | busmode; | |
213 | ||
1b1dcc1b | 214 | mutex_lock(&bus->d_inode->i_mutex); |
1da177e4 | 215 | |
5160ee6f | 216 | list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child) |
1da177e4 LT |
217 | if (dev->d_inode) |
218 | update_dev(dev); | |
219 | ||
1b1dcc1b | 220 | mutex_unlock(&bus->d_inode->i_mutex); |
1da177e4 LT |
221 | } |
222 | ||
223 | static void update_sb(struct super_block *sb) | |
224 | { | |
225 | struct dentry *root = sb->s_root; | |
226 | struct dentry *bus = NULL; | |
227 | ||
228 | if (!root) | |
229 | return; | |
230 | ||
f2eace23 | 231 | mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT); |
1da177e4 | 232 | |
5160ee6f | 233 | list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) { |
1da177e4 LT |
234 | if (bus->d_inode) { |
235 | switch (S_IFMT & bus->d_inode->i_mode) { | |
236 | case S_IFDIR: | |
237 | update_bus(bus); | |
238 | break; | |
239 | case S_IFREG: | |
240 | update_special(bus); | |
241 | break; | |
242 | default: | |
3b6004f3 GKH |
243 | printk(KERN_WARNING "usbfs: Unknown node %s " |
244 | "mode %x found on remount!\n", | |
245 | bus->d_name.name, bus->d_inode->i_mode); | |
1da177e4 LT |
246 | break; |
247 | } | |
248 | } | |
249 | } | |
250 | ||
1b1dcc1b | 251 | mutex_unlock(&root->d_inode->i_mutex); |
1da177e4 LT |
252 | } |
253 | ||
254 | static int remount(struct super_block *sb, int *flags, char *data) | |
255 | { | |
256 | /* If this is not a real mount, | |
257 | * i.e. it's a simple_pin_fs from create_special_files, | |
258 | * then ignore it. | |
259 | */ | |
260 | if (ignore_mount) | |
261 | return 0; | |
262 | ||
263 | if (parse_options(sb, data)) { | |
3b6004f3 | 264 | printk(KERN_WARNING "usbfs: mount parameter error.\n"); |
1da177e4 LT |
265 | return -EINVAL; |
266 | } | |
267 | ||
268 | if (usbfs_mount && usbfs_mount->mnt_sb) | |
269 | update_sb(usbfs_mount->mnt_sb); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev) | |
275 | { | |
276 | struct inode *inode = new_inode(sb); | |
277 | ||
278 | if (inode) { | |
279 | inode->i_mode = mode; | |
280 | inode->i_uid = current->fsuid; | |
281 | inode->i_gid = current->fsgid; | |
1da177e4 LT |
282 | inode->i_blocks = 0; |
283 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
284 | switch (mode & S_IFMT) { | |
285 | default: | |
286 | init_special_inode(inode, mode, dev); | |
287 | break; | |
288 | case S_IFREG: | |
289 | inode->i_fop = &default_file_operations; | |
290 | break; | |
291 | case S_IFDIR: | |
bc7cb323 | 292 | inode->i_op = &simple_dir_inode_operations; |
1da177e4 LT |
293 | inode->i_fop = &simple_dir_operations; |
294 | ||
295 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ | |
d8c76e6f | 296 | inc_nlink(inode); |
1da177e4 LT |
297 | break; |
298 | } | |
299 | } | |
300 | return inode; | |
301 | } | |
302 | ||
303 | /* SMP-safe */ | |
304 | static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode, | |
305 | dev_t dev) | |
306 | { | |
307 | struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev); | |
308 | int error = -EPERM; | |
309 | ||
310 | if (dentry->d_inode) | |
311 | return -EEXIST; | |
312 | ||
313 | if (inode) { | |
314 | d_instantiate(dentry, inode); | |
315 | dget(dentry); | |
316 | error = 0; | |
317 | } | |
318 | return error; | |
319 | } | |
320 | ||
321 | static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode) | |
322 | { | |
323 | int res; | |
324 | ||
325 | mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR; | |
326 | res = usbfs_mknod (dir, dentry, mode, 0); | |
327 | if (!res) | |
d8c76e6f | 328 | inc_nlink(dir); |
1da177e4 LT |
329 | return res; |
330 | } | |
331 | ||
332 | static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode) | |
333 | { | |
334 | mode = (mode & S_IALLUGO) | S_IFREG; | |
335 | return usbfs_mknod (dir, dentry, mode, 0); | |
336 | } | |
337 | ||
338 | static inline int usbfs_positive (struct dentry *dentry) | |
339 | { | |
340 | return dentry->d_inode && !d_unhashed(dentry); | |
341 | } | |
342 | ||
343 | static int usbfs_empty (struct dentry *dentry) | |
344 | { | |
345 | struct list_head *list; | |
346 | ||
347 | spin_lock(&dcache_lock); | |
348 | ||
349 | list_for_each(list, &dentry->d_subdirs) { | |
5160ee6f | 350 | struct dentry *de = list_entry(list, struct dentry, d_u.d_child); |
1da177e4 LT |
351 | if (usbfs_positive(de)) { |
352 | spin_unlock(&dcache_lock); | |
353 | return 0; | |
354 | } | |
355 | } | |
356 | ||
357 | spin_unlock(&dcache_lock); | |
358 | return 1; | |
359 | } | |
360 | ||
361 | static int usbfs_unlink (struct inode *dir, struct dentry *dentry) | |
362 | { | |
363 | struct inode *inode = dentry->d_inode; | |
1b1dcc1b | 364 | mutex_lock(&inode->i_mutex); |
9a53c3a7 | 365 | drop_nlink(dentry->d_inode); |
1da177e4 | 366 | dput(dentry); |
1b1dcc1b | 367 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
368 | d_delete(dentry); |
369 | return 0; | |
370 | } | |
371 | ||
372 | static int usbfs_rmdir(struct inode *dir, struct dentry *dentry) | |
373 | { | |
374 | int error = -ENOTEMPTY; | |
375 | struct inode * inode = dentry->d_inode; | |
376 | ||
1b1dcc1b | 377 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
378 | dentry_unhash(dentry); |
379 | if (usbfs_empty(dentry)) { | |
9a53c3a7 DH |
380 | drop_nlink(dentry->d_inode); |
381 | drop_nlink(dentry->d_inode); | |
1da177e4 LT |
382 | dput(dentry); |
383 | inode->i_flags |= S_DEAD; | |
9a53c3a7 | 384 | drop_nlink(dir); |
1da177e4 LT |
385 | error = 0; |
386 | } | |
1b1dcc1b | 387 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
388 | if (!error) |
389 | d_delete(dentry); | |
390 | dput(dentry); | |
391 | return error; | |
392 | } | |
393 | ||
394 | ||
395 | /* default file operations */ | |
396 | static ssize_t default_read_file (struct file *file, char __user *buf, | |
397 | size_t count, loff_t *ppos) | |
398 | { | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static ssize_t default_write_file (struct file *file, const char __user *buf, | |
403 | size_t count, loff_t *ppos) | |
404 | { | |
405 | return count; | |
406 | } | |
407 | ||
408 | static loff_t default_file_lseek (struct file *file, loff_t offset, int orig) | |
409 | { | |
410 | loff_t retval = -EINVAL; | |
411 | ||
33cb8994 | 412 | mutex_lock(&file->f_path.dentry->d_inode->i_mutex); |
1da177e4 LT |
413 | switch(orig) { |
414 | case 0: | |
415 | if (offset > 0) { | |
416 | file->f_pos = offset; | |
417 | retval = file->f_pos; | |
418 | } | |
419 | break; | |
420 | case 1: | |
421 | if ((offset + file->f_pos) > 0) { | |
422 | file->f_pos += offset; | |
423 | retval = file->f_pos; | |
424 | } | |
425 | break; | |
426 | default: | |
427 | break; | |
428 | } | |
33cb8994 | 429 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
1da177e4 LT |
430 | return retval; |
431 | } | |
432 | ||
433 | static int default_open (struct inode *inode, struct file *file) | |
434 | { | |
8e18e294 TT |
435 | if (inode->i_private) |
436 | file->private_data = inode->i_private; | |
1da177e4 LT |
437 | |
438 | return 0; | |
439 | } | |
440 | ||
066202dd | 441 | static const struct file_operations default_file_operations = { |
1da177e4 LT |
442 | .read = default_read_file, |
443 | .write = default_write_file, | |
444 | .open = default_open, | |
445 | .llseek = default_file_lseek, | |
446 | }; | |
447 | ||
1da177e4 LT |
448 | static struct super_operations usbfs_ops = { |
449 | .statfs = simple_statfs, | |
450 | .drop_inode = generic_delete_inode, | |
451 | .remount_fs = remount, | |
2e4f3c02 | 452 | .show_options = usbfs_show_options, |
1da177e4 LT |
453 | }; |
454 | ||
455 | static int usbfs_fill_super(struct super_block *sb, void *data, int silent) | |
456 | { | |
457 | struct inode *inode; | |
458 | struct dentry *root; | |
459 | ||
460 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
461 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
462 | sb->s_magic = USBDEVICE_SUPER_MAGIC; | |
463 | sb->s_op = &usbfs_ops; | |
464 | sb->s_time_gran = 1; | |
465 | inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0); | |
466 | ||
467 | if (!inode) { | |
441b62c1 | 468 | dbg("%s: could not get inode!",__func__); |
1da177e4 LT |
469 | return -ENOMEM; |
470 | } | |
471 | ||
472 | root = d_alloc_root(inode); | |
473 | if (!root) { | |
441b62c1 | 474 | dbg("%s: could not get root dentry!",__func__); |
1da177e4 LT |
475 | iput(inode); |
476 | return -ENOMEM; | |
477 | } | |
478 | sb->s_root = root; | |
479 | return 0; | |
480 | } | |
481 | ||
1da177e4 LT |
482 | /* |
483 | * fs_create_by_name - create a file, given a name | |
484 | * @name: name of file | |
485 | * @mode: type of file | |
486 | * @parent: dentry of directory to create it in | |
487 | * @dentry: resulting dentry of file | |
488 | * | |
489 | * This function handles both regular files and directories. | |
490 | */ | |
491 | static int fs_create_by_name (const char *name, mode_t mode, | |
492 | struct dentry *parent, struct dentry **dentry) | |
493 | { | |
494 | int error = 0; | |
495 | ||
496 | /* If the parent is not specified, we create it in the root. | |
497 | * We need the root dentry to do this, which is in the super | |
498 | * block. A pointer to that is in the struct vfsmount that we | |
499 | * have around. | |
500 | */ | |
501 | if (!parent ) { | |
502 | if (usbfs_mount && usbfs_mount->mnt_sb) { | |
503 | parent = usbfs_mount->mnt_sb->s_root; | |
504 | } | |
505 | } | |
506 | ||
507 | if (!parent) { | |
508 | dbg("Ah! can not find a parent!"); | |
509 | return -EFAULT; | |
510 | } | |
511 | ||
512 | *dentry = NULL; | |
1b1dcc1b | 513 | mutex_lock(&parent->d_inode->i_mutex); |
5f45f1a7 | 514 | *dentry = lookup_one_len(name, parent, strlen(name)); |
1da177e4 LT |
515 | if (!IS_ERR(dentry)) { |
516 | if ((mode & S_IFMT) == S_IFDIR) | |
517 | error = usbfs_mkdir (parent->d_inode, *dentry, mode); | |
518 | else | |
519 | error = usbfs_create (parent->d_inode, *dentry, mode); | |
520 | } else | |
521 | error = PTR_ERR(dentry); | |
1b1dcc1b | 522 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
523 | |
524 | return error; | |
525 | } | |
526 | ||
527 | static struct dentry *fs_create_file (const char *name, mode_t mode, | |
528 | struct dentry *parent, void *data, | |
066202dd | 529 | const struct file_operations *fops, |
1da177e4 LT |
530 | uid_t uid, gid_t gid) |
531 | { | |
532 | struct dentry *dentry; | |
533 | int error; | |
534 | ||
535 | dbg("creating file '%s'",name); | |
536 | ||
537 | error = fs_create_by_name (name, mode, parent, &dentry); | |
538 | if (error) { | |
539 | dentry = NULL; | |
540 | } else { | |
541 | if (dentry->d_inode) { | |
542 | if (data) | |
8e18e294 | 543 | dentry->d_inode->i_private = data; |
1da177e4 LT |
544 | if (fops) |
545 | dentry->d_inode->i_fop = fops; | |
546 | dentry->d_inode->i_uid = uid; | |
547 | dentry->d_inode->i_gid = gid; | |
548 | } | |
549 | } | |
550 | ||
551 | return dentry; | |
552 | } | |
553 | ||
554 | static void fs_remove_file (struct dentry *dentry) | |
555 | { | |
556 | struct dentry *parent = dentry->d_parent; | |
557 | ||
558 | if (!parent || !parent->d_inode) | |
559 | return; | |
560 | ||
8e7795ef | 561 | mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT); |
1da177e4 LT |
562 | if (usbfs_positive(dentry)) { |
563 | if (dentry->d_inode) { | |
564 | if (S_ISDIR(dentry->d_inode->i_mode)) | |
565 | usbfs_rmdir(parent->d_inode, dentry); | |
566 | else | |
567 | usbfs_unlink(parent->d_inode, dentry); | |
568 | dput(dentry); | |
569 | } | |
570 | } | |
1b1dcc1b | 571 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
572 | } |
573 | ||
574 | /* --------------------------------------------------------------------- */ | |
575 | ||
454e2398 DH |
576 | static int usb_get_sb(struct file_system_type *fs_type, |
577 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
1da177e4 | 578 | { |
454e2398 | 579 | return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt); |
1da177e4 LT |
580 | } |
581 | ||
582 | static struct file_system_type usb_fs_type = { | |
583 | .owner = THIS_MODULE, | |
584 | .name = "usbfs", | |
585 | .get_sb = usb_get_sb, | |
586 | .kill_sb = kill_litter_super, | |
587 | }; | |
588 | ||
589 | /* --------------------------------------------------------------------- */ | |
590 | ||
591 | static int create_special_files (void) | |
592 | { | |
593 | struct dentry *parent; | |
594 | int retval; | |
595 | ||
596 | /* the simple_pin_fs calls will call remount with no options | |
597 | * without this flag that would overwrite the real mount options (if any) | |
598 | */ | |
599 | ignore_mount = 1; | |
600 | ||
601 | /* create the devices special file */ | |
1f5ce9e9 | 602 | retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count); |
1da177e4 LT |
603 | if (retval) { |
604 | err ("Unable to get usbfs mount"); | |
605 | goto exit; | |
606 | } | |
607 | ||
608 | ignore_mount = 0; | |
609 | ||
610 | parent = usbfs_mount->mnt_sb->s_root; | |
611 | devices_usbfs_dentry = fs_create_file ("devices", | |
612 | listmode | S_IFREG, parent, | |
613 | NULL, &usbfs_devices_fops, | |
614 | listuid, listgid); | |
615 | if (devices_usbfs_dentry == NULL) { | |
616 | err ("Unable to create devices usbfs file"); | |
617 | retval = -ENODEV; | |
618 | goto error_clean_mounts; | |
619 | } | |
620 | ||
621 | goto exit; | |
622 | ||
623 | error_clean_mounts: | |
624 | simple_release_fs(&usbfs_mount, &usbfs_mount_count); | |
625 | exit: | |
626 | return retval; | |
627 | } | |
628 | ||
629 | static void remove_special_files (void) | |
630 | { | |
631 | if (devices_usbfs_dentry) | |
632 | fs_remove_file (devices_usbfs_dentry); | |
633 | devices_usbfs_dentry = NULL; | |
634 | simple_release_fs(&usbfs_mount, &usbfs_mount_count); | |
635 | } | |
636 | ||
637 | void usbfs_update_special (void) | |
638 | { | |
639 | struct inode *inode; | |
640 | ||
641 | if (devices_usbfs_dentry) { | |
642 | inode = devices_usbfs_dentry->d_inode; | |
643 | if (inode) | |
644 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
645 | } | |
646 | } | |
647 | ||
54a5c4cd | 648 | static void usbfs_add_bus(struct usb_bus *bus) |
1da177e4 LT |
649 | { |
650 | struct dentry *parent; | |
651 | char name[8]; | |
652 | int retval; | |
653 | ||
654 | /* create the special files if this is the first bus added */ | |
655 | if (num_buses == 0) { | |
656 | retval = create_special_files(); | |
657 | if (retval) | |
658 | return; | |
659 | } | |
660 | ++num_buses; | |
661 | ||
662 | sprintf (name, "%03d", bus->busnum); | |
663 | ||
664 | parent = usbfs_mount->mnt_sb->s_root; | |
665 | bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent, | |
666 | bus, NULL, busuid, busgid); | |
667 | if (bus->usbfs_dentry == NULL) { | |
668 | err ("error creating usbfs bus entry"); | |
669 | return; | |
670 | } | |
1da177e4 LT |
671 | } |
672 | ||
54a5c4cd | 673 | static void usbfs_remove_bus(struct usb_bus *bus) |
1da177e4 LT |
674 | { |
675 | if (bus->usbfs_dentry) { | |
676 | fs_remove_file (bus->usbfs_dentry); | |
677 | bus->usbfs_dentry = NULL; | |
678 | } | |
679 | ||
680 | --num_buses; | |
681 | if (num_buses <= 0) { | |
682 | remove_special_files(); | |
683 | num_buses = 0; | |
684 | } | |
1da177e4 LT |
685 | } |
686 | ||
54a5c4cd | 687 | static void usbfs_add_device(struct usb_device *dev) |
1da177e4 LT |
688 | { |
689 | char name[8]; | |
690 | int i; | |
691 | int i_size; | |
692 | ||
693 | sprintf (name, "%03d", dev->devnum); | |
694 | dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG, | |
695 | dev->bus->usbfs_dentry, dev, | |
9f8b17e6 | 696 | &usbdev_file_operations, |
1da177e4 LT |
697 | devuid, devgid); |
698 | if (dev->usbfs_dentry == NULL) { | |
699 | err ("error creating usbfs device entry"); | |
700 | return; | |
701 | } | |
702 | ||
703 | /* Set the size of the device's file to be | |
704 | * equal to the size of the device descriptors. */ | |
705 | i_size = sizeof (struct usb_device_descriptor); | |
706 | for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) { | |
707 | struct usb_config_descriptor *config = | |
708 | (struct usb_config_descriptor *)dev->rawdescriptors[i]; | |
709 | i_size += le16_to_cpu(config->wTotalLength); | |
710 | } | |
711 | if (dev->usbfs_dentry->d_inode) | |
712 | dev->usbfs_dentry->d_inode->i_size = i_size; | |
1da177e4 LT |
713 | } |
714 | ||
54a5c4cd | 715 | static void usbfs_remove_device(struct usb_device *dev) |
1da177e4 | 716 | { |
1da177e4 LT |
717 | if (dev->usbfs_dentry) { |
718 | fs_remove_file (dev->usbfs_dentry); | |
719 | dev->usbfs_dentry = NULL; | |
720 | } | |
cd9f0375 | 721 | usb_fs_classdev_common_remove(dev); |
54a5c4cd GKH |
722 | } |
723 | ||
724 | static 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 | ||
1da177e4 LT |
740 | usbfs_update_special(); |
741 | usbfs_conn_disc_event(); | |
54a5c4cd | 742 | return NOTIFY_OK; |
1da177e4 LT |
743 | } |
744 | ||
54a5c4cd GKH |
745 | static struct notifier_block usbfs_nb = { |
746 | .notifier_call = usbfs_notify, | |
747 | }; | |
748 | ||
1da177e4 LT |
749 | /* --------------------------------------------------------------------- */ |
750 | ||
751 | static struct proc_dir_entry *usbdir = NULL; | |
752 | ||
753 | int __init usbfs_init(void) | |
754 | { | |
755 | int retval; | |
756 | ||
1da177e4 | 757 | retval = register_filesystem(&usb_fs_type); |
fbf82fd2 | 758 | if (retval) |
1da177e4 | 759 | return retval; |
1da177e4 | 760 | |
54a5c4cd GKH |
761 | usb_register_notify(&usbfs_nb); |
762 | ||
1da177e4 | 763 | /* create mount point for usbfs */ |
9c37066d | 764 | usbdir = proc_mkdir("bus/usb", NULL); |
1da177e4 LT |
765 | |
766 | return 0; | |
767 | } | |
768 | ||
769 | void usbfs_cleanup(void) | |
770 | { | |
54a5c4cd | 771 | usb_unregister_notify(&usbfs_nb); |
1da177e4 LT |
772 | unregister_filesystem(&usb_fs_type); |
773 | if (usbdir) | |
9c37066d | 774 | remove_proc_entry("bus/usb", NULL); |
1da177e4 LT |
775 | } |
776 |