generic.c 20 KB

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