generic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 ssize_t
  181. proc_file_write(struct file *file, const char __user *buffer,
  182. size_t count, loff_t *ppos)
  183. {
  184. struct proc_dir_entry *pde = PDE(file_inode(file));
  185. ssize_t rv = -EIO;
  186. if (pde->write_proc) {
  187. spin_lock(&pde->pde_unload_lock);
  188. if (!pde->proc_fops) {
  189. spin_unlock(&pde->pde_unload_lock);
  190. return rv;
  191. }
  192. pde->pde_users++;
  193. spin_unlock(&pde->pde_unload_lock);
  194. /* FIXME: does this routine need ppos? probably... */
  195. rv = pde->write_proc(file, buffer, count, pde->data);
  196. pde_users_dec(pde);
  197. }
  198. return rv;
  199. }
  200. static loff_t
  201. proc_file_lseek(struct file *file, loff_t offset, int orig)
  202. {
  203. loff_t retval = -EINVAL;
  204. switch (orig) {
  205. case 1:
  206. offset += file->f_pos;
  207. /* fallthrough */
  208. case 0:
  209. if (offset < 0 || offset > MAX_NON_LFS)
  210. break;
  211. file->f_pos = retval = offset;
  212. }
  213. return retval;
  214. }
  215. static const struct file_operations proc_file_operations = {
  216. .llseek = proc_file_lseek,
  217. .read = proc_file_read,
  218. .write = proc_file_write,
  219. };
  220. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  221. {
  222. struct inode *inode = dentry->d_inode;
  223. struct proc_dir_entry *de = PDE(inode);
  224. int error;
  225. error = inode_change_ok(inode, iattr);
  226. if (error)
  227. return error;
  228. setattr_copy(inode, iattr);
  229. mark_inode_dirty(inode);
  230. de->uid = inode->i_uid;
  231. de->gid = inode->i_gid;
  232. de->mode = inode->i_mode;
  233. return 0;
  234. }
  235. static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
  236. struct kstat *stat)
  237. {
  238. struct inode *inode = dentry->d_inode;
  239. struct proc_dir_entry *de = PROC_I(inode)->pde;
  240. if (de && de->nlink)
  241. set_nlink(inode, de->nlink);
  242. generic_fillattr(inode, stat);
  243. return 0;
  244. }
  245. static const struct inode_operations proc_file_inode_operations = {
  246. .setattr = proc_notify_change,
  247. };
  248. /*
  249. * This function parses a name such as "tty/driver/serial", and
  250. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  251. * returns "serial" in residual.
  252. */
  253. static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  254. const char **residual)
  255. {
  256. const char *cp = name, *next;
  257. struct proc_dir_entry *de;
  258. unsigned int len;
  259. de = *ret;
  260. if (!de)
  261. de = &proc_root;
  262. while (1) {
  263. next = strchr(cp, '/');
  264. if (!next)
  265. break;
  266. len = next - cp;
  267. for (de = de->subdir; de ; de = de->next) {
  268. if (proc_match(len, cp, de))
  269. break;
  270. }
  271. if (!de) {
  272. WARN(1, "name '%s'\n", name);
  273. return -ENOENT;
  274. }
  275. cp += len + 1;
  276. }
  277. *residual = cp;
  278. *ret = de;
  279. return 0;
  280. }
  281. static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  282. const char **residual)
  283. {
  284. int rv;
  285. spin_lock(&proc_subdir_lock);
  286. rv = __xlate_proc_name(name, ret, residual);
  287. spin_unlock(&proc_subdir_lock);
  288. return rv;
  289. }
  290. static DEFINE_IDA(proc_inum_ida);
  291. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  292. #define PROC_DYNAMIC_FIRST 0xF0000000U
  293. /*
  294. * Return an inode number between PROC_DYNAMIC_FIRST and
  295. * 0xffffffff, or zero on failure.
  296. */
  297. int proc_alloc_inum(unsigned int *inum)
  298. {
  299. unsigned int i;
  300. int error;
  301. retry:
  302. if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
  303. return -ENOMEM;
  304. spin_lock_irq(&proc_inum_lock);
  305. error = ida_get_new(&proc_inum_ida, &i);
  306. spin_unlock_irq(&proc_inum_lock);
  307. if (error == -EAGAIN)
  308. goto retry;
  309. else if (error)
  310. return error;
  311. if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
  312. spin_lock_irq(&proc_inum_lock);
  313. ida_remove(&proc_inum_ida, i);
  314. spin_unlock_irq(&proc_inum_lock);
  315. return -ENOSPC;
  316. }
  317. *inum = PROC_DYNAMIC_FIRST + i;
  318. return 0;
  319. }
  320. void proc_free_inum(unsigned int inum)
  321. {
  322. unsigned long flags;
  323. spin_lock_irqsave(&proc_inum_lock, flags);
  324. ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  325. spin_unlock_irqrestore(&proc_inum_lock, flags);
  326. }
  327. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  328. {
  329. nd_set_link(nd, PDE(dentry->d_inode)->data);
  330. return NULL;
  331. }
  332. static const struct inode_operations proc_link_inode_operations = {
  333. .readlink = generic_readlink,
  334. .follow_link = proc_follow_link,
  335. };
  336. /*
  337. * As some entries in /proc are volatile, we want to
  338. * get rid of unused dentries. This could be made
  339. * smarter: we could keep a "volatile" flag in the
  340. * inode to indicate which ones to keep.
  341. */
  342. static int proc_delete_dentry(const struct dentry * dentry)
  343. {
  344. return 1;
  345. }
  346. static const struct dentry_operations proc_dentry_operations =
  347. {
  348. .d_delete = proc_delete_dentry,
  349. };
  350. /*
  351. * Don't create negative dentries here, return -ENOENT by hand
  352. * instead.
  353. */
  354. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  355. struct dentry *dentry)
  356. {
  357. struct inode *inode;
  358. spin_lock(&proc_subdir_lock);
  359. for (de = de->subdir; de ; de = de->next) {
  360. if (de->namelen != dentry->d_name.len)
  361. continue;
  362. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  363. pde_get(de);
  364. spin_unlock(&proc_subdir_lock);
  365. inode = proc_get_inode(dir->i_sb, de);
  366. if (!inode)
  367. return ERR_PTR(-ENOMEM);
  368. d_set_d_op(dentry, &proc_dentry_operations);
  369. d_add(dentry, inode);
  370. return NULL;
  371. }
  372. }
  373. spin_unlock(&proc_subdir_lock);
  374. return ERR_PTR(-ENOENT);
  375. }
  376. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  377. unsigned int flags)
  378. {
  379. return proc_lookup_de(PDE(dir), dir, dentry);
  380. }
  381. /*
  382. * This returns non-zero if at EOF, so that the /proc
  383. * root directory can use this and check if it should
  384. * continue with the <pid> entries..
  385. *
  386. * Note that the VFS-layer doesn't care about the return
  387. * value of the readdir() call, as long as it's non-negative
  388. * for success..
  389. */
  390. int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
  391. filldir_t filldir)
  392. {
  393. unsigned int ino;
  394. int i;
  395. struct inode *inode = file_inode(filp);
  396. int ret = 0;
  397. ino = inode->i_ino;
  398. i = filp->f_pos;
  399. switch (i) {
  400. case 0:
  401. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  402. goto out;
  403. i++;
  404. filp->f_pos++;
  405. /* fall through */
  406. case 1:
  407. if (filldir(dirent, "..", 2, i,
  408. parent_ino(filp->f_path.dentry),
  409. DT_DIR) < 0)
  410. goto out;
  411. i++;
  412. filp->f_pos++;
  413. /* fall through */
  414. default:
  415. spin_lock(&proc_subdir_lock);
  416. de = de->subdir;
  417. i -= 2;
  418. for (;;) {
  419. if (!de) {
  420. ret = 1;
  421. spin_unlock(&proc_subdir_lock);
  422. goto out;
  423. }
  424. if (!i)
  425. break;
  426. de = de->next;
  427. i--;
  428. }
  429. do {
  430. struct proc_dir_entry *next;
  431. /* filldir passes info to user space */
  432. pde_get(de);
  433. spin_unlock(&proc_subdir_lock);
  434. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  435. de->low_ino, de->mode >> 12) < 0) {
  436. pde_put(de);
  437. goto out;
  438. }
  439. spin_lock(&proc_subdir_lock);
  440. filp->f_pos++;
  441. next = de->next;
  442. pde_put(de);
  443. de = next;
  444. } while (de);
  445. spin_unlock(&proc_subdir_lock);
  446. }
  447. ret = 1;
  448. out:
  449. return ret;
  450. }
  451. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  452. {
  453. struct inode *inode = file_inode(filp);
  454. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  455. }
  456. /*
  457. * These are the generic /proc directory operations. They
  458. * use the in-memory "struct proc_dir_entry" tree to parse
  459. * the /proc directory.
  460. */
  461. static const struct file_operations proc_dir_operations = {
  462. .llseek = generic_file_llseek,
  463. .read = generic_read_dir,
  464. .readdir = proc_readdir,
  465. };
  466. /*
  467. * proc directories can do almost nothing..
  468. */
  469. static const struct inode_operations proc_dir_inode_operations = {
  470. .lookup = proc_lookup,
  471. .getattr = proc_getattr,
  472. .setattr = proc_notify_change,
  473. };
  474. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  475. {
  476. struct proc_dir_entry *tmp;
  477. int ret;
  478. ret = proc_alloc_inum(&dp->low_ino);
  479. if (ret)
  480. return ret;
  481. if (S_ISDIR(dp->mode)) {
  482. if (dp->proc_iops == NULL) {
  483. dp->proc_fops = &proc_dir_operations;
  484. dp->proc_iops = &proc_dir_inode_operations;
  485. }
  486. dir->nlink++;
  487. } else if (S_ISLNK(dp->mode)) {
  488. if (dp->proc_iops == NULL)
  489. dp->proc_iops = &proc_link_inode_operations;
  490. } else if (S_ISREG(dp->mode)) {
  491. if (dp->proc_fops == NULL)
  492. dp->proc_fops = &proc_file_operations;
  493. if (dp->proc_iops == NULL)
  494. dp->proc_iops = &proc_file_inode_operations;
  495. }
  496. spin_lock(&proc_subdir_lock);
  497. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  498. if (strcmp(tmp->name, dp->name) == 0) {
  499. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  500. dir->name, dp->name);
  501. break;
  502. }
  503. dp->next = dir->subdir;
  504. dp->parent = dir;
  505. dir->subdir = dp;
  506. spin_unlock(&proc_subdir_lock);
  507. return 0;
  508. }
  509. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  510. const char *name,
  511. umode_t mode,
  512. nlink_t nlink)
  513. {
  514. struct proc_dir_entry *ent = NULL;
  515. const char *fn = name;
  516. unsigned int len;
  517. /* make sure name is valid */
  518. if (!name || !strlen(name))
  519. goto out;
  520. if (xlate_proc_name(name, parent, &fn) != 0)
  521. goto out;
  522. /* At this point there must not be any '/' characters beyond *fn */
  523. if (strchr(fn, '/'))
  524. goto out;
  525. len = strlen(fn);
  526. ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  527. if (!ent)
  528. goto out;
  529. memcpy(ent->name, fn, len + 1);
  530. ent->namelen = len;
  531. ent->mode = mode;
  532. ent->nlink = nlink;
  533. atomic_set(&ent->count, 1);
  534. spin_lock_init(&ent->pde_unload_lock);
  535. INIT_LIST_HEAD(&ent->pde_openers);
  536. out:
  537. return ent;
  538. }
  539. struct proc_dir_entry *proc_symlink(const char *name,
  540. struct proc_dir_entry *parent, const char *dest)
  541. {
  542. struct proc_dir_entry *ent;
  543. ent = __proc_create(&parent, name,
  544. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  545. if (ent) {
  546. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  547. if (ent->data) {
  548. strcpy((char*)ent->data,dest);
  549. if (proc_register(parent, ent) < 0) {
  550. kfree(ent->data);
  551. kfree(ent);
  552. ent = NULL;
  553. }
  554. } else {
  555. kfree(ent);
  556. ent = NULL;
  557. }
  558. }
  559. return ent;
  560. }
  561. EXPORT_SYMBOL(proc_symlink);
  562. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  563. struct proc_dir_entry *parent)
  564. {
  565. struct proc_dir_entry *ent;
  566. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  567. if (ent) {
  568. if (proc_register(parent, ent) < 0) {
  569. kfree(ent);
  570. ent = NULL;
  571. }
  572. }
  573. return ent;
  574. }
  575. EXPORT_SYMBOL(proc_mkdir_mode);
  576. struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
  577. struct proc_dir_entry *parent)
  578. {
  579. struct proc_dir_entry *ent;
  580. ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
  581. if (ent) {
  582. ent->data = net;
  583. if (proc_register(parent, ent) < 0) {
  584. kfree(ent);
  585. ent = NULL;
  586. }
  587. }
  588. return ent;
  589. }
  590. EXPORT_SYMBOL_GPL(proc_net_mkdir);
  591. struct proc_dir_entry *proc_mkdir(const char *name,
  592. struct proc_dir_entry *parent)
  593. {
  594. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  595. }
  596. EXPORT_SYMBOL(proc_mkdir);
  597. struct proc_dir_entry *create_proc_entry(const char *name, umode_t mode,
  598. struct proc_dir_entry *parent)
  599. {
  600. struct proc_dir_entry *ent;
  601. nlink_t nlink;
  602. if (S_ISDIR(mode)) {
  603. if ((mode & S_IALLUGO) == 0)
  604. mode |= S_IRUGO | S_IXUGO;
  605. nlink = 2;
  606. } else {
  607. if ((mode & S_IFMT) == 0)
  608. mode |= S_IFREG;
  609. if ((mode & S_IALLUGO) == 0)
  610. mode |= S_IRUGO;
  611. nlink = 1;
  612. }
  613. ent = __proc_create(&parent, name, mode, nlink);
  614. if (ent) {
  615. if (proc_register(parent, ent) < 0) {
  616. kfree(ent);
  617. ent = NULL;
  618. }
  619. }
  620. return ent;
  621. }
  622. EXPORT_SYMBOL(create_proc_entry);
  623. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  624. struct proc_dir_entry *parent,
  625. const struct file_operations *proc_fops,
  626. void *data)
  627. {
  628. struct proc_dir_entry *pde;
  629. nlink_t nlink;
  630. if (S_ISDIR(mode)) {
  631. if ((mode & S_IALLUGO) == 0)
  632. mode |= S_IRUGO | S_IXUGO;
  633. nlink = 2;
  634. } else {
  635. if ((mode & S_IFMT) == 0)
  636. mode |= S_IFREG;
  637. if ((mode & S_IALLUGO) == 0)
  638. mode |= S_IRUGO;
  639. nlink = 1;
  640. }
  641. pde = __proc_create(&parent, name, mode, nlink);
  642. if (!pde)
  643. goto out;
  644. pde->proc_fops = proc_fops;
  645. pde->data = data;
  646. if (proc_register(parent, pde) < 0)
  647. goto out_free;
  648. return pde;
  649. out_free:
  650. kfree(pde);
  651. out:
  652. return NULL;
  653. }
  654. EXPORT_SYMBOL(proc_create_data);
  655. static void free_proc_entry(struct proc_dir_entry *de)
  656. {
  657. proc_free_inum(de->low_ino);
  658. if (S_ISLNK(de->mode))
  659. kfree(de->data);
  660. kfree(de);
  661. }
  662. void pde_put(struct proc_dir_entry *pde)
  663. {
  664. if (atomic_dec_and_test(&pde->count))
  665. free_proc_entry(pde);
  666. }
  667. static void entry_rundown(struct proc_dir_entry *de)
  668. {
  669. spin_lock(&de->pde_unload_lock);
  670. /*
  671. * Stop accepting new callers into module. If you're
  672. * dynamically allocating ->proc_fops, save a pointer somewhere.
  673. */
  674. de->proc_fops = NULL;
  675. /* Wait until all existing callers into module are done. */
  676. if (de->pde_users > 0) {
  677. DECLARE_COMPLETION_ONSTACK(c);
  678. if (!de->pde_unload_completion)
  679. de->pde_unload_completion = &c;
  680. spin_unlock(&de->pde_unload_lock);
  681. wait_for_completion(de->pde_unload_completion);
  682. spin_lock(&de->pde_unload_lock);
  683. }
  684. while (!list_empty(&de->pde_openers)) {
  685. struct pde_opener *pdeo;
  686. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  687. list_del(&pdeo->lh);
  688. spin_unlock(&de->pde_unload_lock);
  689. pdeo->release(pdeo->inode, pdeo->file);
  690. kfree(pdeo);
  691. spin_lock(&de->pde_unload_lock);
  692. }
  693. spin_unlock(&de->pde_unload_lock);
  694. }
  695. /*
  696. * Remove a /proc entry and free it if it's not currently in use.
  697. */
  698. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  699. {
  700. struct proc_dir_entry **p;
  701. struct proc_dir_entry *de = NULL;
  702. const char *fn = name;
  703. unsigned int len;
  704. spin_lock(&proc_subdir_lock);
  705. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  706. spin_unlock(&proc_subdir_lock);
  707. return;
  708. }
  709. len = strlen(fn);
  710. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  711. if (proc_match(len, fn, *p)) {
  712. de = *p;
  713. *p = de->next;
  714. de->next = NULL;
  715. break;
  716. }
  717. }
  718. spin_unlock(&proc_subdir_lock);
  719. if (!de) {
  720. WARN(1, "name '%s'\n", name);
  721. return;
  722. }
  723. entry_rundown(de);
  724. if (S_ISDIR(de->mode))
  725. parent->nlink--;
  726. de->nlink = 0;
  727. WARN(de->subdir, "%s: removing non-empty directory "
  728. "'%s/%s', leaking at least '%s'\n", __func__,
  729. de->parent->name, de->name, de->subdir->name);
  730. pde_put(de);
  731. }
  732. EXPORT_SYMBOL(remove_proc_entry);
  733. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  734. {
  735. struct proc_dir_entry **p;
  736. struct proc_dir_entry *root = NULL, *de, *next;
  737. const char *fn = name;
  738. unsigned int len;
  739. spin_lock(&proc_subdir_lock);
  740. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  741. spin_unlock(&proc_subdir_lock);
  742. return -ENOENT;
  743. }
  744. len = strlen(fn);
  745. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  746. if (proc_match(len, fn, *p)) {
  747. root = *p;
  748. *p = root->next;
  749. root->next = NULL;
  750. break;
  751. }
  752. }
  753. if (!root) {
  754. spin_unlock(&proc_subdir_lock);
  755. return -ENOENT;
  756. }
  757. de = root;
  758. while (1) {
  759. next = de->subdir;
  760. if (next) {
  761. de->subdir = next->next;
  762. next->next = NULL;
  763. de = next;
  764. continue;
  765. }
  766. spin_unlock(&proc_subdir_lock);
  767. entry_rundown(de);
  768. next = de->parent;
  769. if (S_ISDIR(de->mode))
  770. next->nlink--;
  771. de->nlink = 0;
  772. if (de == root)
  773. break;
  774. pde_put(de);
  775. spin_lock(&proc_subdir_lock);
  776. de = next;
  777. }
  778. pde_put(root);
  779. return 0;
  780. }
  781. EXPORT_SYMBOL(remove_proc_subtree);