generic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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_IDA(proc_inum_ida);
  271. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  272. #define PROC_DYNAMIC_FIRST 0xF0000000U
  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. unsigned int i;
  280. int error;
  281. retry:
  282. if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0)
  283. return 0;
  284. spin_lock(&proc_inum_lock);
  285. error = ida_get_new(&proc_inum_ida, &i);
  286. spin_unlock(&proc_inum_lock);
  287. if (error == -EAGAIN)
  288. goto retry;
  289. else if (error)
  290. return 0;
  291. if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
  292. spin_lock(&proc_inum_lock);
  293. ida_remove(&proc_inum_ida, i);
  294. spin_unlock(&proc_inum_lock);
  295. return 0;
  296. }
  297. return PROC_DYNAMIC_FIRST + i;
  298. }
  299. static void release_inode_number(unsigned int inum)
  300. {
  301. spin_lock(&proc_inum_lock);
  302. ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  303. spin_unlock(&proc_inum_lock);
  304. }
  305. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  306. {
  307. nd_set_link(nd, PDE(dentry->d_inode)->data);
  308. return NULL;
  309. }
  310. static const struct inode_operations proc_link_inode_operations = {
  311. .readlink = generic_readlink,
  312. .follow_link = proc_follow_link,
  313. };
  314. /*
  315. * As some entries in /proc are volatile, we want to
  316. * get rid of unused dentries. This could be made
  317. * smarter: we could keep a "volatile" flag in the
  318. * inode to indicate which ones to keep.
  319. */
  320. static int proc_delete_dentry(struct dentry * dentry)
  321. {
  322. return 1;
  323. }
  324. static struct dentry_operations proc_dentry_operations =
  325. {
  326. .d_delete = proc_delete_dentry,
  327. };
  328. /*
  329. * Don't create negative dentries here, return -ENOENT by hand
  330. * instead.
  331. */
  332. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  333. struct dentry *dentry)
  334. {
  335. struct inode *inode = NULL;
  336. int error = -ENOENT;
  337. lock_kernel();
  338. spin_lock(&proc_subdir_lock);
  339. for (de = de->subdir; de ; de = de->next) {
  340. if (de->namelen != dentry->d_name.len)
  341. continue;
  342. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  343. unsigned int ino;
  344. ino = de->low_ino;
  345. de_get(de);
  346. spin_unlock(&proc_subdir_lock);
  347. error = -EINVAL;
  348. inode = proc_get_inode(dir->i_sb, ino, de);
  349. goto out_unlock;
  350. }
  351. }
  352. spin_unlock(&proc_subdir_lock);
  353. out_unlock:
  354. unlock_kernel();
  355. if (inode) {
  356. dentry->d_op = &proc_dentry_operations;
  357. d_add(dentry, inode);
  358. return NULL;
  359. }
  360. if (de)
  361. de_put(de);
  362. return ERR_PTR(error);
  363. }
  364. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  365. struct nameidata *nd)
  366. {
  367. return proc_lookup_de(PDE(dir), dir, dentry);
  368. }
  369. /*
  370. * This returns non-zero if at EOF, so that the /proc
  371. * root directory can use this and check if it should
  372. * continue with the <pid> entries..
  373. *
  374. * Note that the VFS-layer doesn't care about the return
  375. * value of the readdir() call, as long as it's non-negative
  376. * for success..
  377. */
  378. int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
  379. filldir_t filldir)
  380. {
  381. unsigned int ino;
  382. int i;
  383. struct inode *inode = filp->f_path.dentry->d_inode;
  384. int ret = 0;
  385. lock_kernel();
  386. ino = inode->i_ino;
  387. i = filp->f_pos;
  388. switch (i) {
  389. case 0:
  390. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  391. goto out;
  392. i++;
  393. filp->f_pos++;
  394. /* fall through */
  395. case 1:
  396. if (filldir(dirent, "..", 2, i,
  397. parent_ino(filp->f_path.dentry),
  398. DT_DIR) < 0)
  399. goto out;
  400. i++;
  401. filp->f_pos++;
  402. /* fall through */
  403. default:
  404. spin_lock(&proc_subdir_lock);
  405. de = de->subdir;
  406. i -= 2;
  407. for (;;) {
  408. if (!de) {
  409. ret = 1;
  410. spin_unlock(&proc_subdir_lock);
  411. goto out;
  412. }
  413. if (!i)
  414. break;
  415. de = de->next;
  416. i--;
  417. }
  418. do {
  419. struct proc_dir_entry *next;
  420. /* filldir passes info to user space */
  421. de_get(de);
  422. spin_unlock(&proc_subdir_lock);
  423. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  424. de->low_ino, de->mode >> 12) < 0) {
  425. de_put(de);
  426. goto out;
  427. }
  428. spin_lock(&proc_subdir_lock);
  429. filp->f_pos++;
  430. next = de->next;
  431. de_put(de);
  432. de = next;
  433. } while (de);
  434. spin_unlock(&proc_subdir_lock);
  435. }
  436. ret = 1;
  437. out: unlock_kernel();
  438. return ret;
  439. }
  440. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  441. {
  442. struct inode *inode = filp->f_path.dentry->d_inode;
  443. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  444. }
  445. /*
  446. * These are the generic /proc directory operations. They
  447. * use the in-memory "struct proc_dir_entry" tree to parse
  448. * the /proc directory.
  449. */
  450. static const struct file_operations proc_dir_operations = {
  451. .read = generic_read_dir,
  452. .readdir = proc_readdir,
  453. };
  454. /*
  455. * proc directories can do almost nothing..
  456. */
  457. static const struct inode_operations proc_dir_inode_operations = {
  458. .lookup = proc_lookup,
  459. .getattr = proc_getattr,
  460. .setattr = proc_notify_change,
  461. };
  462. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  463. {
  464. unsigned int i;
  465. struct proc_dir_entry *tmp;
  466. i = get_inode_number();
  467. if (i == 0)
  468. return -EAGAIN;
  469. dp->low_ino = i;
  470. if (S_ISDIR(dp->mode)) {
  471. if (dp->proc_iops == NULL) {
  472. dp->proc_fops = &proc_dir_operations;
  473. dp->proc_iops = &proc_dir_inode_operations;
  474. }
  475. dir->nlink++;
  476. } else if (S_ISLNK(dp->mode)) {
  477. if (dp->proc_iops == NULL)
  478. dp->proc_iops = &proc_link_inode_operations;
  479. } else if (S_ISREG(dp->mode)) {
  480. if (dp->proc_fops == NULL)
  481. dp->proc_fops = &proc_file_operations;
  482. if (dp->proc_iops == NULL)
  483. dp->proc_iops = &proc_file_inode_operations;
  484. }
  485. spin_lock(&proc_subdir_lock);
  486. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  487. if (strcmp(tmp->name, dp->name) == 0) {
  488. printk(KERN_WARNING "proc_dir_entry '%s' already "
  489. "registered\n", dp->name);
  490. dump_stack();
  491. break;
  492. }
  493. dp->next = dir->subdir;
  494. dp->parent = dir;
  495. dir->subdir = dp;
  496. spin_unlock(&proc_subdir_lock);
  497. return 0;
  498. }
  499. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  500. const char *name,
  501. mode_t mode,
  502. nlink_t nlink)
  503. {
  504. struct proc_dir_entry *ent = NULL;
  505. const char *fn = name;
  506. int len;
  507. /* make sure name is valid */
  508. if (!name || !strlen(name)) goto out;
  509. if (xlate_proc_name(name, parent, &fn) != 0)
  510. goto out;
  511. /* At this point there must not be any '/' characters beyond *fn */
  512. if (strchr(fn, '/'))
  513. goto out;
  514. len = strlen(fn);
  515. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  516. if (!ent) goto out;
  517. memset(ent, 0, sizeof(struct proc_dir_entry));
  518. memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
  519. ent->name = ((char *) ent) + sizeof(*ent);
  520. ent->namelen = len;
  521. ent->mode = mode;
  522. ent->nlink = nlink;
  523. atomic_set(&ent->count, 1);
  524. ent->pde_users = 0;
  525. spin_lock_init(&ent->pde_unload_lock);
  526. ent->pde_unload_completion = NULL;
  527. INIT_LIST_HEAD(&ent->pde_openers);
  528. out:
  529. return ent;
  530. }
  531. struct proc_dir_entry *proc_symlink(const char *name,
  532. struct proc_dir_entry *parent, const char *dest)
  533. {
  534. struct proc_dir_entry *ent;
  535. ent = __proc_create(&parent, name,
  536. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  537. if (ent) {
  538. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  539. if (ent->data) {
  540. strcpy((char*)ent->data,dest);
  541. if (proc_register(parent, ent) < 0) {
  542. kfree(ent->data);
  543. kfree(ent);
  544. ent = NULL;
  545. }
  546. } else {
  547. kfree(ent);
  548. ent = NULL;
  549. }
  550. }
  551. return ent;
  552. }
  553. struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
  554. struct proc_dir_entry *parent)
  555. {
  556. struct proc_dir_entry *ent;
  557. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  558. if (ent) {
  559. if (proc_register(parent, ent) < 0) {
  560. kfree(ent);
  561. ent = NULL;
  562. }
  563. }
  564. return ent;
  565. }
  566. struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
  567. struct proc_dir_entry *parent)
  568. {
  569. struct proc_dir_entry *ent;
  570. ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
  571. if (ent) {
  572. ent->data = net;
  573. if (proc_register(parent, ent) < 0) {
  574. kfree(ent);
  575. ent = NULL;
  576. }
  577. }
  578. return ent;
  579. }
  580. EXPORT_SYMBOL_GPL(proc_net_mkdir);
  581. struct proc_dir_entry *proc_mkdir(const char *name,
  582. struct proc_dir_entry *parent)
  583. {
  584. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  585. }
  586. struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
  587. struct proc_dir_entry *parent)
  588. {
  589. struct proc_dir_entry *ent;
  590. nlink_t nlink;
  591. if (S_ISDIR(mode)) {
  592. if ((mode & S_IALLUGO) == 0)
  593. mode |= S_IRUGO | S_IXUGO;
  594. nlink = 2;
  595. } else {
  596. if ((mode & S_IFMT) == 0)
  597. mode |= S_IFREG;
  598. if ((mode & S_IALLUGO) == 0)
  599. mode |= S_IRUGO;
  600. nlink = 1;
  601. }
  602. ent = __proc_create(&parent, name, mode, nlink);
  603. if (ent) {
  604. if (proc_register(parent, ent) < 0) {
  605. kfree(ent);
  606. ent = NULL;
  607. }
  608. }
  609. return ent;
  610. }
  611. struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
  612. struct proc_dir_entry *parent,
  613. const struct file_operations *proc_fops,
  614. void *data)
  615. {
  616. struct proc_dir_entry *pde;
  617. nlink_t nlink;
  618. if (S_ISDIR(mode)) {
  619. if ((mode & S_IALLUGO) == 0)
  620. mode |= S_IRUGO | S_IXUGO;
  621. nlink = 2;
  622. } else {
  623. if ((mode & S_IFMT) == 0)
  624. mode |= S_IFREG;
  625. if ((mode & S_IALLUGO) == 0)
  626. mode |= S_IRUGO;
  627. nlink = 1;
  628. }
  629. pde = __proc_create(&parent, name, mode, nlink);
  630. if (!pde)
  631. goto out;
  632. pde->proc_fops = proc_fops;
  633. pde->data = data;
  634. if (proc_register(parent, pde) < 0)
  635. goto out_free;
  636. return pde;
  637. out_free:
  638. kfree(pde);
  639. out:
  640. return NULL;
  641. }
  642. void free_proc_entry(struct proc_dir_entry *de)
  643. {
  644. unsigned int ino = de->low_ino;
  645. if (ino < PROC_DYNAMIC_FIRST)
  646. return;
  647. release_inode_number(ino);
  648. if (S_ISLNK(de->mode))
  649. kfree(de->data);
  650. kfree(de);
  651. }
  652. /*
  653. * Remove a /proc entry and free it if it's not currently in use.
  654. */
  655. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  656. {
  657. struct proc_dir_entry **p;
  658. struct proc_dir_entry *de = NULL;
  659. const char *fn = name;
  660. int len;
  661. if (xlate_proc_name(name, &parent, &fn) != 0)
  662. return;
  663. len = strlen(fn);
  664. spin_lock(&proc_subdir_lock);
  665. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  666. if (proc_match(len, fn, *p)) {
  667. de = *p;
  668. *p = de->next;
  669. de->next = NULL;
  670. break;
  671. }
  672. }
  673. spin_unlock(&proc_subdir_lock);
  674. if (!de)
  675. return;
  676. spin_lock(&de->pde_unload_lock);
  677. /*
  678. * Stop accepting new callers into module. If you're
  679. * dynamically allocating ->proc_fops, save a pointer somewhere.
  680. */
  681. de->proc_fops = NULL;
  682. /* Wait until all existing callers into module are done. */
  683. if (de->pde_users > 0) {
  684. DECLARE_COMPLETION_ONSTACK(c);
  685. if (!de->pde_unload_completion)
  686. de->pde_unload_completion = &c;
  687. spin_unlock(&de->pde_unload_lock);
  688. wait_for_completion(de->pde_unload_completion);
  689. goto continue_removing;
  690. }
  691. spin_unlock(&de->pde_unload_lock);
  692. continue_removing:
  693. spin_lock(&de->pde_unload_lock);
  694. while (!list_empty(&de->pde_openers)) {
  695. struct pde_opener *pdeo;
  696. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  697. list_del(&pdeo->lh);
  698. spin_unlock(&de->pde_unload_lock);
  699. pdeo->release(pdeo->inode, pdeo->file);
  700. kfree(pdeo);
  701. spin_lock(&de->pde_unload_lock);
  702. }
  703. spin_unlock(&de->pde_unload_lock);
  704. if (S_ISDIR(de->mode))
  705. parent->nlink--;
  706. de->nlink = 0;
  707. WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
  708. "'%s/%s', leaking at least '%s'\n", __func__,
  709. de->parent->name, de->name, de->subdir->name);
  710. if (atomic_dec_and_test(&de->count))
  711. free_proc_entry(de);
  712. }