inode.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. SLAB_MEM_SPREAD),
  107. init_once, NULL);
  108. if (proc_inode_cachep == NULL)
  109. return -ENOMEM;
  110. return 0;
  111. }
  112. static int proc_remount(struct super_block *sb, int *flags, char *data)
  113. {
  114. *flags |= MS_NODIRATIME;
  115. return 0;
  116. }
  117. static struct super_operations proc_sops = {
  118. .alloc_inode = proc_alloc_inode,
  119. .destroy_inode = proc_destroy_inode,
  120. .read_inode = proc_read_inode,
  121. .drop_inode = generic_delete_inode,
  122. .delete_inode = proc_delete_inode,
  123. .statfs = simple_statfs,
  124. .remount_fs = proc_remount,
  125. };
  126. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  127. struct proc_dir_entry *de)
  128. {
  129. struct inode * inode;
  130. /*
  131. * Increment the use count so the dir entry can't disappear.
  132. */
  133. de_get(de);
  134. WARN_ON(de && de->deleted);
  135. if (de != NULL && !try_module_get(de->owner))
  136. goto out_mod;
  137. inode = iget(sb, ino);
  138. if (!inode)
  139. goto out_ino;
  140. PROC_I(inode)->pde = de;
  141. if (de) {
  142. if (de->mode) {
  143. inode->i_mode = de->mode;
  144. inode->i_uid = de->uid;
  145. inode->i_gid = de->gid;
  146. }
  147. if (de->size)
  148. inode->i_size = de->size;
  149. if (de->nlink)
  150. inode->i_nlink = de->nlink;
  151. if (de->proc_iops)
  152. inode->i_op = de->proc_iops;
  153. if (de->proc_fops)
  154. inode->i_fop = de->proc_fops;
  155. }
  156. return inode;
  157. out_ino:
  158. if (de != NULL)
  159. module_put(de->owner);
  160. out_mod:
  161. de_put(de);
  162. return NULL;
  163. }
  164. int proc_fill_super(struct super_block *s, void *data, int silent)
  165. {
  166. struct inode * root_inode;
  167. s->s_flags |= MS_NODIRATIME;
  168. s->s_blocksize = 1024;
  169. s->s_blocksize_bits = 10;
  170. s->s_magic = PROC_SUPER_MAGIC;
  171. s->s_op = &proc_sops;
  172. s->s_time_gran = 1;
  173. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  174. if (!root_inode)
  175. goto out_no_root;
  176. root_inode->i_uid = 0;
  177. root_inode->i_gid = 0;
  178. s->s_root = d_alloc_root(root_inode);
  179. if (!s->s_root)
  180. goto out_no_root;
  181. return 0;
  182. out_no_root:
  183. printk("proc_read_super: get root inode failed\n");
  184. iput(root_inode);
  185. return -ENOMEM;
  186. }
  187. MODULE_LICENSE("GPL");