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