generic.c 20 KB

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