inode.c 17 KB

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