inode.c 18 KB

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