inode.c 18 KB

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