inode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*****************************************************************************/
  2. /*
  3. * inode.c -- Inode/Dentry functions for the USB device file system.
  4. *
  5. * Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. * Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * History:
  23. * 0.1 04.01.2000 Created
  24. * 0.2 10.12.2001 converted to use the vfs layer better
  25. */
  26. /*****************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/fs.h>
  29. #include <linux/mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/init.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/usb.h>
  34. #include <linux/namei.h>
  35. #include <linux/usbdevice_fs.h>
  36. #include <linux/parser.h>
  37. #include <linux/notifier.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/usb/hcd.h>
  40. #include <asm/byteorder.h>
  41. #include "usb.h"
  42. #define USBFS_DEFAULT_DEVMODE (S_IWUSR | S_IRUGO)
  43. #define USBFS_DEFAULT_BUSMODE (S_IXUGO | S_IRUGO)
  44. #define USBFS_DEFAULT_LISTMODE S_IRUGO
  45. static const struct file_operations default_file_operations;
  46. static struct vfsmount *usbfs_mount;
  47. static int usbfs_mount_count; /* = 0 */
  48. static int ignore_mount = 0;
  49. static struct dentry *devices_usbfs_dentry;
  50. static int num_buses; /* = 0 */
  51. static uid_t devuid; /* = 0 */
  52. static uid_t busuid; /* = 0 */
  53. static uid_t listuid; /* = 0 */
  54. static gid_t devgid; /* = 0 */
  55. static gid_t busgid; /* = 0 */
  56. static gid_t listgid; /* = 0 */
  57. static umode_t devmode = USBFS_DEFAULT_DEVMODE;
  58. static umode_t busmode = USBFS_DEFAULT_BUSMODE;
  59. static umode_t listmode = USBFS_DEFAULT_LISTMODE;
  60. static int usbfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
  61. {
  62. if (devuid != 0)
  63. seq_printf(seq, ",devuid=%u", devuid);
  64. if (devgid != 0)
  65. seq_printf(seq, ",devgid=%u", devgid);
  66. if (devmode != USBFS_DEFAULT_DEVMODE)
  67. seq_printf(seq, ",devmode=%o", devmode);
  68. if (busuid != 0)
  69. seq_printf(seq, ",busuid=%u", busuid);
  70. if (busgid != 0)
  71. seq_printf(seq, ",busgid=%u", busgid);
  72. if (busmode != USBFS_DEFAULT_BUSMODE)
  73. seq_printf(seq, ",busmode=%o", busmode);
  74. if (listuid != 0)
  75. seq_printf(seq, ",listuid=%u", listuid);
  76. if (listgid != 0)
  77. seq_printf(seq, ",listgid=%u", listgid);
  78. if (listmode != USBFS_DEFAULT_LISTMODE)
  79. seq_printf(seq, ",listmode=%o", listmode);
  80. return 0;
  81. }
  82. enum {
  83. Opt_devuid, Opt_devgid, Opt_devmode,
  84. Opt_busuid, Opt_busgid, Opt_busmode,
  85. Opt_listuid, Opt_listgid, Opt_listmode,
  86. Opt_err,
  87. };
  88. static const match_table_t tokens = {
  89. {Opt_devuid, "devuid=%u"},
  90. {Opt_devgid, "devgid=%u"},
  91. {Opt_devmode, "devmode=%o"},
  92. {Opt_busuid, "busuid=%u"},
  93. {Opt_busgid, "busgid=%u"},
  94. {Opt_busmode, "busmode=%o"},
  95. {Opt_listuid, "listuid=%u"},
  96. {Opt_listgid, "listgid=%u"},
  97. {Opt_listmode, "listmode=%o"},
  98. {Opt_err, NULL}
  99. };
  100. static int parse_options(struct super_block *s, char *data)
  101. {
  102. char *p;
  103. int option;
  104. /* (re)set to defaults. */
  105. devuid = 0;
  106. busuid = 0;
  107. listuid = 0;
  108. devgid = 0;
  109. busgid = 0;
  110. listgid = 0;
  111. devmode = USBFS_DEFAULT_DEVMODE;
  112. busmode = USBFS_DEFAULT_BUSMODE;
  113. listmode = USBFS_DEFAULT_LISTMODE;
  114. while ((p = strsep(&data, ",")) != NULL) {
  115. substring_t args[MAX_OPT_ARGS];
  116. int token;
  117. if (!*p)
  118. continue;
  119. token = match_token(p, tokens, args);
  120. switch (token) {
  121. case Opt_devuid:
  122. if (match_int(&args[0], &option))
  123. return -EINVAL;
  124. devuid = option;
  125. break;
  126. case Opt_devgid:
  127. if (match_int(&args[0], &option))
  128. return -EINVAL;
  129. devgid = option;
  130. break;
  131. case Opt_devmode:
  132. if (match_octal(&args[0], &option))
  133. return -EINVAL;
  134. devmode = option & S_IRWXUGO;
  135. break;
  136. case Opt_busuid:
  137. if (match_int(&args[0], &option))
  138. return -EINVAL;
  139. busuid = option;
  140. break;
  141. case Opt_busgid:
  142. if (match_int(&args[0], &option))
  143. return -EINVAL;
  144. busgid = option;
  145. break;
  146. case Opt_busmode:
  147. if (match_octal(&args[0], &option))
  148. return -EINVAL;
  149. busmode = option & S_IRWXUGO;
  150. break;
  151. case Opt_listuid:
  152. if (match_int(&args[0], &option))
  153. return -EINVAL;
  154. listuid = option;
  155. break;
  156. case Opt_listgid:
  157. if (match_int(&args[0], &option))
  158. return -EINVAL;
  159. listgid = option;
  160. break;
  161. case Opt_listmode:
  162. if (match_octal(&args[0], &option))
  163. return -EINVAL;
  164. listmode = option & S_IRWXUGO;
  165. break;
  166. default:
  167. printk(KERN_ERR "usbfs: unrecognised mount option "
  168. "\"%s\" or missing value\n", p);
  169. return -EINVAL;
  170. }
  171. }
  172. return 0;
  173. }
  174. static void update_special(struct dentry *special)
  175. {
  176. special->d_inode->i_uid = listuid;
  177. special->d_inode->i_gid = listgid;
  178. special->d_inode->i_mode = S_IFREG | listmode;
  179. }
  180. static void update_dev(struct dentry *dev)
  181. {
  182. dev->d_inode->i_uid = devuid;
  183. dev->d_inode->i_gid = devgid;
  184. dev->d_inode->i_mode = S_IFREG | devmode;
  185. }
  186. static void update_bus(struct dentry *bus)
  187. {
  188. struct dentry *dev = NULL;
  189. bus->d_inode->i_uid = busuid;
  190. bus->d_inode->i_gid = busgid;
  191. bus->d_inode->i_mode = S_IFDIR | busmode;
  192. mutex_lock(&bus->d_inode->i_mutex);
  193. list_for_each_entry(dev, &bus->d_subdirs, d_u.d_child)
  194. if (dev->d_inode)
  195. update_dev(dev);
  196. mutex_unlock(&bus->d_inode->i_mutex);
  197. }
  198. static void update_sb(struct super_block *sb)
  199. {
  200. struct dentry *root = sb->s_root;
  201. struct dentry *bus = NULL;
  202. if (!root)
  203. return;
  204. mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
  205. list_for_each_entry(bus, &root->d_subdirs, d_u.d_child) {
  206. if (bus->d_inode) {
  207. switch (S_IFMT & bus->d_inode->i_mode) {
  208. case S_IFDIR:
  209. update_bus(bus);
  210. break;
  211. case S_IFREG:
  212. update_special(bus);
  213. break;
  214. default:
  215. printk(KERN_WARNING "usbfs: Unknown node %s "
  216. "mode %x found on remount!\n",
  217. bus->d_name.name, bus->d_inode->i_mode);
  218. break;
  219. }
  220. }
  221. }
  222. mutex_unlock(&root->d_inode->i_mutex);
  223. }
  224. static int remount(struct super_block *sb, int *flags, char *data)
  225. {
  226. /* If this is not a real mount,
  227. * i.e. it's a simple_pin_fs from create_special_files,
  228. * then ignore it.
  229. */
  230. if (ignore_mount)
  231. return 0;
  232. if (parse_options(sb, data)) {
  233. printk(KERN_WARNING "usbfs: mount parameter error.\n");
  234. return -EINVAL;
  235. }
  236. if (usbfs_mount && usbfs_mount->mnt_sb)
  237. update_sb(usbfs_mount->mnt_sb);
  238. return 0;
  239. }
  240. static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
  241. {
  242. struct inode *inode = new_inode(sb);
  243. if (inode) {
  244. inode->i_ino = get_next_ino();
  245. inode->i_mode = mode;
  246. inode->i_uid = current_fsuid();
  247. inode->i_gid = current_fsgid();
  248. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  249. switch (mode & S_IFMT) {
  250. default:
  251. init_special_inode(inode, mode, dev);
  252. break;
  253. case S_IFREG:
  254. inode->i_fop = &default_file_operations;
  255. break;
  256. case S_IFDIR:
  257. inode->i_op = &simple_dir_inode_operations;
  258. inode->i_fop = &simple_dir_operations;
  259. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  260. inc_nlink(inode);
  261. break;
  262. }
  263. }
  264. return inode;
  265. }
  266. /* SMP-safe */
  267. static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
  268. dev_t dev)
  269. {
  270. struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
  271. int error = -EPERM;
  272. if (dentry->d_inode)
  273. return -EEXIST;
  274. if (inode) {
  275. d_instantiate(dentry, inode);
  276. dget(dentry);
  277. error = 0;
  278. }
  279. return error;
  280. }
  281. static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
  282. {
  283. int res;
  284. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  285. res = usbfs_mknod (dir, dentry, mode, 0);
  286. if (!res)
  287. inc_nlink(dir);
  288. return res;
  289. }
  290. static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
  291. {
  292. mode = (mode & S_IALLUGO) | S_IFREG;
  293. return usbfs_mknod (dir, dentry, mode, 0);
  294. }
  295. static inline int usbfs_positive (struct dentry *dentry)
  296. {
  297. return dentry->d_inode && !d_unhashed(dentry);
  298. }
  299. static int usbfs_empty (struct dentry *dentry)
  300. {
  301. struct list_head *list;
  302. spin_lock(&dentry->d_lock);
  303. list_for_each(list, &dentry->d_subdirs) {
  304. struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
  305. spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
  306. if (usbfs_positive(de)) {
  307. spin_unlock(&de->d_lock);
  308. spin_unlock(&dentry->d_lock);
  309. return 0;
  310. }
  311. spin_unlock(&de->d_lock);
  312. }
  313. spin_unlock(&dentry->d_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. dont_mount(dentry);
  334. drop_nlink(dentry->d_inode);
  335. drop_nlink(dentry->d_inode);
  336. dput(dentry);
  337. inode->i_flags |= S_DEAD;
  338. drop_nlink(dir);
  339. error = 0;
  340. }
  341. mutex_unlock(&inode->i_mutex);
  342. if (!error)
  343. d_delete(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 const 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 struct dentry *usb_mount(struct file_system_type *fs_type,
  503. int flags, const char *dev_name, void *data)
  504. {
  505. return mount_single(fs_type, flags, data, usbfs_fill_super);
  506. }
  507. static struct file_system_type usb_fs_type = {
  508. .owner = THIS_MODULE,
  509. .name = "usbfs",
  510. .mount = usb_mount,
  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. }