generic.c 19 KB

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