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