inode.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. 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. 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_CONSTRUCTOR)
  95. inode_init_once(&ei->vfs_inode);
  96. }
  97. int __init proc_init_inodecache(void)
  98. {
  99. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  100. sizeof(struct proc_inode),
  101. 0, (SLAB_RECLAIM_ACCOUNT|
  102. SLAB_MEM_SPREAD),
  103. init_once, NULL);
  104. if (proc_inode_cachep == NULL)
  105. return -ENOMEM;
  106. return 0;
  107. }
  108. static int proc_remount(struct super_block *sb, int *flags, char *data)
  109. {
  110. *flags |= MS_NODIRATIME;
  111. return 0;
  112. }
  113. static const struct super_operations proc_sops = {
  114. .alloc_inode = proc_alloc_inode,
  115. .destroy_inode = proc_destroy_inode,
  116. .read_inode = proc_read_inode,
  117. .drop_inode = generic_delete_inode,
  118. .delete_inode = proc_delete_inode,
  119. .statfs = simple_statfs,
  120. .remount_fs = proc_remount,
  121. };
  122. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  123. struct proc_dir_entry *de)
  124. {
  125. struct inode * inode;
  126. if (de != NULL && !try_module_get(de->owner))
  127. goto out_mod;
  128. inode = iget(sb, ino);
  129. if (!inode)
  130. goto out_ino;
  131. PROC_I(inode)->fd = 0;
  132. PROC_I(inode)->pde = de;
  133. if (de) {
  134. if (de->mode) {
  135. inode->i_mode = de->mode;
  136. inode->i_uid = de->uid;
  137. inode->i_gid = de->gid;
  138. }
  139. if (de->size)
  140. inode->i_size = de->size;
  141. if (de->nlink)
  142. inode->i_nlink = de->nlink;
  143. if (de->proc_iops)
  144. inode->i_op = de->proc_iops;
  145. if (de->proc_fops)
  146. inode->i_fop = de->proc_fops;
  147. }
  148. return inode;
  149. out_ino:
  150. if (de != NULL)
  151. module_put(de->owner);
  152. out_mod:
  153. return NULL;
  154. }
  155. int proc_fill_super(struct super_block *s, void *data, int silent)
  156. {
  157. struct inode * root_inode;
  158. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  159. s->s_blocksize = 1024;
  160. s->s_blocksize_bits = 10;
  161. s->s_magic = PROC_SUPER_MAGIC;
  162. s->s_op = &proc_sops;
  163. s->s_time_gran = 1;
  164. de_get(&proc_root);
  165. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  166. if (!root_inode)
  167. goto out_no_root;
  168. root_inode->i_uid = 0;
  169. root_inode->i_gid = 0;
  170. s->s_root = d_alloc_root(root_inode);
  171. if (!s->s_root)
  172. goto out_no_root;
  173. return 0;
  174. out_no_root:
  175. printk("proc_read_super: get root inode failed\n");
  176. iput(root_inode);
  177. de_put(&proc_root);
  178. return -ENOMEM;
  179. }
  180. MODULE_LICENSE("GPL");