blob: 22b1158389aed97d50044b64ac2b1383706d7923 [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
Adrian Bunkfee781e2006-01-08 01:04:16 -080022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Mark Fashehfef26652005-09-09 13:01:31 -070062 truncate_inode_pages(&inode->i_data, 0);
63
Eric W. Biederman99f89552006-06-26 00:25:55 -070064 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070065 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /* Let go of any associated proc directory entry */
68 de = PROC_I(inode)->pde;
69 if (de) {
70 if (de->owner)
71 module_put(de->owner);
72 de_put(de);
73 }
74 clear_inode(inode);
75}
76
77struct vfsmount *proc_mnt;
78
79static void proc_read_inode(struct inode * inode)
80{
81 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
82}
83
Christoph Lametere18b8902006-12-06 20:33:20 -080084static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static struct inode *proc_alloc_inode(struct super_block *sb)
87{
88 struct proc_inode *ei;
89 struct inode *inode;
90
Christoph Lametere94b1762006-12-06 20:33:17 -080091 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!ei)
93 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070094 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070095 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ei->op.proc_get_link = NULL;
97 ei->pde = NULL;
98 inode = &ei->vfs_inode;
99 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
100 return inode;
101}
102
103static void proc_destroy_inode(struct inode *inode)
104{
105 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
106}
107
Christoph Lametere18b8902006-12-06 20:33:20 -0800108static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct proc_inode *ei = (struct proc_inode *) foo;
111
Christoph Lameter50953fe2007-05-06 14:50:16 -0700112 if (flags & SLAB_CTOR_CONSTRUCTOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 inode_init_once(&ei->vfs_inode);
114}
115
116int __init proc_init_inodecache(void)
117{
118 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
119 sizeof(struct proc_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800120 0, (SLAB_RECLAIM_ACCOUNT|
121 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 init_once, NULL);
123 if (proc_inode_cachep == NULL)
124 return -ENOMEM;
125 return 0;
126}
127
128static int proc_remount(struct super_block *sb, int *flags, char *data)
129{
130 *flags |= MS_NODIRATIME;
131 return 0;
132}
133
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800134static const struct super_operations proc_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .alloc_inode = proc_alloc_inode,
136 .destroy_inode = proc_destroy_inode,
137 .read_inode = proc_read_inode,
138 .drop_inode = generic_delete_inode,
139 .delete_inode = proc_delete_inode,
140 .statfs = simple_statfs,
141 .remount_fs = proc_remount,
142};
143
144struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
145 struct proc_dir_entry *de)
146{
147 struct inode * inode;
148
149 /*
150 * Increment the use count so the dir entry can't disappear.
151 */
152 de_get(de);
153
154 WARN_ON(de && de->deleted);
155
Kirill Korotaeve9543652005-10-30 15:02:26 -0800156 if (de != NULL && !try_module_get(de->owner))
157 goto out_mod;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 inode = iget(sb, ino);
160 if (!inode)
Kirill Korotaeve9543652005-10-30 15:02:26 -0800161 goto out_ino;
162
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800163 PROC_I(inode)->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 PROC_I(inode)->pde = de;
165 if (de) {
166 if (de->mode) {
167 inode->i_mode = de->mode;
168 inode->i_uid = de->uid;
169 inode->i_gid = de->gid;
170 }
171 if (de->size)
172 inode->i_size = de->size;
173 if (de->nlink)
174 inode->i_nlink = de->nlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (de->proc_iops)
176 inode->i_op = de->proc_iops;
177 if (de->proc_fops)
178 inode->i_fop = de->proc_fops;
179 }
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return inode;
182
Kirill Korotaeve9543652005-10-30 15:02:26 -0800183out_ino:
184 if (de != NULL)
185 module_put(de->owner);
186out_mod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 de_put(de);
Kirill Korotaeve9543652005-10-30 15:02:26 -0800188 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191int proc_fill_super(struct super_block *s, void *data, int silent)
192{
193 struct inode * root_inode;
194
Linus Torvalds92d03282006-07-15 12:20:05 -0700195 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 s->s_blocksize = 1024;
197 s->s_blocksize_bits = 10;
198 s->s_magic = PROC_SUPER_MAGIC;
199 s->s_op = &proc_sops;
200 s->s_time_gran = 1;
201
202 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
203 if (!root_inode)
204 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 root_inode->i_uid = 0;
206 root_inode->i_gid = 0;
207 s->s_root = d_alloc_root(root_inode);
208 if (!s->s_root)
209 goto out_no_root;
210 return 0;
211
212out_no_root:
213 printk("proc_read_super: get root inode failed\n");
214 iput(root_inode);
215 return -ENOMEM;
216}
217MODULE_LICENSE("GPL");