inode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 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. err("usbfs: unrecognised mount option \"%s\" "
  169. "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. warn("Unknown node %s mode %x found on remount!\n",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 (ignore_mount)
  230. return 0;
  231. if (parse_options(sb, data)) {
  232. warn("usbfs: mount parameter error:");
  233. return -EINVAL;
  234. }
  235. if (usbfs_mount && usbfs_mount->mnt_sb)
  236. update_sb(usbfs_mount->mnt_sb);
  237. return 0;
  238. }
  239. static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
  240. {
  241. struct inode *inode = new_inode(sb);
  242. if (inode) {
  243. inode->i_mode = mode;
  244. inode->i_uid = current->fsuid;
  245. inode->i_gid = current->fsgid;
  246. inode->i_blocks = 0;
  247. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  248. switch (mode & S_IFMT) {
  249. default:
  250. init_special_inode(inode, mode, dev);
  251. break;
  252. case S_IFREG:
  253. inode->i_fop = &default_file_operations;
  254. break;
  255. case S_IFDIR:
  256. inode->i_op = &simple_dir_inode_operations;
  257. inode->i_fop = &simple_dir_operations;
  258. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  259. inc_nlink(inode);
  260. break;
  261. }
  262. }
  263. return inode;
  264. }
  265. /* SMP-safe */
  266. static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
  267. dev_t dev)
  268. {
  269. struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
  270. int error = -EPERM;
  271. if (dentry->d_inode)
  272. return -EEXIST;
  273. if (inode) {
  274. d_instantiate(dentry, inode);
  275. dget(dentry);
  276. error = 0;
  277. }
  278. return error;
  279. }
  280. static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
  281. {
  282. int res;
  283. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  284. res = usbfs_mknod (dir, dentry, mode, 0);
  285. if (!res)
  286. inc_nlink(dir);
  287. return res;
  288. }
  289. static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
  290. {
  291. mode = (mode & S_IALLUGO) | S_IFREG;
  292. return usbfs_mknod (dir, dentry, mode, 0);
  293. }
  294. static inline int usbfs_positive (struct dentry *dentry)
  295. {
  296. return dentry->d_inode && !d_unhashed(dentry);
  297. }
  298. static int usbfs_empty (struct dentry *dentry)
  299. {
  300. struct list_head *list;
  301. spin_lock(&dcache_lock);
  302. list_for_each(list, &dentry->d_subdirs) {
  303. struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
  304. if (usbfs_positive(de)) {
  305. spin_unlock(&dcache_lock);
  306. return 0;
  307. }
  308. }
  309. spin_unlock(&dcache_lock);
  310. return 1;
  311. }
  312. static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
  313. {
  314. struct inode *inode = dentry->d_inode;
  315. mutex_lock(&inode->i_mutex);
  316. drop_nlink(dentry->d_inode);
  317. dput(dentry);
  318. mutex_unlock(&inode->i_mutex);
  319. d_delete(dentry);
  320. return 0;
  321. }
  322. static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
  323. {
  324. int error = -ENOTEMPTY;
  325. struct inode * inode = dentry->d_inode;
  326. mutex_lock(&inode->i_mutex);
  327. dentry_unhash(dentry);
  328. if (usbfs_empty(dentry)) {
  329. drop_nlink(dentry->d_inode);
  330. drop_nlink(dentry->d_inode);
  331. dput(dentry);
  332. inode->i_flags |= S_DEAD;
  333. drop_nlink(dir);
  334. error = 0;
  335. }
  336. mutex_unlock(&inode->i_mutex);
  337. if (!error)
  338. d_delete(dentry);
  339. dput(dentry);
  340. return error;
  341. }
  342. /* default file operations */
  343. static ssize_t default_read_file (struct file *file, char __user *buf,
  344. size_t count, loff_t *ppos)
  345. {
  346. return 0;
  347. }
  348. static ssize_t default_write_file (struct file *file, const char __user *buf,
  349. size_t count, loff_t *ppos)
  350. {
  351. return count;
  352. }
  353. static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
  354. {
  355. loff_t retval = -EINVAL;
  356. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  357. switch(orig) {
  358. case 0:
  359. if (offset > 0) {
  360. file->f_pos = offset;
  361. retval = file->f_pos;
  362. }
  363. break;
  364. case 1:
  365. if ((offset + file->f_pos) > 0) {
  366. file->f_pos += offset;
  367. retval = file->f_pos;
  368. }
  369. break;
  370. default:
  371. break;
  372. }
  373. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  374. return retval;
  375. }
  376. static int default_open (struct inode *inode, struct file *file)
  377. {
  378. if (inode->i_private)
  379. file->private_data = inode->i_private;
  380. return 0;
  381. }
  382. static const struct file_operations default_file_operations = {
  383. .read = default_read_file,
  384. .write = default_write_file,
  385. .open = default_open,
  386. .llseek = default_file_lseek,
  387. };
  388. static struct super_operations usbfs_ops = {
  389. .statfs = simple_statfs,
  390. .drop_inode = generic_delete_inode,
  391. .remount_fs = remount,
  392. .show_options = usbfs_show_options,
  393. };
  394. static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
  395. {
  396. struct inode *inode;
  397. struct dentry *root;
  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. if (!inode) {
  405. dbg("%s: could not get inode!",__func__);
  406. return -ENOMEM;
  407. }
  408. root = d_alloc_root(inode);
  409. if (!root) {
  410. dbg("%s: could not get root dentry!",__func__);
  411. iput(inode);
  412. return -ENOMEM;
  413. }
  414. sb->s_root = root;
  415. return 0;
  416. }
  417. /*
  418. * fs_create_by_name - create a file, given a name
  419. * @name: name of file
  420. * @mode: type of file
  421. * @parent: dentry of directory to create it in
  422. * @dentry: resulting dentry of file
  423. *
  424. * This function handles both regular files and directories.
  425. */
  426. static int fs_create_by_name (const char *name, mode_t mode,
  427. struct dentry *parent, struct dentry **dentry)
  428. {
  429. int error = 0;
  430. /* If the parent is not specified, we create it in the root.
  431. * We need the root dentry to do this, which is in the super
  432. * block. A pointer to that is in the struct vfsmount that we
  433. * have around.
  434. */
  435. if (!parent ) {
  436. if (usbfs_mount && usbfs_mount->mnt_sb) {
  437. parent = usbfs_mount->mnt_sb->s_root;
  438. }
  439. }
  440. if (!parent) {
  441. dbg("Ah! can not find a parent!");
  442. return -EFAULT;
  443. }
  444. *dentry = NULL;
  445. mutex_lock(&parent->d_inode->i_mutex);
  446. *dentry = lookup_one_len(name, parent, strlen(name));
  447. if (!IS_ERR(dentry)) {
  448. if ((mode & S_IFMT) == S_IFDIR)
  449. error = usbfs_mkdir (parent->d_inode, *dentry, mode);
  450. else
  451. error = usbfs_create (parent->d_inode, *dentry, mode);
  452. } else
  453. error = PTR_ERR(dentry);
  454. mutex_unlock(&parent->d_inode->i_mutex);
  455. return error;
  456. }
  457. static struct dentry *fs_create_file (const char *name, mode_t mode,
  458. struct dentry *parent, void *data,
  459. const struct file_operations *fops,
  460. uid_t uid, gid_t gid)
  461. {
  462. struct dentry *dentry;
  463. int error;
  464. dbg("creating file '%s'",name);
  465. error = fs_create_by_name (name, mode, parent, &dentry);
  466. if (error) {
  467. dentry = NULL;
  468. } else {
  469. if (dentry->d_inode) {
  470. if (data)
  471. dentry->d_inode->i_private = data;
  472. if (fops)
  473. dentry->d_inode->i_fop = fops;
  474. dentry->d_inode->i_uid = uid;
  475. dentry->d_inode->i_gid = gid;
  476. }
  477. }
  478. return dentry;
  479. }
  480. static void fs_remove_file (struct dentry *dentry)
  481. {
  482. struct dentry *parent = dentry->d_parent;
  483. if (!parent || !parent->d_inode)
  484. return;
  485. mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_PARENT);
  486. if (usbfs_positive(dentry)) {
  487. if (dentry->d_inode) {
  488. if (S_ISDIR(dentry->d_inode->i_mode))
  489. usbfs_rmdir(parent->d_inode, dentry);
  490. else
  491. usbfs_unlink(parent->d_inode, dentry);
  492. dput(dentry);
  493. }
  494. }
  495. mutex_unlock(&parent->d_inode->i_mutex);
  496. }
  497. /* --------------------------------------------------------------------- */
  498. static int usb_get_sb(struct file_system_type *fs_type,
  499. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  500. {
  501. return get_sb_single(fs_type, flags, data, usbfs_fill_super, mnt);
  502. }
  503. static struct file_system_type usb_fs_type = {
  504. .owner = THIS_MODULE,
  505. .name = "usbfs",
  506. .get_sb = usb_get_sb,
  507. .kill_sb = kill_litter_super,
  508. };
  509. /* --------------------------------------------------------------------- */
  510. static int create_special_files (void)
  511. {
  512. struct dentry *parent;
  513. int retval;
  514. /* the simple_pin_fs calls will call remount with no options
  515. * without this flag that would overwrite the real mount options (if any)
  516. */
  517. ignore_mount = 1;
  518. /* create the devices special file */
  519. retval = simple_pin_fs(&usb_fs_type, &usbfs_mount, &usbfs_mount_count);
  520. if (retval) {
  521. err ("Unable to get usbfs mount");
  522. goto exit;
  523. }
  524. ignore_mount = 0;
  525. parent = usbfs_mount->mnt_sb->s_root;
  526. devices_usbfs_dentry = fs_create_file ("devices",
  527. listmode | S_IFREG, parent,
  528. NULL, &usbfs_devices_fops,
  529. listuid, listgid);
  530. if (devices_usbfs_dentry == NULL) {
  531. err ("Unable to create devices usbfs file");
  532. retval = -ENODEV;
  533. goto error_clean_mounts;
  534. }
  535. goto exit;
  536. error_clean_mounts:
  537. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  538. exit:
  539. return retval;
  540. }
  541. static void remove_special_files (void)
  542. {
  543. if (devices_usbfs_dentry)
  544. fs_remove_file (devices_usbfs_dentry);
  545. devices_usbfs_dentry = NULL;
  546. simple_release_fs(&usbfs_mount, &usbfs_mount_count);
  547. }
  548. void usbfs_update_special (void)
  549. {
  550. struct inode *inode;
  551. if (devices_usbfs_dentry) {
  552. inode = devices_usbfs_dentry->d_inode;
  553. if (inode)
  554. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  555. }
  556. }
  557. static void usbfs_add_bus(struct usb_bus *bus)
  558. {
  559. struct dentry *parent;
  560. char name[8];
  561. int retval;
  562. /* create the special files if this is the first bus added */
  563. if (num_buses == 0) {
  564. retval = create_special_files();
  565. if (retval)
  566. return;
  567. }
  568. ++num_buses;
  569. sprintf (name, "%03d", bus->busnum);
  570. parent = usbfs_mount->mnt_sb->s_root;
  571. bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
  572. bus, NULL, busuid, busgid);
  573. if (bus->usbfs_dentry == NULL) {
  574. err ("error creating usbfs bus entry");
  575. return;
  576. }
  577. }
  578. static void usbfs_remove_bus(struct usb_bus *bus)
  579. {
  580. if (bus->usbfs_dentry) {
  581. fs_remove_file (bus->usbfs_dentry);
  582. bus->usbfs_dentry = NULL;
  583. }
  584. --num_buses;
  585. if (num_buses <= 0) {
  586. remove_special_files();
  587. num_buses = 0;
  588. }
  589. }
  590. static void usbfs_add_device(struct usb_device *dev)
  591. {
  592. char name[8];
  593. int i;
  594. int i_size;
  595. sprintf (name, "%03d", dev->devnum);
  596. dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
  597. dev->bus->usbfs_dentry, dev,
  598. &usbdev_file_operations,
  599. devuid, devgid);
  600. if (dev->usbfs_dentry == NULL) {
  601. err ("error creating usbfs device entry");
  602. return;
  603. }
  604. /* Set the size of the device's file to be
  605. * equal to the size of the device descriptors. */
  606. i_size = sizeof (struct usb_device_descriptor);
  607. for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
  608. struct usb_config_descriptor *config =
  609. (struct usb_config_descriptor *)dev->rawdescriptors[i];
  610. i_size += le16_to_cpu(config->wTotalLength);
  611. }
  612. if (dev->usbfs_dentry->d_inode)
  613. dev->usbfs_dentry->d_inode->i_size = i_size;
  614. }
  615. static void usbfs_remove_device(struct usb_device *dev)
  616. {
  617. if (dev->usbfs_dentry) {
  618. fs_remove_file (dev->usbfs_dentry);
  619. dev->usbfs_dentry = NULL;
  620. }
  621. usb_fs_classdev_common_remove(dev);
  622. }
  623. static int usbfs_notify(struct notifier_block *self, unsigned long action, void *dev)
  624. {
  625. switch (action) {
  626. case USB_DEVICE_ADD:
  627. usbfs_add_device(dev);
  628. break;
  629. case USB_DEVICE_REMOVE:
  630. usbfs_remove_device(dev);
  631. break;
  632. case USB_BUS_ADD:
  633. usbfs_add_bus(dev);
  634. break;
  635. case USB_BUS_REMOVE:
  636. usbfs_remove_bus(dev);
  637. }
  638. usbfs_update_special();
  639. usbfs_conn_disc_event();
  640. return NOTIFY_OK;
  641. }
  642. static struct notifier_block usbfs_nb = {
  643. .notifier_call = usbfs_notify,
  644. };
  645. /* --------------------------------------------------------------------- */
  646. static struct proc_dir_entry *usbdir = NULL;
  647. int __init usbfs_init(void)
  648. {
  649. int retval;
  650. retval = register_filesystem(&usb_fs_type);
  651. if (retval)
  652. return retval;
  653. usb_register_notify(&usbfs_nb);
  654. /* create mount point for usbfs */
  655. usbdir = proc_mkdir("bus/usb", NULL);
  656. return 0;
  657. }
  658. void usbfs_cleanup(void)
  659. {
  660. usb_unregister_notify(&usbfs_nb);
  661. unregister_filesystem(&usb_fs_type);
  662. if (usbdir)
  663. remove_proc_entry("bus/usb", NULL);
  664. }