inode.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * linux/fs/proc/inode.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/time.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/string.h>
  11. #include <linux/stat.h>
  12. #include <linux/file.h>
  13. #include <linux/limits.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/smp_lock.h>
  17. #include <asm/system.h>
  18. #include <asm/uaccess.h>
  19. #include "internal.h"
  20. static inline struct proc_dir_entry * de_get(struct proc_dir_entry *de)
  21. {
  22. if (de)
  23. atomic_inc(&de->count);
  24. return de;
  25. }
  26. /*
  27. * Decrements the use count and checks for deferred deletion.
  28. */
  29. static void de_put(struct proc_dir_entry *de)
  30. {
  31. if (de) {
  32. lock_kernel();
  33. if (!atomic_read(&de->count)) {
  34. printk("de_put: entry %s already free!\n", de->name);
  35. unlock_kernel();
  36. return;
  37. }
  38. if (atomic_dec_and_test(&de->count)) {
  39. if (de->deleted) {
  40. printk("de_put: deferred delete of %s\n",
  41. de->name);
  42. free_proc_entry(de);
  43. }
  44. }
  45. unlock_kernel();
  46. }
  47. }
  48. /*
  49. * Decrement the use count of the proc_dir_entry.
  50. */
  51. static void proc_delete_inode(struct inode *inode)
  52. {
  53. struct proc_dir_entry *de;
  54. struct task_struct *tsk;
  55. truncate_inode_pages(&inode->i_data, 0);
  56. /* Let go of any associated process */
  57. tsk = PROC_I(inode)->task;
  58. if (tsk)
  59. put_task_struct(tsk);
  60. /* Let go of any associated proc directory entry */
  61. de = PROC_I(inode)->pde;
  62. if (de) {
  63. if (de->owner)
  64. module_put(de->owner);
  65. de_put(de);
  66. }
  67. clear_inode(inode);
  68. }
  69. struct vfsmount *proc_mnt;
  70. static void proc_read_inode(struct inode * inode)
  71. {
  72. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  73. }
  74. static kmem_cache_t * proc_inode_cachep;
  75. static struct inode *proc_alloc_inode(struct super_block *sb)
  76. {
  77. struct proc_inode *ei;
  78. struct inode *inode;
  79. ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, SLAB_KERNEL);
  80. if (!ei)
  81. return NULL;
  82. ei->task = NULL;
  83. ei->type = 0;
  84. ei->op.proc_get_link = NULL;
  85. ei->pde = NULL;
  86. inode = &ei->vfs_inode;
  87. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  88. return inode;
  89. }
  90. static void proc_destroy_inode(struct inode *inode)
  91. {
  92. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  93. }
  94. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  95. {
  96. struct proc_inode *ei = (struct proc_inode *) foo;
  97. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  98. SLAB_CTOR_CONSTRUCTOR)
  99. inode_init_once(&ei->vfs_inode);
  100. }
  101. int __init proc_init_inodecache(void)
  102. {
  103. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  104. sizeof(struct proc_inode),
  105. 0, SLAB_RECLAIM_ACCOUNT,
  106. init_once, NULL);
  107. if (proc_inode_cachep == NULL)
  108. return -ENOMEM;
  109. return 0;
  110. }
  111. static int proc_remount(struct super_block *sb, int *flags, char *data)
  112. {
  113. *flags |= MS_NODIRATIME;
  114. return 0;
  115. }
  116. static struct super_operations proc_sops = {
  117. .alloc_inode = proc_alloc_inode,
  118. .destroy_inode = proc_destroy_inode,
  119. .read_inode = proc_read_inode,
  120. .drop_inode = generic_delete_inode,
  121. .delete_inode = proc_delete_inode,
  122. .statfs = simple_statfs,
  123. .remount_fs = proc_remount,
  124. };
  125. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  126. struct proc_dir_entry *de)
  127. {
  128. struct inode * inode;
  129. /*
  130. * Increment the use count so the dir entry can't disappear.
  131. */
  132. de_get(de);
  133. WARN_ON(de && de->deleted);
  134. if (de != NULL && !try_module_get(de->owner))
  135. goto out_mod;
  136. inode = iget(sb, ino);
  137. if (!inode)
  138. goto out_ino;
  139. PROC_I(inode)->pde = de;
  140. if (de) {
  141. if (de->mode) {
  142. inode->i_mode = de->mode;
  143. inode->i_uid = de->uid;
  144. inode->i_gid = de->gid;
  145. }
  146. if (de->size)
  147. inode->i_size = de->size;
  148. if (de->nlink)
  149. inode->i_nlink = de->nlink;
  150. if (de->proc_iops)
  151. inode->i_op = de->proc_iops;
  152. if (de->proc_fops)
  153. inode->i_fop = de->proc_fops;
  154. }
  155. return inode;
  156. out_ino:
  157. if (de != NULL)
  158. module_put(de->owner);
  159. out_mod:
  160. de_put(de);
  161. return NULL;
  162. }
  163. int proc_fill_super(struct super_block *s, void *data, int silent)
  164. {
  165. struct inode * root_inode;
  166. s->s_flags |= MS_NODIRATIME;
  167. s->s_blocksize = 1024;
  168. s->s_blocksize_bits = 10;
  169. s->s_magic = PROC_SUPER_MAGIC;
  170. s->s_op = &proc_sops;
  171. s->s_time_gran = 1;
  172. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  173. if (!root_inode)
  174. goto out_no_root;
  175. /*
  176. * Fixup the root inode's nlink value
  177. */
  178. root_inode->i_nlink += nr_processes();
  179. root_inode->i_uid = 0;
  180. root_inode->i_gid = 0;
  181. s->s_root = d_alloc_root(root_inode);
  182. if (!s->s_root)
  183. goto out_no_root;
  184. return 0;
  185. out_no_root:
  186. printk("proc_read_super: get root inode failed\n");
  187. iput(root_inode);
  188. return -ENOMEM;
  189. }
  190. MODULE_LICENSE("GPL");