inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*****************************************************************************/
  2. /*
  3. * inode.c -- Inode/Dentry functions for the USB device file system.
  4. *
  5. * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * History:
  23. * 0.1 04.01.2000 Created
  24. * 0.2 10.12.2001 converted to use the vfs layer better
  25. */
  26. /*****************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/fs.h>
  29. #include <linux/mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/init.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/usb.h>
  34. #include <linux/namei.h>
  35. #include <linux/usbdevice_fs.h>
  36. #include <linux/parser.h>
  37. #include <linux/notifier.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/usb/hcd.h>
  40. #include <asm/byteorder.h>
  41. #include "usb.h"
  42. #define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
  43. #define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
  44. #define USBFS_DEFAULT_LISTMODE S_IRUGO
  45. static const struct file_operations default_file_operations;
  46. static struct vfsmount *usbfs_mount;
  47. static int usbfs_mount_count; /* = 0 */
  48. static struct dentry *devices_usbfs_dentry;
  49. static int num_buses; /* = 0 */
  50. static uid_t devuid; /* = 0 */
  51. static uid_t busuid; /* = 0 */
  52. static uid_t listuid; /* = 0 */
  53. static gid_t devgid; /* = 0 */
  54. static gid_t busgid; /* = 0 */
  55. static gid_t listgid; /* = 0 */
  56. static umode_t devmode = USBFS_DEFAULT_DEVMODE;
  57. static umode_t busmode = USBFS_DEFAULT_BUSMODE;
  58. static umode_t listmode = USBFS_DEFAULT_LISTMODE;
  59. static int usbfs_show_options(struct seq_file *seq, struct dentry *root)
  60. {
  61. if (devuid != 0)
  62. seq_printf(seq, ",devuid=%u", devuid);
  63. if (devgid != 0)
  64. seq_printf(seq, ",devgid=%u", devgid);
  65. if (devmode != USBFS_DEFAULT_DEVMODE)
  66. seq_printf(seq, ",devmode=%o", devmode);
  67. if (busuid != 0)
  68. seq_printf(seq, ",busuid=%u", busuid);
  69. if (busgid != 0)
  70. seq_printf(seq, ",busgid=%u", busgid);
  71. if (busmode != USBFS_DEFAULT_BUSMODE)
  72. seq_printf(seq, ",busmode=%o", busmode);
  73. if (listuid != 0)
  74. seq_printf(seq, ",listuid=%u", listuid);
  75. if (listgid != 0)
  76. seq_printf(seq, ",listgid=%u", listgid);
  77. if (listmode != USBFS_DEFAULT_LISTMODE)
  78. seq_printf(seq, ",listmode=%o", listmode);
  79. return 0;
  80. }
  81. enum {
  82. Opt_devuid, Opt_devgid, Opt_devmode,
  83. Opt_busuid, Opt_busgid, Opt_busmode,
  84. Opt_listuid, Opt_listgid, Opt_listmode,
  85. Opt_err,
  86. };
  87. static const match_table_t tokens = {
  88. {Opt_devuid, "devuid=%u"},
  89. {Opt_devgid, "devgid=%u"},
  90. {Opt_devmode, "devmode=%o"},
  91. {Opt_busuid, "busuid=%u"},
  92. {Opt_busgid, "busgid=%u"},
  93. {Opt_busmode, "busmode=%o"},
  94. {Opt_listuid, "listuid=%u"},
  95. {Opt_listgid, "listgid=%u"},
  96. {Opt_listmode, "listmode=%o"},
  97. {Opt_err, NULL}
  98. };
  99. static int parse_options(struct super_block *s, char *data)
  100. {
  101. char *p;
  102. int option;
  103. /* (re)set to defaults. */
  104. devuid = 0;
  105. busuid = 0;
  106. listuid = 0;
  107. devgid = 0;
  108. busgid = 0;
  109. listgid = 0;
  110. devmode = USBFS_DEFAULT_DEVMODE;
  111. busmode = USBFS_DEFAULT_BUSMODE;
  112. listmode = USBFS_DEFAULT_LISTMODE;
  113. while ((p = strsep(&data, ",")) != NULL) {
  114. substring_t args[MAX_OPT_ARGS];
  115. int token;
  116. if (!*p)
  117. continue;
  118. token = match_token(p, tokens, args);
  119. switch (token) {
  120. case Opt_devuid:
  121. if (match_int(&args[0], &option))
  122. return -EINVAL;
  123. devuid = option;
  124. break;
  125. case Opt_devgid:
  126. if (match_int(&args[0], &option))
  127. return -EINVAL;
  128. devgid = option;
  129. break;
  130. case Opt_devmode:
  131. if (match_octal(&args[0], &option))
  132. return -EINVAL;
  133. devmode = option & S_IRWXUGO;
  134. break;
  135. case Opt_busuid:
  136. if (match_int(&args[0], &option))
  137. return -EINVAL;
  138. busuid = option;
  139. break;
  140. case Opt_busgid:
  141. if (match_int(&args[0], &option))
  142. return -EINVAL;
  143. busgid = option;
  144. break;
  145. case Opt_busmode:
  146. if (match_octal(&args[0], &option))
  147. return -EINVAL;
  148. busmode = option & S_IRWXUGO;
  149. break;
  150. case Opt_listuid:
  151. if (match_int(&args[0], &option))
  152. return -EINVAL;
  153. listuid = option;
  154. break;
  155. case Opt_listgid:
  156. if (match_int(&args[0], &option))
  157. return -EINVAL;
  158. listgid = option;
  159. break;
  160. case Opt_listmode:
  161. if (match_octal(&args[0], &option))
  162. return -EINVAL;
  163. listmode = option & S_IRWXUGO;
  164. break;
  165. default:
  166. printk(KERN_ERR "usbfs: unrecognised mount option "
  167. "\"%s\" or missing value\n", p);
  168. return -EINVAL;
  169. }
  170. }
  171. return 0;
  172. }
  173. static void update_special(struct dentry *special)
  174. {
  175. special->d_inode->i_uid = listuid;
  176. special->d_inode->i_gid = listgid;
  177. special->d_inode->i_mode = S_IFREG | listmode;
  178. }
  179. static void update_dev(struct dentry *dev)
  180. {
  181. dev->d_inode->i_uid = devuid;
  182. dev->d_inode->i_gid = devgid;
  183. dev->d_inode->i_mode = S_IFREG | devmode;
  184. }
  185. static void update_bus(struct dentry *bus)
  186. {
  187. struct dentry *dev = NULL;
  188. bus->d_inode->i_uid = busuid;
  189. bus->d_inode->i_gid = busgid;
  190. bus->d_inode->i_mode = S_IFDIR | busmode;
  191. mutex_lock(&bus->d_inode->i_mutex);
  192. list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
  193. if (dev->d_inode)
  194. update_dev(dev);
  195. mutex_unlock(&bus->d_inode->i_mutex);
  196. }
  197. static void update_sb(struct super_block *sb)
  198. {
  199. struct dentry *root = sb->s_root;
  200. struct dentry *bus = NULL;
  201. if (!root)
  202. return;
  203. mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
  204. list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
  205. if (bus->d_inode) {
  206. switch (S_IFMT & bus->d_inode->i_mode) {
  207. case S_IFDIR:
  208. update_bus(bus);
  209. break;
  210. case S_IFREG:
  211. update_special(bus);
  212. break;
  213. default:
  214. printk(KERN_WARNING "usbfs: Unknown node %s "
  215. "mode %x found on remount!\n",
  216. bus->d_name.name, bus->d_inode->i_mode);
  217. break;
  218. }
  219. }
  220. }
  221. mutex_unlock(&root->d_inode->i_mutex);
  222. }
  223. static int remount(struct super_block *sb, int *flags, char *data)
  224. {
  225. /* If this is not a real mount,
  226. * i.e. it's a simple_pin_fs from create_special_files,
  227. * then ignore it.
  228. */
  229. if (*flags & MS_KERNMOUNT)
  230. return 0;
  231. if (parse_options(sb, data)) {
  232. printk(KERN_WARNING "usbfs: mount parameter error.\n");
  233. return -EINVAL;
  234. }
  235. if (usbfs_mount)
  236. update_sb(usbfs_mount->mnt_sb);
  237. return 0;
  238. }
  239. static struct inode *usbfs_get_inode (struct super_block *sb, umode_t mode, dev_t dev)
  240. {
  241. struct inode *inode = new_inode(sb);
  242. if (inode) {
  243. inode->i_ino = get_next_ino();
  244. inode_init_owner(inode, NULL, mode);
  245. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  246. switch (mode & S_IFMT) {
  247. default:
  248. init_special_inode(inode, mode, dev);
  249. break;
  250. case S_IFREG:
  251. inode->i_fop = &default_file_operations;
  252. break;
  253. case S_IFDIR:
  254. inode->i_op = &simple_dir_inode_operations;
  255. inode->i_fop = &simple_dir_operations;
  256. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  257. inc_nlink(inode);
  258. break;
  259. }
  260. }
  261. return inode;
  262. }
  263. /* SMP-safe */
  264. static int usbfs_mknod (struct inode *dir, struct dentry *dentry, umode_t mode,
  265. dev_t dev)
  266. {
  267. struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
  268. int error = -EPERM;
  269. if (dentry->d_inode)
  270. return -EEXIST;
  271. if (inode) {
  272. d_instantiate(dentry, inode);
  273. dget(dentry);
  274. error = 0;
  275. }
  276. return error;
  277. }
  278. static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, umode_t mode)
  279. {
  280. int res;
  281. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  282. res = usbfs_mknod (dir, dentry, mode, 0);
  283. if (!res)
  284. inc_nlink(dir);
  285. return res;
  286. }
  287. static int usbfs_create (struct inode *dir, struct dentry *dentry, umode_t mode)
  288. {
  289. mode = (mode & S_IALLUGO) | S_IFREG;
  290. return usbfs_mknod (dir, dentry, mode, 0);
  291. }
  292. static inline int usbfs_positive (struct dentry *dentry)
  293. {
  294. return dentry->d_inode && !d_unhashed(dentry);
  295. }
  296. static int usbfs_empty (struct dentry *dentry)
  297. {
  298. struct list_head *list;
  299. spin_lock(&dentry->d_lock);
  300. list_for_each(list, &dentry->d_subdirs) {
  301. struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
  302. spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
  303. if (usbfs_positive(de)) {
  304. spin_unlock(&de->d_lock);
  305. spin_unlock(&dentry->d_lock);
  306. return 0;
  307. }
  308. spin_unlock(&de->d_lock);
  309. }
  310. spin_unlock(&dentry->d_lock);
  311. return 1;
  312. }
  313. static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
  314. {
  315. struct inode *inode = dentry->d_inode;
  316. mutex_lock(&inode->i_mutex);
  317. drop_nlink(dentry->d_inode);
  318. dput(dentry);
  319. mutex_unlock(&inode->i_mutex);
  320. d_delete(dentry);
  321. return 0;
  322. }
  323. static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
  324. {
  325. int error = -ENOTEMPTY;
  326. struct inode * inode = dentry->d_inode;
  327. mutex_lock(&inode->i_mutex);
  328. dentry_unhash(dentry);
  329. if (usbfs_empty(dentry)) {
  330. dont_mount(dentry);
  331. drop_nlink(dentry->d_inode);
  332. drop_nlink(dentry->d_inode);
  333. dput(dentry);
  334. inode->i_flags |= S_DEAD;
  335. drop_nlink(dir);
  336. error = 0;
  337. }
  338. mutex_unlock(&inode->i_mutex);
  339. if (!error)
  340. d_delete(dentry);
  341. return error;
  342. }
  343. /* default file operations */
  344. static ssize_t default_read_file (struct file *file, char __user *buf,
  345. size_t count, loff_t *ppos)
  346. {
  347. return 0;
  348. }
  349. static ssize_t default_write_file (struct file *file, const char __user *buf,
  350. size_t count, loff_t *ppos)
  351. {
  352. return count;
  353. }
  354. static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
  355. {
  356. loff_t retval = -EINVAL;
  357. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  358. switch(orig) {
  359. case 0:
  360. if (offset > 0) {
  361. file->f_pos = offset;
  362. retval = file->f_pos;
  363. }
  364. break;
  365. case 1:
  366. if ((offset + file->f_pos) > 0) {
  367. file->f_pos += offset;
  368. retval = file->f_pos;
  369. }
  370. break;
  371. default:
  372. break;
  373. }
  374. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  375. return retval;
  376. }
  377. static int default_open (struct inode *inode, struct file *file)
  378. {
  379. if (inode->i_private)
  380. file->private_data = inode->i_private;
  381. return 0;
  382. }
  383. static const struct file_operations default_file_operations = {
  384. .read = default_read_file,
  385. .write = default_write_file,
  386. .open = default_open,
  387. .llseek = default_file_lseek,
  388. };
  389. static const struct super_operations usbfs_ops = {
  390. .statfs = simple_statfs,
  391. .drop_inode = generic_delete_inode,
  392. .remount_fs = remount,
  393. .show_options = usbfs_show_options,
  394. };
  395. static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
  396. {
  397. struct inode *inode;
  398. sb->s_blocksize = PAGE_CACHE_SIZE;
  399. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  400. sb->s_magic = USBDEVICE_SUPER_MAGIC;
  401. sb->s_op = &usbfs_ops;
  402. sb->s_time_gran = 1;
  403. inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
  404. sb->s_root = d_make_root(inode);
  405. if (!sb->s_root) {
  406. dbg("%s: could not get root dentry!",__func__);
  407. return -ENOMEM;
  408. }
  409. return 0;
  410. }
  411. /*
  412. * fs_create_by_name - create a file, given a name
  413. * @name: name of file
  414. * @mode: type of file
  415. * @parent: dentry of directory to create it in
  416. * @dentry: resulting dentry of file
  417. *
  418. * This function handles both regular files and directories.
  419. */
  420. static int fs_create_by_name (const char *name, umode_t mode,
  421. struct dentry *parent, struct dentry **dentry)
  422. {
  423. int error = 0;
  424. /* If the parent is not specified, we create it in the root.
  425. * We need the root dentry to do this, which is in the super
  426. * block. A pointer to that is in the struct vfsmount that we
  427. * have around.
  428. */
  429. if (!parent ) {
  430. if (usbfs_mount)
  431. parent = usbfs_mount->mnt_root;
  432. }
  433. if (!parent) {
  434. dbg("Ah! can not find a parent!");
  435. return -EFAULT;
  436. }
  437. *dentry = NULL;
  438. mutex_lock(&parent->d_inode->i_mutex);
  439. *dentry = lookup_one_len(name, parent, strlen(name));
  440. if (!IS_ERR(*dentry)) {
  441. if (S_ISDIR(mode))
  442. error = usbfs_mkdir (parent->d_inode, *dentry, mode);
  443. else
  444. error = usbfs_create (parent->d_inode, *dentry, mode);
  445. } else
  446. error = PTR_ERR(*dentry);
  447. mutex_unlock(&parent->d_inode->i_mutex);
  448. return error;
  449. }
  450. static struct dentry *fs_create_file (const char *name, umode_t mode,
  451. struct dentry *parent, void *data,
  452. const struct file_operations *fops,
  453. uid_t uid, gid_t gid)
  454. {
  455. struct dentry *dentry;
  456. int error;
  457. dbg("creating file '%s'",name);
  458. error = fs_create_by_name (name, mode, parent, &dentry);
  459. if (error) {
  460. dentry = NULL;
  461. } else {
  462. if (dentry->d_inode) {
  463. if (data)
  464. dentry->d_inode->i_private = data;
  465. if (fops)
  466. dentry->d_inode->i_fop = fops;
  467. dentry->d_inode->i_uid = uid;
  468. dentry->d_inode->i_gid = gid;
  469. }
  470. }
  471. return dentry;
  472. }
  473. static void fs_remove_file (struct dentry *dentry)
  474. {
  475. struct dentry *parent = dentry->d_parent;
  476. if (!parent || !parent->d_inode)
  477. return;
  478. mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
  479. if (usbfs_positive(dentry)) {
  480. if (dentry->d_inode) {
  481. if (S_ISDIR(dentry->d_inode->i_mode))
  482. usbfs_rmdir(parent->d_inode, dentry);
  483. else
  484. usbfs_unlink(parent->d_inode, dentry);
  485. dput(dentry);
  486. }
  487. }
  488. mutex_unlock(&parent->d_inode->i_mutex);
  489. }
  490. /* --------------------------------------------------------------------- */
  491. static struct dentry *usb_mount(struct file_system_type *fs_type,
  492. int flags, const char *dev_name, void *data)
  493. {
  494. return mount_single(fs_type, flags, data, usbfs_fill_super);
  495. }
  496. static struct file_system_type usb_fs_type = {
  497. .owner = THIS_MODULE,
  498. .name = "usbfs",
  499. .mount = usb_mount,
  500. .kill_sb = kill_litter_super,
  501. };
  502. /* --------------------------------------------------------------------- */
  503. static int create_special_files (void)
  504. {
  505. struct dentry *parent;
  506. int retval;
  507. /* create the devices special file */
  508. retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
  509. if (retval) {
  510. printk(KERN_ERR "Unable to get usbfs mount\n");
  511. goto exit;
  512. }
  513. parent = usbfs_mount->mnt_root;
  514. devices_usbfs_dentry = fs_create_file ("devices",
  515. listmode | S_IFREG, parent,
  516. NULL, &usbfs_devices_fops,
  517. listuid, listgid);
  518. if (devices_usbfs_dentry == NULL) {
  519. printk(KERN_ERR "Unable to create devices usbfs file\n");
  520. retval = -ENODEV;
  521. goto error_clean_mounts;
  522. }
  523. goto exit;
  524. error_clean_mounts:
  525. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  526. exit:
  527. return retval;
  528. }
  529. static void remove_special_files (void)
  530. {
  531. if (devices_usbfs_dentry)
  532. fs_remove_file (devices_usbfs_dentry);
  533. devices_usbfs_dentry = NULL;
  534. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  535. }
  536. void usbfs_update_special (void)
  537. {
  538. struct inode *inode;
  539. if (devices_usbfs_dentry) {
  540. inode = devices_usbfs_dentry->d_inode;
  541. if (inode)
  542. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  543. }
  544. }
  545. static void usbfs_add_bus(struct usb_bus *bus)
  546. {
  547. struct dentry *parent;
  548. char name[8];
  549. int retval;
  550. /* create the special files if this is the first bus added */
  551. if (num_buses == 0) {
  552. retval = create_special_files();
  553. if (retval)
  554. return;
  555. }
  556. ++num_buses;
  557. sprintf (name, "%03d", bus->busnum);
  558. parent = usbfs_mount->mnt_root;
  559. bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
  560. bus, NULL, busuid, busgid);
  561. if (bus->usbfs_dentry == NULL) {
  562. printk(KERN_ERR "Error creating usbfs bus entry\n");
  563. return;
  564. }
  565. }
  566. static void usbfs_remove_bus(struct usb_bus *bus)
  567. {
  568. if (bus->usbfs_dentry) {
  569. fs_remove_file (bus->usbfs_dentry);
  570. bus->usbfs_dentry = NULL;
  571. }
  572. --num_buses;
  573. if (num_buses <= 0) {
  574. remove_special_files();
  575. num_buses = 0;
  576. }
  577. }
  578. static void usbfs_add_device(struct usb_device *dev)
  579. {
  580. char name[8];
  581. int i;
  582. int i_size;
  583. sprintf (name, "%03d", dev->devnum);
  584. dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
  585. dev->bus->usbfs_dentry, dev,
  586. &usbdev_file_operations,
  587. devuid, devgid);
  588. if (dev->usbfs_dentry == NULL) {
  589. printk(KERN_ERR "Error creating usbfs device entry\n");
  590. return;
  591. }
  592. /* Set the size of the device's file to be
  593. * equal to the size of the device descriptors. */
  594. i_size = sizeof (struct usb_device_descriptor);
  595. for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
  596. struct usb_config_descriptor *config =
  597. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  598. i_size += le16_to_cpu(config->wTotalLength);
  599. }
  600. if (dev->usbfs_dentry->d_inode)
  601. dev->usbfs_dentry->d_inode->i_size = i_size;
  602. }
  603. static void usbfs_remove_device(struct usb_device *dev)
  604. {
  605. if (dev->usbfs_dentry) {
  606. fs_remove_file (dev->usbfs_dentry);
  607. dev->usbfs_dentry = NULL;
  608. }
  609. }
  610. static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
  611. {
  612. switch (action) {
  613. case USB_DEVICE_ADD:
  614. usbfs_add_device(dev);
  615. break;
  616. case USB_DEVICE_REMOVE:
  617. usbfs_remove_device(dev);
  618. break;
  619. case USB_BUS_ADD:
  620. usbfs_add_bus(dev);
  621. break;
  622. case USB_BUS_REMOVE:
  623. usbfs_remove_bus(dev);
  624. }
  625. usbfs_update_special();
  626. usbfs_conn_disc_event();
  627. return NOTIFY_OK;
  628. }
  629. static struct notifier_block usbfs_nb = {
  630. .notifier_call = usbfs_notify,
  631. };
  632. /* --------------------------------------------------------------------- */
  633. static struct proc_dir_entry *usbdir = NULL;
  634. int __init usbfs_init(void)
  635. {
  636. int retval;
  637. retval = register_filesystem(&usb_fs_type);
  638. if (retval)
  639. return retval;
  640. usb_register_notify(&usbfs_nb);
  641. /* create mount point for usbfs */
  642. usbdir = proc_mkdir("bus/usb", NULL);
  643. return 0;
  644. }
  645. void usbfs_cleanup(void)
  646. {
  647. usb_unregister_notify(&usbfs_nb);
  648. unregister_filesystem(&usb_fs_type);
  649. if (usbdir)
  650. remove_proc_entry("bus/usb", NULL);
  651. }