generic.c 18 KB

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