blob: dd28e86ab422fe23e06969cebf14c3677a6c8902 [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>
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070013#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/file.h>
15#include <linux/limits.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/smp_lock.h>
19
20#include <asm/system.h>
21#include <asm/uaccess.h>
22
Adrian Bunkfee781e2006-01-08 01:04:16 -080023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alexey Dobriyan76956502007-05-08 00:25:45 -070025struct proc_dir_entry *de_get(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 if (de)
28 atomic_inc(&de->count);
29 return de;
30}
31
32/*
33 * Decrements the use count and checks for deferred deletion.
34 */
Alexey Dobriyan76956502007-05-08 00:25:45 -070035void de_put(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 if (de) {
38 lock_kernel();
39 if (!atomic_read(&de->count)) {
40 printk("de_put: entry %s already free!\n", de->name);
41 unlock_kernel();
42 return;
43 }
44
45 if (atomic_dec_and_test(&de->count)) {
46 if (de->deleted) {
47 printk("de_put: deferred delete of %s\n",
48 de->name);
49 free_proc_entry(de);
50 }
51 }
52 unlock_kernel();
53 }
54}
55
56/*
57 * Decrement the use count of the proc_dir_entry.
58 */
59static void proc_delete_inode(struct inode *inode)
60{
61 struct proc_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mark Fashehfef26652005-09-09 13:01:31 -070063 truncate_inode_pages(&inode->i_data, 0);
64
Eric W. Biederman99f89552006-06-26 00:25:55 -070065 /* Stop tracking associated processes */
Eric W. Biederman13b41b02006-06-26 00:25:56 -070066 put_pid(PROC_I(inode)->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /* Let go of any associated proc directory entry */
69 de = PROC_I(inode)->pde;
70 if (de) {
71 if (de->owner)
72 module_put(de->owner);
73 de_put(de);
74 }
75 clear_inode(inode);
76}
77
78struct vfsmount *proc_mnt;
79
80static void proc_read_inode(struct inode * inode)
81{
82 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
83}
84
Christoph Lametere18b8902006-12-06 20:33:20 -080085static struct kmem_cache * proc_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static struct inode *proc_alloc_inode(struct super_block *sb)
88{
89 struct proc_inode *ei;
90 struct inode *inode;
91
Christoph Lametere94b1762006-12-06 20:33:17 -080092 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!ei)
94 return NULL;
Eric W. Biederman13b41b02006-06-26 00:25:56 -070095 ei->pid = NULL;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -070096 ei->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ei->op.proc_get_link = NULL;
98 ei->pde = NULL;
99 inode = &ei->vfs_inode;
100 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
101 return inode;
102}
103
104static void proc_destroy_inode(struct inode *inode)
105{
106 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
107}
108
Christoph Lametere18b8902006-12-06 20:33:20 -0800109static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct proc_inode *ei = (struct proc_inode *) foo;
112
Christoph Lametera35afb82007-05-16 22:10:57 -0700113 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
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
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700144static void pde_users_dec(struct proc_dir_entry *pde)
145{
146 spin_lock(&pde->pde_unload_lock);
147 pde->pde_users--;
148 if (pde->pde_unload_completion && pde->pde_users == 0)
149 complete(pde->pde_unload_completion);
150 spin_unlock(&pde->pde_unload_lock);
151}
152
153static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
154{
155 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
156 loff_t rv = -EINVAL;
157 loff_t (*llseek)(struct file *, loff_t, int);
158
159 spin_lock(&pde->pde_unload_lock);
160 /*
161 * remove_proc_entry() is going to delete PDE (as part of module
162 * cleanup sequence). No new callers into module allowed.
163 */
164 if (!pde->proc_fops) {
165 spin_unlock(&pde->pde_unload_lock);
166 return rv;
167 }
168 /*
169 * Bump refcount so that remove_proc_entry will wail for ->llseek to
170 * complete.
171 */
172 pde->pde_users++;
173 /*
174 * Save function pointer under lock, to protect against ->proc_fops
175 * NULL'ifying right after ->pde_unload_lock is dropped.
176 */
177 llseek = pde->proc_fops->llseek;
178 spin_unlock(&pde->pde_unload_lock);
179
180 if (!llseek)
181 llseek = default_llseek;
182 rv = llseek(file, offset, whence);
183
184 pde_users_dec(pde);
185 return rv;
186}
187
188static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
189{
190 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
191 ssize_t rv = -EIO;
192 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
193
194 spin_lock(&pde->pde_unload_lock);
195 if (!pde->proc_fops) {
196 spin_unlock(&pde->pde_unload_lock);
197 return rv;
198 }
199 pde->pde_users++;
200 read = pde->proc_fops->read;
201 spin_unlock(&pde->pde_unload_lock);
202
203 if (read)
204 rv = read(file, buf, count, ppos);
205
206 pde_users_dec(pde);
207 return rv;
208}
209
210static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
211{
212 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
213 ssize_t rv = -EIO;
214 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
215
216 spin_lock(&pde->pde_unload_lock);
217 if (!pde->proc_fops) {
218 spin_unlock(&pde->pde_unload_lock);
219 return rv;
220 }
221 pde->pde_users++;
222 write = pde->proc_fops->write;
223 spin_unlock(&pde->pde_unload_lock);
224
225 if (write)
226 rv = write(file, buf, count, ppos);
227
228 pde_users_dec(pde);
229 return rv;
230}
231
232static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
233{
234 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
235 unsigned int rv = 0;
236 unsigned int (*poll)(struct file *, struct poll_table_struct *);
237
238 spin_lock(&pde->pde_unload_lock);
239 if (!pde->proc_fops) {
240 spin_unlock(&pde->pde_unload_lock);
241 return rv;
242 }
243 pde->pde_users++;
244 poll = pde->proc_fops->poll;
245 spin_unlock(&pde->pde_unload_lock);
246
247 if (poll)
248 rv = poll(file, pts);
249
250 pde_users_dec(pde);
251 return rv;
252}
253
254static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
255{
256 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
257 long rv = -ENOTTY;
258 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
259 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
260
261 spin_lock(&pde->pde_unload_lock);
262 if (!pde->proc_fops) {
263 spin_unlock(&pde->pde_unload_lock);
264 return rv;
265 }
266 pde->pde_users++;
267 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
268 ioctl = pde->proc_fops->ioctl;
269 spin_unlock(&pde->pde_unload_lock);
270
271 if (unlocked_ioctl) {
272 rv = unlocked_ioctl(file, cmd, arg);
273 if (rv == -ENOIOCTLCMD)
274 rv = -EINVAL;
275 } else if (ioctl) {
276 lock_kernel();
277 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
278 unlock_kernel();
279 }
280
281 pde_users_dec(pde);
282 return rv;
283}
284
285#ifdef CONFIG_COMPAT
286static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
287{
288 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
289 long rv = -ENOTTY;
290 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
291
292 spin_lock(&pde->pde_unload_lock);
293 if (!pde->proc_fops) {
294 spin_unlock(&pde->pde_unload_lock);
295 return rv;
296 }
297 pde->pde_users++;
298 compat_ioctl = pde->proc_fops->compat_ioctl;
299 spin_unlock(&pde->pde_unload_lock);
300
301 if (compat_ioctl)
302 rv = compat_ioctl(file, cmd, arg);
303
304 pde_users_dec(pde);
305 return rv;
306}
307#endif
308
309static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
310{
311 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
312 int rv = -EIO;
313 int (*mmap)(struct file *, struct vm_area_struct *);
314
315 spin_lock(&pde->pde_unload_lock);
316 if (!pde->proc_fops) {
317 spin_unlock(&pde->pde_unload_lock);
318 return rv;
319 }
320 pde->pde_users++;
321 mmap = pde->proc_fops->mmap;
322 spin_unlock(&pde->pde_unload_lock);
323
324 if (mmap)
325 rv = mmap(file, vma);
326
327 pde_users_dec(pde);
328 return rv;
329}
330
331static int proc_reg_open(struct inode *inode, struct file *file)
332{
333 struct proc_dir_entry *pde = PDE(inode);
334 int rv = 0;
335 int (*open)(struct inode *, struct file *);
336
337 spin_lock(&pde->pde_unload_lock);
338 if (!pde->proc_fops) {
339 spin_unlock(&pde->pde_unload_lock);
340 return rv;
341 }
342 pde->pde_users++;
343 open = pde->proc_fops->open;
344 spin_unlock(&pde->pde_unload_lock);
345
346 if (open)
347 rv = open(inode, file);
348
349 pde_users_dec(pde);
350 return rv;
351}
352
353static int proc_reg_release(struct inode *inode, struct file *file)
354{
355 struct proc_dir_entry *pde = PDE(inode);
356 int rv = 0;
357 int (*release)(struct inode *, struct file *);
358
359 spin_lock(&pde->pde_unload_lock);
360 if (!pde->proc_fops) {
361 spin_unlock(&pde->pde_unload_lock);
362 return rv;
363 }
364 pde->pde_users++;
365 release = pde->proc_fops->release;
366 spin_unlock(&pde->pde_unload_lock);
367
368 if (release)
369 rv = release(inode, file);
370
371 pde_users_dec(pde);
372 return rv;
373}
374
375static const struct file_operations proc_reg_file_ops = {
376 .llseek = proc_reg_llseek,
377 .read = proc_reg_read,
378 .write = proc_reg_write,
379 .poll = proc_reg_poll,
380 .unlocked_ioctl = proc_reg_unlocked_ioctl,
381#ifdef CONFIG_COMPAT
382 .compat_ioctl = proc_reg_compat_ioctl,
383#endif
384 .mmap = proc_reg_mmap,
385 .open = proc_reg_open,
386 .release = proc_reg_release,
387};
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
390 struct proc_dir_entry *de)
391{
392 struct inode * inode;
393
Kirill Korotaeve9543652005-10-30 15:02:26 -0800394 if (de != NULL && !try_module_get(de->owner))
395 goto out_mod;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 inode = iget(sb, ino);
398 if (!inode)
Kirill Korotaeve9543652005-10-30 15:02:26 -0800399 goto out_ino;
400
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800401 PROC_I(inode)->fd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 PROC_I(inode)->pde = de;
403 if (de) {
404 if (de->mode) {
405 inode->i_mode = de->mode;
406 inode->i_uid = de->uid;
407 inode->i_gid = de->gid;
408 }
409 if (de->size)
410 inode->i_size = de->size;
411 if (de->nlink)
412 inode->i_nlink = de->nlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (de->proc_iops)
414 inode->i_op = de->proc_iops;
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700415 if (de->proc_fops) {
416 if (S_ISREG(inode->i_mode))
417 inode->i_fop = &proc_reg_file_ops;
418 else
419 inode->i_fop = de->proc_fops;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return inode;
424
Kirill Korotaeve9543652005-10-30 15:02:26 -0800425out_ino:
426 if (de != NULL)
427 module_put(de->owner);
428out_mod:
Kirill Korotaeve9543652005-10-30 15:02:26 -0800429 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432int proc_fill_super(struct super_block *s, void *data, int silent)
433{
434 struct inode * root_inode;
435
Linus Torvalds92d03282006-07-15 12:20:05 -0700436 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 s->s_blocksize = 1024;
438 s->s_blocksize_bits = 10;
439 s->s_magic = PROC_SUPER_MAGIC;
440 s->s_op = &proc_sops;
441 s->s_time_gran = 1;
442
Alexey Dobriyan76956502007-05-08 00:25:45 -0700443 de_get(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
445 if (!root_inode)
446 goto out_no_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 root_inode->i_uid = 0;
448 root_inode->i_gid = 0;
449 s->s_root = d_alloc_root(root_inode);
450 if (!s->s_root)
451 goto out_no_root;
452 return 0;
453
454out_no_root:
455 printk("proc_read_super: get root inode failed\n");
456 iput(root_inode);
Alexey Dobriyan76956502007-05-08 00:25:45 -0700457 de_put(&proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -ENOMEM;
459}
460MODULE_LICENSE("GPL");