generic.c 19 KB

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