generic.c 19 KB

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