inode.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. truncate_inode_pages(&inode->i_data, 0);
  55. /* Stop tracking associated processes */
  56. put_pid(PROC_I(inode)->pid);
  57. /* Let go of any associated proc directory entry */
  58. de = PROC_I(inode)->pde;
  59. if (de) {
  60. if (de->owner)
  61. module_put(de->owner);
  62. de_put(de);
  63. }
  64. clear_inode(inode);
  65. }
  66. struct vfsmount *proc_mnt;
  67. static void proc_read_inode(struct inode * inode)
  68. {
  69. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  70. }
  71. static struct kmem_cache * proc_inode_cachep;
  72. static struct inode *proc_alloc_inode(struct super_block *sb)
  73. {
  74. struct proc_inode *ei;
  75. struct inode *inode;
  76. ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  77. if (!ei)
  78. return NULL;
  79. ei->pid = NULL;
  80. ei->fd = 0;
  81. ei->op.proc_get_link = NULL;
  82. ei->pde = NULL;
  83. inode = &ei->vfs_inode;
  84. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  85. return inode;
  86. }
  87. static void proc_destroy_inode(struct inode *inode)
  88. {
  89. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  90. }
  91. static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  92. {
  93. struct proc_inode *ei = (struct proc_inode *) foo;
  94. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  95. SLAB_CTOR_CONSTRUCTOR)
  96. inode_init_once(&ei->vfs_inode);
  97. }
  98. int __init proc_init_inodecache(void)
  99. {
  100. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  101. sizeof(struct proc_inode),
  102. 0, (SLAB_RECLAIM_ACCOUNT|
  103. SLAB_MEM_SPREAD),
  104. init_once, NULL);
  105. if (proc_inode_cachep == NULL)
  106. return -ENOMEM;
  107. return 0;
  108. }
  109. static int proc_remount(struct super_block *sb, int *flags, char *data)
  110. {
  111. *flags |= MS_NODIRATIME;
  112. return 0;
  113. }
  114. static const struct super_operations proc_sops = {
  115. .alloc_inode = proc_alloc_inode,
  116. .destroy_inode = proc_destroy_inode,
  117. .read_inode = proc_read_inode,
  118. .drop_inode = generic_delete_inode,
  119. .delete_inode = proc_delete_inode,
  120. .statfs = simple_statfs,
  121. .remount_fs = proc_remount,
  122. };
  123. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  124. struct proc_dir_entry *de)
  125. {
  126. struct inode * inode;
  127. /*
  128. * Increment the use count so the dir entry can't disappear.
  129. */
  130. de_get(de);
  131. WARN_ON(de && de->deleted);
  132. if (de != NULL && !try_module_get(de->owner))
  133. goto out_mod;
  134. inode = iget(sb, ino);
  135. if (!inode)
  136. goto out_ino;
  137. PROC_I(inode)->fd = 0;
  138. PROC_I(inode)->pde = de;
  139. if (de) {
  140. if (de->mode) {
  141. inode->i_mode = de->mode;
  142. inode->i_uid = de->uid;
  143. inode->i_gid = de->gid;
  144. }
  145. if (de->size)
  146. inode->i_size = de->size;
  147. if (de->nlink)
  148. inode->i_nlink = de->nlink;
  149. if (de->proc_iops)
  150. inode->i_op = de->proc_iops;
  151. if (de->proc_fops)
  152. inode->i_fop = de->proc_fops;
  153. }
  154. return inode;
  155. out_ino:
  156. if (de != NULL)
  157. module_put(de->owner);
  158. out_mod:
  159. de_put(de);
  160. return NULL;
  161. }
  162. int proc_fill_super(struct super_block *s, void *data, int silent)
  163. {
  164. struct inode * root_inode;
  165. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  166. s->s_blocksize = 1024;
  167. s->s_blocksize_bits = 10;
  168. s->s_magic = PROC_SUPER_MAGIC;
  169. s->s_op = &proc_sops;
  170. s->s_time_gran = 1;
  171. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  172. if (!root_inode)
  173. goto out_no_root;
  174. root_inode->i_uid = 0;
  175. root_inode->i_gid = 0;
  176. s->s_root = d_alloc_root(root_inode);
  177. if (!s->s_root)
  178. goto out_no_root;
  179. return 0;
  180. out_no_root:
  181. printk("proc_read_super: get root inode failed\n");
  182. iput(root_inode);
  183. return -ENOMEM;
  184. }
  185. MODULE_LICENSE("GPL");