generic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * proc/fs/generic.c --- generic routines for the proc-fs
  3. *
  4. * This file contains generic proc-fs routines for handling
  5. * directories and files.
  6. *
  7. * Copyright (C) 1991, 1992 Linus Torvalds.
  8. * Copyright (C) 1997 Theodore Ts'o
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/time.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/stat.h>
  14. #include <linux/module.h>
  15. #include <linux/mount.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/init.h>
  18. #include <linux/idr.h>
  19. #include <linux/namei.h>
  20. #include <linux/bitops.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/completion.h>
  23. #include <asm/uaccess.h>
  24. #include "internal.h"
  25. DEFINE_SPINLOCK(proc_subdir_lock);
  26. static int proc_match(int len, const char *name, struct proc_dir_entry *de)
  27. {
  28. if (de->namelen != len)
  29. return 0;
  30. return !memcmp(name, de->name, len);
  31. }
  32. /* buffer size is one page but our output routines use some slack for overruns */
  33. #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
  34. static ssize_t
  35. proc_file_read(struct file *file, char __user *buf, size_t nbytes,
  36. loff_t *ppos)
  37. {
  38. struct inode * inode = file->f_path.dentry->d_inode;
  39. char *page;
  40. ssize_t retval=0;
  41. int eof=0;
  42. ssize_t n, count;
  43. char *start;
  44. struct proc_dir_entry * dp;
  45. unsigned long long pos;
  46. /*
  47. * Gaah, please just use "seq_file" instead. The legacy /proc
  48. * interfaces cut loff_t down to off_t for reads, and ignore
  49. * the offset entirely for writes..
  50. */
  51. pos = *ppos;
  52. if (pos > MAX_NON_LFS)
  53. return 0;
  54. if (nbytes > MAX_NON_LFS - pos)
  55. nbytes = MAX_NON_LFS - pos;
  56. dp = PDE(inode);
  57. if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
  58. return -ENOMEM;
  59. while ((nbytes > 0) && !eof) {
  60. count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
  61. start = NULL;
  62. if (dp->get_info) {
  63. /* Handle old net routines */
  64. n = dp->get_info(page, &start, *ppos, count);
  65. if (n < count)
  66. eof = 1;
  67. } else if (dp->read_proc) {
  68. /*
  69. * How to be a proc read function
  70. * ------------------------------
  71. * Prototype:
  72. * int f(char *buffer, char **start, off_t offset,
  73. * int count, int *peof, void *dat)
  74. *
  75. * Assume that the buffer is "count" bytes in size.
  76. *
  77. * If you know you have supplied all the data you
  78. * have, set *peof.
  79. *
  80. * You have three ways to return data:
  81. * 0) Leave *start = NULL. (This is the default.)
  82. * Put the data of the requested offset at that
  83. * offset within the buffer. Return the number (n)
  84. * of bytes there are from the beginning of the
  85. * buffer up to the last byte of data. If the
  86. * number of supplied bytes (= n - offset) is
  87. * greater than zero and you didn't signal eof
  88. * and the reader is prepared to take more data
  89. * you will be called again with the requested
  90. * offset advanced by the number of bytes
  91. * absorbed. This interface is useful for files
  92. * no larger than the buffer.
  93. * 1) Set *start = an unsigned long value less than
  94. * the buffer address but greater than zero.
  95. * Put the data of the requested offset at the
  96. * beginning of the buffer. Return the number of
  97. * bytes of data placed there. If this number is
  98. * greater than zero and you didn't signal eof
  99. * and the reader is prepared to take more data
  100. * you will be called again with the requested
  101. * offset advanced by *start. This interface is
  102. * useful when you have a large file consisting
  103. * of a series of blocks which you want to count
  104. * and return as wholes.
  105. * (Hack by Paul.Russell@rustcorp.com.au)
  106. * 2) Set *start = an address within the buffer.
  107. * Put the data of the requested offset at *start.
  108. * Return the number of bytes of data placed there.
  109. * If this number is greater than zero and you
  110. * didn't signal eof and the reader is prepared to
  111. * take more data you will be called again with the
  112. * requested offset advanced by the number of bytes
  113. * absorbed.
  114. */
  115. n = dp->read_proc(page, &start, *ppos,
  116. count, &eof, dp->data);
  117. } else
  118. break;
  119. if (n == 0) /* end of file */
  120. break;
  121. if (n < 0) { /* error */
  122. if (retval == 0)
  123. retval = n;
  124. break;
  125. }
  126. if (start == NULL) {
  127. if (n > PAGE_SIZE) {
  128. printk(KERN_ERR
  129. "proc_file_read: Apparent buffer overflow!\n");
  130. n = PAGE_SIZE;
  131. }
  132. n -= *ppos;
  133. if (n <= 0)
  134. break;
  135. if (n > count)
  136. n = count;
  137. start = page + *ppos;
  138. } else if (start < page) {
  139. if (n > PAGE_SIZE) {
  140. printk(KERN_ERR
  141. "proc_file_read: Apparent buffer overflow!\n");
  142. n = PAGE_SIZE;
  143. }
  144. if (n > count) {
  145. /*
  146. * Don't reduce n because doing so might
  147. * cut off part of a data block.
  148. */
  149. printk(KERN_WARNING
  150. "proc_file_read: Read count exceeded\n");
  151. }
  152. } else /* start >= page */ {
  153. unsigned long startoff = (unsigned long)(start - page);
  154. if (n > (PAGE_SIZE - startoff)) {
  155. printk(KERN_ERR
  156. "proc_file_read: Apparent buffer overflow!\n");
  157. n = PAGE_SIZE - startoff;
  158. }
  159. if (n > count)
  160. n = count;
  161. }
  162. n -= copy_to_user(buf, start < page ? page : start, n);
  163. if (n == 0) {
  164. if (retval == 0)
  165. retval = -EFAULT;
  166. break;
  167. }
  168. *ppos += start < page ? (unsigned long)start : n;
  169. nbytes -= n;
  170. buf += n;
  171. retval += n;
  172. }
  173. free_page((unsigned long) page);
  174. return retval;
  175. }
  176. static ssize_t
  177. proc_file_write(struct file *file, const char __user *buffer,
  178. size_t count, loff_t *ppos)
  179. {
  180. struct inode *inode = file->f_path.dentry->d_inode;
  181. struct proc_dir_entry * dp;
  182. dp = PDE(inode);
  183. if (!dp->write_proc)
  184. return -EIO;
  185. /* FIXME: does this routine need ppos? probably... */
  186. return dp->write_proc(file, buffer, count, dp->data);
  187. }
  188. static loff_t
  189. proc_file_lseek(struct file *file, loff_t offset, int orig)
  190. {
  191. loff_t retval = -EINVAL;
  192. switch (orig) {
  193. case 1:
  194. offset += file->f_pos;
  195. /* fallthrough */
  196. case 0:
  197. if (offset < 0 || offset > MAX_NON_LFS)
  198. break;
  199. file->f_pos = retval = offset;
  200. }
  201. return retval;
  202. }
  203. static const struct file_operations proc_file_operations = {
  204. .llseek = proc_file_lseek,
  205. .read = proc_file_read,
  206. .write = proc_file_write,
  207. };
  208. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  209. {
  210. struct inode *inode = dentry->d_inode;
  211. struct proc_dir_entry *de = PDE(inode);
  212. int error;
  213. error = inode_change_ok(inode, iattr);
  214. if (error)
  215. goto out;
  216. error = inode_setattr(inode, iattr);
  217. if (error)
  218. goto out;
  219. de->uid = inode->i_uid;
  220. de->gid = inode->i_gid;
  221. de->mode = inode->i_mode;
  222. out:
  223. return error;
  224. }
  225. static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
  226. struct kstat *stat)
  227. {
  228. struct inode *inode = dentry->d_inode;
  229. struct proc_dir_entry *de = PROC_I(inode)->pde;
  230. if (de && de->nlink)
  231. inode->i_nlink = de->nlink;
  232. generic_fillattr(inode, stat);
  233. return 0;
  234. }
  235. static const struct inode_operations proc_file_inode_operations = {
  236. .setattr = proc_notify_change,
  237. };
  238. /*
  239. * This function parses a name such as "tty/driver/serial", and
  240. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  241. * returns "serial" in residual.
  242. */
  243. static int xlate_proc_name(const char *name,
  244. struct proc_dir_entry **ret, const char **residual)
  245. {
  246. const char *cp = name, *next;
  247. struct proc_dir_entry *de;
  248. int len;
  249. int rtn = 0;
  250. spin_lock(&proc_subdir_lock);
  251. de = &proc_root;
  252. while (1) {
  253. next = strchr(cp, '/');
  254. if (!next)
  255. break;
  256. len = next - cp;
  257. for (de = de->subdir; de ; de = de->next) {
  258. if (proc_match(len, cp, de))
  259. break;
  260. }
  261. if (!de) {
  262. rtn = -ENOENT;
  263. goto out;
  264. }
  265. cp += len + 1;
  266. }
  267. *residual = cp;
  268. *ret = de;
  269. out:
  270. spin_unlock(&proc_subdir_lock);
  271. return rtn;
  272. }
  273. static DEFINE_IDR(proc_inum_idr);
  274. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  275. #define PROC_DYNAMIC_FIRST 0xF0000000UL
  276. /*
  277. * Return an inode number between PROC_DYNAMIC_FIRST and
  278. * 0xffffffff, or zero on failure.
  279. */
  280. static unsigned int get_inode_number(void)
  281. {
  282. int i, inum = 0;
  283. int error;
  284. retry:
  285. if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
  286. return 0;
  287. spin_lock(&proc_inum_lock);
  288. error = idr_get_new(&proc_inum_idr, NULL, &i);
  289. spin_unlock(&proc_inum_lock);
  290. if (error == -EAGAIN)
  291. goto retry;
  292. else if (error)
  293. return 0;
  294. inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
  295. /* inum will never be more than 0xf0ffffff, so no check
  296. * for overflow.
  297. */
  298. return inum;
  299. }
  300. static void release_inode_number(unsigned int inum)
  301. {
  302. int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
  303. spin_lock(&proc_inum_lock);
  304. idr_remove(&proc_inum_idr, id);
  305. spin_unlock(&proc_inum_lock);
  306. }
  307. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  308. {
  309. nd_set_link(nd, PDE(dentry->d_inode)->data);
  310. return NULL;
  311. }
  312. static const struct inode_operations proc_link_inode_operations = {
  313. .readlink = generic_readlink,
  314. .follow_link = proc_follow_link,
  315. };
  316. /*
  317. * As some entries in /proc are volatile, we want to
  318. * get rid of unused dentries. This could be made
  319. * smarter: we could keep a "volatile" flag in the
  320. * inode to indicate which ones to keep.
  321. */
  322. static int proc_delete_dentry(struct dentry * dentry)
  323. {
  324. return 1;
  325. }
  326. static struct dentry_operations proc_dentry_operations =
  327. {
  328. .d_delete = proc_delete_dentry,
  329. };
  330. /*
  331. * Don't create negative dentries here, return -ENOENT by hand
  332. * instead.
  333. */
  334. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  335. struct dentry *dentry)
  336. {
  337. struct inode *inode = NULL;
  338. int error = -ENOENT;
  339. lock_kernel();
  340. spin_lock(&proc_subdir_lock);
  341. if (de) {
  342. for (de = de->subdir; de ; de = de->next) {
  343. if (de->namelen != dentry->d_name.len)
  344. continue;
  345. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  346. unsigned int ino;
  347. ino = de->low_ino;
  348. de_get(de);
  349. spin_unlock(&proc_subdir_lock);
  350. error = -EINVAL;
  351. inode = proc_get_inode(dir->i_sb, ino, de);
  352. goto out_unlock;
  353. }
  354. }
  355. }
  356. spin_unlock(&proc_subdir_lock);
  357. out_unlock:
  358. unlock_kernel();
  359. if (inode) {
  360. dentry->d_op = &proc_dentry_operations;
  361. d_add(dentry, inode);
  362. return NULL;
  363. }
  364. de_put(de);
  365. return ERR_PTR(error);
  366. }
  367. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  368. struct nameidata *nd)
  369. {
  370. return proc_lookup_de(PDE(dir), dir, dentry);
  371. }
  372. /*
  373. * This returns non-zero if at EOF, so that the /proc
  374. * root directory can use this and check if it should
  375. * continue with the <pid> entries..
  376. *
  377. * Note that the VFS-layer doesn't care about the return
  378. * value of the readdir() call, as long as it's non-negative
  379. * for success..
  380. */
  381. int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
  382. filldir_t filldir)
  383. {
  384. unsigned int ino;
  385. int i;
  386. struct inode *inode = filp->f_path.dentry->d_inode;
  387. int ret = 0;
  388. lock_kernel();
  389. ino = inode->i_ino;
  390. if (!de) {
  391. ret = -EINVAL;
  392. goto out;
  393. }
  394. i = filp->f_pos;
  395. switch (i) {
  396. case 0:
  397. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  398. goto out;
  399. i++;
  400. filp->f_pos++;
  401. /* fall through */
  402. case 1:
  403. if (filldir(dirent, "..", 2, i,
  404. parent_ino(filp->f_path.dentry),
  405. DT_DIR) < 0)
  406. goto out;
  407. i++;
  408. filp->f_pos++;
  409. /* fall through */
  410. default:
  411. spin_lock(&proc_subdir_lock);
  412. de = de->subdir;
  413. i -= 2;
  414. for (;;) {
  415. if (!de) {
  416. ret = 1;
  417. spin_unlock(&proc_subdir_lock);
  418. goto out;
  419. }
  420. if (!i)
  421. break;
  422. de = de->next;
  423. i--;
  424. }
  425. do {
  426. struct proc_dir_entry *next;
  427. /* filldir passes info to user space */
  428. de_get(de);
  429. spin_unlock(&proc_subdir_lock);
  430. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  431. de->low_ino, de->mode >> 12) < 0) {
  432. de_put(de);
  433. goto out;
  434. }
  435. spin_lock(&proc_subdir_lock);
  436. filp->f_pos++;
  437. next = de->next;
  438. de_put(de);
  439. de = next;
  440. } while (de);
  441. spin_unlock(&proc_subdir_lock);
  442. }
  443. ret = 1;
  444. out: unlock_kernel();
  445. return ret;
  446. }
  447. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  448. {
  449. struct inode *inode = filp->f_path.dentry->d_inode;
  450. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  451. }
  452. /*
  453. * These are the generic /proc directory operations. They
  454. * use the in-memory "struct proc_dir_entry" tree to parse
  455. * the /proc directory.
  456. */
  457. static const struct file_operations proc_dir_operations = {
  458. .read = generic_read_dir,
  459. .readdir = proc_readdir,
  460. };
  461. /*
  462. * proc directories can do almost nothing..
  463. */
  464. static const struct inode_operations proc_dir_inode_operations = {
  465. .lookup = proc_lookup,
  466. .getattr = proc_getattr,
  467. .setattr = proc_notify_change,
  468. };
  469. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  470. {
  471. unsigned int i;
  472. struct proc_dir_entry *tmp;
  473. i = get_inode_number();
  474. if (i == 0)
  475. return -EAGAIN;
  476. dp->low_ino = i;
  477. if (S_ISDIR(dp->mode)) {
  478. if (dp->proc_iops == NULL) {
  479. dp->proc_fops = &proc_dir_operations;
  480. dp->proc_iops = &proc_dir_inode_operations;
  481. }
  482. dir->nlink++;
  483. } else if (S_ISLNK(dp->mode)) {
  484. if (dp->proc_iops == NULL)
  485. dp->proc_iops = &proc_link_inode_operations;
  486. } else if (S_ISREG(dp->mode)) {
  487. if (dp->proc_fops == NULL)
  488. dp->proc_fops = &proc_file_operations;
  489. if (dp->proc_iops == NULL)
  490. dp->proc_iops = &proc_file_inode_operations;
  491. }
  492. spin_lock(&proc_subdir_lock);
  493. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  494. if (strcmp(tmp->name, dp->name) == 0) {
  495. printk(KERN_WARNING "proc_dir_entry '%s' already "
  496. "registered\n", dp->name);
  497. dump_stack();
  498. break;
  499. }
  500. dp->next = dir->subdir;
  501. dp->parent = dir;
  502. dir->subdir = dp;
  503. spin_unlock(&proc_subdir_lock);
  504. return 0;
  505. }
  506. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  507. const char *name,
  508. mode_t mode,
  509. nlink_t nlink)
  510. {
  511. struct proc_dir_entry *ent = NULL;
  512. const char *fn = name;
  513. int len;
  514. /* make sure name is valid */
  515. if (!name || !strlen(name)) goto out;
  516. if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
  517. goto out;
  518. /* At this point there must not be any '/' characters beyond *fn */
  519. if (strchr(fn, '/'))
  520. goto out;
  521. len = strlen(fn);
  522. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  523. if (!ent) goto out;
  524. memset(ent, 0, sizeof(struct proc_dir_entry));
  525. memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
  526. ent->name = ((char *) ent) + sizeof(*ent);
  527. ent->namelen = len;
  528. ent->mode = mode;
  529. ent->nlink = nlink;
  530. atomic_set(&ent->count, 1);
  531. ent->pde_users = 0;
  532. spin_lock_init(&ent->pde_unload_lock);
  533. ent->pde_unload_completion = NULL;
  534. out:
  535. return ent;
  536. }
  537. struct proc_dir_entry *proc_symlink(const char *name,
  538. struct proc_dir_entry *parent, const char *dest)
  539. {
  540. struct proc_dir_entry *ent;
  541. ent = __proc_create(&parent, name,
  542. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  543. if (ent) {
  544. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  545. if (ent->data) {
  546. strcpy((char*)ent->data,dest);
  547. if (proc_register(parent, ent) < 0) {
  548. kfree(ent->data);
  549. kfree(ent);
  550. ent = NULL;
  551. }
  552. } else {
  553. kfree(ent);
  554. ent = NULL;
  555. }
  556. }
  557. return ent;
  558. }
  559. struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
  560. struct proc_dir_entry *parent)
  561. {
  562. struct proc_dir_entry *ent;
  563. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  564. if (ent) {
  565. if (proc_register(parent, ent) < 0) {
  566. kfree(ent);
  567. ent = NULL;
  568. }
  569. }
  570. return ent;
  571. }
  572. struct proc_dir_entry *proc_mkdir(const char *name,
  573. struct proc_dir_entry *parent)
  574. {
  575. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  576. }
  577. struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
  578. struct proc_dir_entry *parent)
  579. {
  580. struct proc_dir_entry *ent;
  581. nlink_t nlink;
  582. if (S_ISDIR(mode)) {
  583. if ((mode & S_IALLUGO) == 0)
  584. mode |= S_IRUGO | S_IXUGO;
  585. nlink = 2;
  586. } else {
  587. if ((mode & S_IFMT) == 0)
  588. mode |= S_IFREG;
  589. if ((mode & S_IALLUGO) == 0)
  590. mode |= S_IRUGO;
  591. nlink = 1;
  592. }
  593. ent = __proc_create(&parent, name, mode, nlink);
  594. if (ent) {
  595. if (proc_register(parent, ent) < 0) {
  596. kfree(ent);
  597. ent = NULL;
  598. }
  599. }
  600. return ent;
  601. }
  602. struct proc_dir_entry *proc_create(const char *name, mode_t mode,
  603. struct proc_dir_entry *parent,
  604. const struct file_operations *proc_fops)
  605. {
  606. struct proc_dir_entry *pde;
  607. nlink_t nlink;
  608. if (S_ISDIR(mode)) {
  609. if ((mode & S_IALLUGO) == 0)
  610. mode |= S_IRUGO | S_IXUGO;
  611. nlink = 2;
  612. } else {
  613. if ((mode & S_IFMT) == 0)
  614. mode |= S_IFREG;
  615. if ((mode & S_IALLUGO) == 0)
  616. mode |= S_IRUGO;
  617. nlink = 1;
  618. }
  619. pde = __proc_create(&parent, name, mode, nlink);
  620. if (!pde)
  621. goto out;
  622. pde->proc_fops = proc_fops;
  623. if (proc_register(parent, pde) < 0)
  624. goto out_free;
  625. return pde;
  626. out_free:
  627. kfree(pde);
  628. out:
  629. return NULL;
  630. }
  631. void free_proc_entry(struct proc_dir_entry *de)
  632. {
  633. unsigned int ino = de->low_ino;
  634. if (ino < PROC_DYNAMIC_FIRST)
  635. return;
  636. release_inode_number(ino);
  637. if (S_ISLNK(de->mode))
  638. kfree(de->data);
  639. kfree(de);
  640. }
  641. /*
  642. * Remove a /proc entry and free it if it's not currently in use.
  643. */
  644. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  645. {
  646. struct proc_dir_entry **p;
  647. struct proc_dir_entry *de;
  648. const char *fn = name;
  649. int len;
  650. if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
  651. goto out;
  652. len = strlen(fn);
  653. spin_lock(&proc_subdir_lock);
  654. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  655. if (!proc_match(len, fn, *p))
  656. continue;
  657. de = *p;
  658. *p = de->next;
  659. de->next = NULL;
  660. spin_lock(&de->pde_unload_lock);
  661. /*
  662. * Stop accepting new callers into module. If you're
  663. * dynamically allocating ->proc_fops, save a pointer somewhere.
  664. */
  665. de->proc_fops = NULL;
  666. /* Wait until all existing callers into module are done. */
  667. if (de->pde_users > 0) {
  668. DECLARE_COMPLETION_ONSTACK(c);
  669. if (!de->pde_unload_completion)
  670. de->pde_unload_completion = &c;
  671. spin_unlock(&de->pde_unload_lock);
  672. spin_unlock(&proc_subdir_lock);
  673. wait_for_completion(de->pde_unload_completion);
  674. spin_lock(&proc_subdir_lock);
  675. goto continue_removing;
  676. }
  677. spin_unlock(&de->pde_unload_lock);
  678. continue_removing:
  679. if (S_ISDIR(de->mode))
  680. parent->nlink--;
  681. de->nlink = 0;
  682. WARN_ON(de->subdir);
  683. if (atomic_dec_and_test(&de->count))
  684. free_proc_entry(de);
  685. break;
  686. }
  687. spin_unlock(&proc_subdir_lock);
  688. out:
  689. return;
  690. }