generic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. set_nlink(inode, 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. int proc_alloc_inum(unsigned int *inum)
  313. {
  314. unsigned int i;
  315. int error;
  316. retry:
  317. if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
  318. return -ENOMEM;
  319. spin_lock_irq(&proc_inum_lock);
  320. error = ida_get_new(&proc_inum_ida, &i);
  321. spin_unlock_irq(&proc_inum_lock);
  322. if (error == -EAGAIN)
  323. goto retry;
  324. else if (error)
  325. return error;
  326. if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
  327. spin_lock_irq(&proc_inum_lock);
  328. ida_remove(&proc_inum_ida, i);
  329. spin_unlock_irq(&proc_inum_lock);
  330. return -ENOSPC;
  331. }
  332. *inum = PROC_DYNAMIC_FIRST + i;
  333. return 0;
  334. }
  335. void proc_free_inum(unsigned int inum)
  336. {
  337. unsigned long flags;
  338. spin_lock_irqsave(&proc_inum_lock, flags);
  339. ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  340. spin_unlock_irqrestore(&proc_inum_lock, flags);
  341. }
  342. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  343. {
  344. nd_set_link(nd, PDE(dentry->d_inode)->data);
  345. return NULL;
  346. }
  347. static const struct inode_operations proc_link_inode_operations = {
  348. .readlink = generic_readlink,
  349. .follow_link = proc_follow_link,
  350. };
  351. /*
  352. * As some entries in /proc are volatile, we want to
  353. * get rid of unused dentries. This could be made
  354. * smarter: we could keep a "volatile" flag in the
  355. * inode to indicate which ones to keep.
  356. */
  357. static int proc_delete_dentry(const struct dentry * dentry)
  358. {
  359. return 1;
  360. }
  361. static const struct dentry_operations proc_dentry_operations =
  362. {
  363. .d_delete = proc_delete_dentry,
  364. };
  365. /*
  366. * Don't create negative dentries here, return -ENOENT by hand
  367. * instead.
  368. */
  369. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  370. struct dentry *dentry)
  371. {
  372. struct inode *inode = NULL;
  373. int error = -ENOENT;
  374. spin_lock(&proc_subdir_lock);
  375. for (de = de->subdir; de ; de = de->next) {
  376. if (de->namelen != dentry->d_name.len)
  377. continue;
  378. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  379. pde_get(de);
  380. spin_unlock(&proc_subdir_lock);
  381. error = -EINVAL;
  382. inode = proc_get_inode(dir->i_sb, de);
  383. goto out_unlock;
  384. }
  385. }
  386. spin_unlock(&proc_subdir_lock);
  387. out_unlock:
  388. if (inode) {
  389. d_set_d_op(dentry, &proc_dentry_operations);
  390. d_add(dentry, inode);
  391. return NULL;
  392. }
  393. if (de)
  394. pde_put(de);
  395. return ERR_PTR(error);
  396. }
  397. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  398. struct nameidata *nd)
  399. {
  400. return proc_lookup_de(PDE(dir), dir, dentry);
  401. }
  402. /*
  403. * This returns non-zero if at EOF, so that the /proc
  404. * root directory can use this and check if it should
  405. * continue with the <pid> entries..
  406. *
  407. * Note that the VFS-layer doesn't care about the return
  408. * value of the readdir() call, as long as it's non-negative
  409. * for success..
  410. */
  411. int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
  412. filldir_t filldir)
  413. {
  414. unsigned int ino;
  415. int i;
  416. struct inode *inode = filp->f_path.dentry->d_inode;
  417. int ret = 0;
  418. ino = inode->i_ino;
  419. i = filp->f_pos;
  420. switch (i) {
  421. case 0:
  422. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  423. goto out;
  424. i++;
  425. filp->f_pos++;
  426. /* fall through */
  427. case 1:
  428. if (filldir(dirent, "..", 2, i,
  429. parent_ino(filp->f_path.dentry),
  430. DT_DIR) < 0)
  431. goto out;
  432. i++;
  433. filp->f_pos++;
  434. /* fall through */
  435. default:
  436. spin_lock(&proc_subdir_lock);
  437. de = de->subdir;
  438. i -= 2;
  439. for (;;) {
  440. if (!de) {
  441. ret = 1;
  442. spin_unlock(&proc_subdir_lock);
  443. goto out;
  444. }
  445. if (!i)
  446. break;
  447. de = de->next;
  448. i--;
  449. }
  450. do {
  451. struct proc_dir_entry *next;
  452. /* filldir passes info to user space */
  453. pde_get(de);
  454. spin_unlock(&proc_subdir_lock);
  455. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  456. de->low_ino, de->mode >> 12) < 0) {
  457. pde_put(de);
  458. goto out;
  459. }
  460. spin_lock(&proc_subdir_lock);
  461. filp->f_pos++;
  462. next = de->next;
  463. pde_put(de);
  464. de = next;
  465. } while (de);
  466. spin_unlock(&proc_subdir_lock);
  467. }
  468. ret = 1;
  469. out:
  470. return ret;
  471. }
  472. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  473. {
  474. struct inode *inode = filp->f_path.dentry->d_inode;
  475. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  476. }
  477. /*
  478. * These are the generic /proc directory operations. They
  479. * use the in-memory "struct proc_dir_entry" tree to parse
  480. * the /proc directory.
  481. */
  482. static const struct file_operations proc_dir_operations = {
  483. .llseek = generic_file_llseek,
  484. .read = generic_read_dir,
  485. .readdir = proc_readdir,
  486. };
  487. /*
  488. * proc directories can do almost nothing..
  489. */
  490. static const struct inode_operations proc_dir_inode_operations = {
  491. .lookup = proc_lookup,
  492. .getattr = proc_getattr,
  493. .setattr = proc_notify_change,
  494. };
  495. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  496. {
  497. struct proc_dir_entry *tmp;
  498. int ret;
  499. ret = proc_alloc_inum(&dp->low_ino);
  500. if (ret)
  501. return ret;
  502. if (S_ISDIR(dp->mode)) {
  503. if (dp->proc_iops == NULL) {
  504. dp->proc_fops = &proc_dir_operations;
  505. dp->proc_iops = &proc_dir_inode_operations;
  506. }
  507. dir->nlink++;
  508. } else if (S_ISLNK(dp->mode)) {
  509. if (dp->proc_iops == NULL)
  510. dp->proc_iops = &proc_link_inode_operations;
  511. } else if (S_ISREG(dp->mode)) {
  512. if (dp->proc_fops == NULL)
  513. dp->proc_fops = &proc_file_operations;
  514. if (dp->proc_iops == NULL)
  515. dp->proc_iops = &proc_file_inode_operations;
  516. }
  517. spin_lock(&proc_subdir_lock);
  518. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  519. if (strcmp(tmp->name, dp->name) == 0) {
  520. WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n",
  521. dir->name, dp->name);
  522. break;
  523. }
  524. dp->next = dir->subdir;
  525. dp->parent = dir;
  526. dir->subdir = dp;
  527. spin_unlock(&proc_subdir_lock);
  528. return 0;
  529. }
  530. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  531. const char *name,
  532. umode_t mode,
  533. nlink_t nlink)
  534. {
  535. struct proc_dir_entry *ent = NULL;
  536. const char *fn = name;
  537. unsigned int len;
  538. /* make sure name is valid */
  539. if (!name || !strlen(name)) goto out;
  540. if (xlate_proc_name(name, parent, &fn) != 0)
  541. goto out;
  542. /* At this point there must not be any '/' characters beyond *fn */
  543. if (strchr(fn, '/'))
  544. goto out;
  545. len = strlen(fn);
  546. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  547. if (!ent) goto out;
  548. memset(ent, 0, sizeof(struct proc_dir_entry));
  549. memcpy(ent->name, fn, len + 1);
  550. ent->namelen = len;
  551. ent->mode = mode;
  552. ent->nlink = nlink;
  553. atomic_set(&ent->count, 1);
  554. ent->pde_users = 0;
  555. spin_lock_init(&ent->pde_unload_lock);
  556. ent->pde_unload_completion = NULL;
  557. INIT_LIST_HEAD(&ent->pde_openers);
  558. out:
  559. return ent;
  560. }
  561. struct proc_dir_entry *proc_symlink(const char *name,
  562. struct proc_dir_entry *parent, const char *dest)
  563. {
  564. struct proc_dir_entry *ent;
  565. ent = __proc_create(&parent, name,
  566. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  567. if (ent) {
  568. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  569. if (ent->data) {
  570. strcpy((char*)ent->data,dest);
  571. if (proc_register(parent, ent) < 0) {
  572. kfree(ent->data);
  573. kfree(ent);
  574. ent = NULL;
  575. }
  576. } else {
  577. kfree(ent);
  578. ent = NULL;
  579. }
  580. }
  581. return ent;
  582. }
  583. EXPORT_SYMBOL(proc_symlink);
  584. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  585. struct proc_dir_entry *parent)
  586. {
  587. struct proc_dir_entry *ent;
  588. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  589. if (ent) {
  590. if (proc_register(parent, ent) < 0) {
  591. kfree(ent);
  592. ent = NULL;
  593. }
  594. }
  595. return ent;
  596. }
  597. EXPORT_SYMBOL(proc_mkdir_mode);
  598. struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
  599. struct proc_dir_entry *parent)
  600. {
  601. struct proc_dir_entry *ent;
  602. ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
  603. if (ent) {
  604. ent->data = net;
  605. if (proc_register(parent, ent) < 0) {
  606. kfree(ent);
  607. ent = NULL;
  608. }
  609. }
  610. return ent;
  611. }
  612. EXPORT_SYMBOL_GPL(proc_net_mkdir);
  613. struct proc_dir_entry *proc_mkdir(const char *name,
  614. struct proc_dir_entry *parent)
  615. {
  616. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  617. }
  618. EXPORT_SYMBOL(proc_mkdir);
  619. struct proc_dir_entry *create_proc_entry(const char *name, umode_t mode,
  620. struct proc_dir_entry *parent)
  621. {
  622. struct proc_dir_entry *ent;
  623. nlink_t nlink;
  624. if (S_ISDIR(mode)) {
  625. if ((mode & S_IALLUGO) == 0)
  626. mode |= S_IRUGO | S_IXUGO;
  627. nlink = 2;
  628. } else {
  629. if ((mode & S_IFMT) == 0)
  630. mode |= S_IFREG;
  631. if ((mode & S_IALLUGO) == 0)
  632. mode |= S_IRUGO;
  633. nlink = 1;
  634. }
  635. ent = __proc_create(&parent, name, mode, nlink);
  636. if (ent) {
  637. if (proc_register(parent, ent) < 0) {
  638. kfree(ent);
  639. ent = NULL;
  640. }
  641. }
  642. return ent;
  643. }
  644. EXPORT_SYMBOL(create_proc_entry);
  645. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  646. struct proc_dir_entry *parent,
  647. const struct file_operations *proc_fops,
  648. void *data)
  649. {
  650. struct proc_dir_entry *pde;
  651. nlink_t nlink;
  652. if (S_ISDIR(mode)) {
  653. if ((mode & S_IALLUGO) == 0)
  654. mode |= S_IRUGO | S_IXUGO;
  655. nlink = 2;
  656. } else {
  657. if ((mode & S_IFMT) == 0)
  658. mode |= S_IFREG;
  659. if ((mode & S_IALLUGO) == 0)
  660. mode |= S_IRUGO;
  661. nlink = 1;
  662. }
  663. pde = __proc_create(&parent, name, mode, nlink);
  664. if (!pde)
  665. goto out;
  666. pde->proc_fops = proc_fops;
  667. pde->data = data;
  668. if (proc_register(parent, pde) < 0)
  669. goto out_free;
  670. return pde;
  671. out_free:
  672. kfree(pde);
  673. out:
  674. return NULL;
  675. }
  676. EXPORT_SYMBOL(proc_create_data);
  677. static void free_proc_entry(struct proc_dir_entry *de)
  678. {
  679. proc_free_inum(de->low_ino);
  680. if (S_ISLNK(de->mode))
  681. kfree(de->data);
  682. kfree(de);
  683. }
  684. void pde_put(struct proc_dir_entry *pde)
  685. {
  686. if (atomic_dec_and_test(&pde->count))
  687. free_proc_entry(pde);
  688. }
  689. /*
  690. * Remove a /proc entry and free it if it's not currently in use.
  691. */
  692. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  693. {
  694. struct proc_dir_entry **p;
  695. struct proc_dir_entry *de = NULL;
  696. const char *fn = name;
  697. unsigned int len;
  698. spin_lock(&proc_subdir_lock);
  699. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  700. spin_unlock(&proc_subdir_lock);
  701. return;
  702. }
  703. len = strlen(fn);
  704. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  705. if (proc_match(len, fn, *p)) {
  706. de = *p;
  707. *p = de->next;
  708. de->next = NULL;
  709. break;
  710. }
  711. }
  712. spin_unlock(&proc_subdir_lock);
  713. if (!de) {
  714. WARN(1, "name '%s'\n", name);
  715. return;
  716. }
  717. spin_lock(&de->pde_unload_lock);
  718. /*
  719. * Stop accepting new callers into module. If you're
  720. * dynamically allocating ->proc_fops, save a pointer somewhere.
  721. */
  722. de->proc_fops = NULL;
  723. /* Wait until all existing callers into module are done. */
  724. if (de->pde_users > 0) {
  725. DECLARE_COMPLETION_ONSTACK(c);
  726. if (!de->pde_unload_completion)
  727. de->pde_unload_completion = &c;
  728. spin_unlock(&de->pde_unload_lock);
  729. wait_for_completion(de->pde_unload_completion);
  730. spin_lock(&de->pde_unload_lock);
  731. }
  732. while (!list_empty(&de->pde_openers)) {
  733. struct pde_opener *pdeo;
  734. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  735. list_del(&pdeo->lh);
  736. spin_unlock(&de->pde_unload_lock);
  737. pdeo->release(pdeo->inode, pdeo->file);
  738. kfree(pdeo);
  739. spin_lock(&de->pde_unload_lock);
  740. }
  741. spin_unlock(&de->pde_unload_lock);
  742. if (S_ISDIR(de->mode))
  743. parent->nlink--;
  744. de->nlink = 0;
  745. WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
  746. "'%s/%s', leaking at least '%s'\n", __func__,
  747. de->parent->name, de->name, de->subdir->name);
  748. pde_put(de);
  749. }
  750. EXPORT_SYMBOL(remove_proc_entry);