generic.c 18 KB

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