inode.c 18 KB

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