2 * linux/fs/proc/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/time.h>
8 #include <linux/proc_fs.h>
9 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/stat.h>
13 #include <linux/completion.h>
14 #include <linux/poll.h>
15 #include <linux/file.h>
16 #include <linux/limits.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/sysctl.h>
20 #include <linux/slab.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
27 static void proc_evict_inode(struct inode *inode)
29 struct proc_dir_entry *de;
31 truncate_inode_pages(&inode->i_data, 0);
34 /* Stop tracking associated processes */
35 put_pid(PROC_I(inode)->pid);
37 /* Let go of any associated proc directory entry */
38 de = PROC_I(inode)->pde;
41 if (PROC_I(inode)->sysctl)
42 sysctl_head_put(PROC_I(inode)->sysctl);
45 struct vfsmount *proc_mnt;
47 static struct kmem_cache * proc_inode_cachep;
49 static struct inode *proc_alloc_inode(struct super_block *sb)
51 struct proc_inode *ei;
54 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
59 ei->op.proc_get_link = NULL;
62 ei->sysctl_entry = NULL;
63 inode = &ei->vfs_inode;
64 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
68 static void proc_destroy_inode(struct inode *inode)
70 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
73 static void init_once(void *foo)
75 struct proc_inode *ei = (struct proc_inode *) foo;
77 inode_init_once(&ei->vfs_inode);
80 void __init proc_init_inodecache(void)
82 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
83 sizeof(struct proc_inode),
84 0, (SLAB_RECLAIM_ACCOUNT|
85 SLAB_MEM_SPREAD|SLAB_PANIC),
89 static const struct super_operations proc_sops = {
90 .alloc_inode = proc_alloc_inode,
91 .destroy_inode = proc_destroy_inode,
92 .drop_inode = generic_delete_inode,
93 .evict_inode = proc_evict_inode,
94 .statfs = simple_statfs,
97 static void __pde_users_dec(struct proc_dir_entry *pde)
100 if (pde->pde_unload_completion && pde->pde_users == 0)
101 complete(pde->pde_unload_completion);
104 void pde_users_dec(struct proc_dir_entry *pde)
106 spin_lock(&pde->pde_unload_lock);
107 __pde_users_dec(pde);
108 spin_unlock(&pde->pde_unload_lock);
111 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
113 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
115 loff_t (*llseek)(struct file *, loff_t, int);
117 spin_lock(&pde->pde_unload_lock);
119 * remove_proc_entry() is going to delete PDE (as part of module
120 * cleanup sequence). No new callers into module allowed.
122 if (!pde->proc_fops) {
123 spin_unlock(&pde->pde_unload_lock);
127 * Bump refcount so that remove_proc_entry will wail for ->llseek to
132 * Save function pointer under lock, to protect against ->proc_fops
133 * NULL'ifying right after ->pde_unload_lock is dropped.
135 llseek = pde->proc_fops->llseek;
136 spin_unlock(&pde->pde_unload_lock);
139 llseek = default_llseek;
140 rv = llseek(file, offset, whence);
146 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
148 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
150 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
152 spin_lock(&pde->pde_unload_lock);
153 if (!pde->proc_fops) {
154 spin_unlock(&pde->pde_unload_lock);
158 read = pde->proc_fops->read;
159 spin_unlock(&pde->pde_unload_lock);
162 rv = read(file, buf, count, ppos);
168 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
170 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
172 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
174 spin_lock(&pde->pde_unload_lock);
175 if (!pde->proc_fops) {
176 spin_unlock(&pde->pde_unload_lock);
180 write = pde->proc_fops->write;
181 spin_unlock(&pde->pde_unload_lock);
184 rv = write(file, buf, count, ppos);
190 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
192 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
193 unsigned int rv = DEFAULT_POLLMASK;
194 unsigned int (*poll)(struct file *, struct poll_table_struct *);
196 spin_lock(&pde->pde_unload_lock);
197 if (!pde->proc_fops) {
198 spin_unlock(&pde->pde_unload_lock);
202 poll = pde->proc_fops->poll;
203 spin_unlock(&pde->pde_unload_lock);
206 rv = poll(file, pts);
212 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
214 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
216 long (*ioctl)(struct file *, unsigned int, unsigned long);
218 spin_lock(&pde->pde_unload_lock);
219 if (!pde->proc_fops) {
220 spin_unlock(&pde->pde_unload_lock);
224 ioctl = pde->proc_fops->unlocked_ioctl;
225 spin_unlock(&pde->pde_unload_lock);
228 rv = ioctl(file, cmd, arg);
235 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
237 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
239 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
241 spin_lock(&pde->pde_unload_lock);
242 if (!pde->proc_fops) {
243 spin_unlock(&pde->pde_unload_lock);
247 compat_ioctl = pde->proc_fops->compat_ioctl;
248 spin_unlock(&pde->pde_unload_lock);
251 rv = compat_ioctl(file, cmd, arg);
258 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
260 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
262 int (*mmap)(struct file *, struct vm_area_struct *);
264 spin_lock(&pde->pde_unload_lock);
265 if (!pde->proc_fops) {
266 spin_unlock(&pde->pde_unload_lock);
270 mmap = pde->proc_fops->mmap;
271 spin_unlock(&pde->pde_unload_lock);
274 rv = mmap(file, vma);
280 static int proc_reg_open(struct inode *inode, struct file *file)
282 struct proc_dir_entry *pde = PDE(inode);
284 int (*open)(struct inode *, struct file *);
285 int (*release)(struct inode *, struct file *);
286 struct pde_opener *pdeo;
289 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
290 * sequence. ->release won't be called because ->proc_fops will be
291 * cleared. Depending on complexity of ->release, consequences vary.
293 * We can't wait for mercy when close will be done for real, it's
294 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
295 * by hand in remove_proc_entry(). For this, save opener's credentials
298 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
302 spin_lock(&pde->pde_unload_lock);
303 if (!pde->proc_fops) {
304 spin_unlock(&pde->pde_unload_lock);
309 open = pde->proc_fops->open;
310 release = pde->proc_fops->release;
311 spin_unlock(&pde->pde_unload_lock);
314 rv = open(inode, file);
316 spin_lock(&pde->pde_unload_lock);
317 if (rv == 0 && release) {
318 /* To know what to release. */
321 /* Strictly for "too late" ->release in proc_reg_release(). */
322 pdeo->release = release;
323 list_add(&pdeo->lh, &pde->pde_openers);
326 __pde_users_dec(pde);
327 spin_unlock(&pde->pde_unload_lock);
331 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
332 struct inode *inode, struct file *file)
334 struct pde_opener *pdeo;
336 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
337 if (pdeo->inode == inode && pdeo->file == file)
343 static int proc_reg_release(struct inode *inode, struct file *file)
345 struct proc_dir_entry *pde = PDE(inode);
347 int (*release)(struct inode *, struct file *);
348 struct pde_opener *pdeo;
350 spin_lock(&pde->pde_unload_lock);
351 pdeo = find_pde_opener(pde, inode, file);
352 if (!pde->proc_fops) {
354 * Can't simply exit, __fput() will think that everything is OK,
355 * and move on to freeing struct file. remove_proc_entry() will
356 * find slacker in opener's list and will try to do non-trivial
357 * things with struct file. Therefore, remove opener from list.
359 * But if opener is removed from list, who will ->release it?
363 spin_unlock(&pde->pde_unload_lock);
364 rv = pdeo->release(inode, file);
367 spin_unlock(&pde->pde_unload_lock);
371 release = pde->proc_fops->release;
376 spin_unlock(&pde->pde_unload_lock);
379 rv = release(inode, file);
385 static const struct file_operations proc_reg_file_ops = {
386 .llseek = proc_reg_llseek,
387 .read = proc_reg_read,
388 .write = proc_reg_write,
389 .poll = proc_reg_poll,
390 .unlocked_ioctl = proc_reg_unlocked_ioctl,
392 .compat_ioctl = proc_reg_compat_ioctl,
394 .mmap = proc_reg_mmap,
395 .open = proc_reg_open,
396 .release = proc_reg_release,
400 static const struct file_operations proc_reg_file_ops_no_compat = {
401 .llseek = proc_reg_llseek,
402 .read = proc_reg_read,
403 .write = proc_reg_write,
404 .poll = proc_reg_poll,
405 .unlocked_ioctl = proc_reg_unlocked_ioctl,
406 .mmap = proc_reg_mmap,
407 .open = proc_reg_open,
408 .release = proc_reg_release,
412 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
413 struct proc_dir_entry *de)
415 struct inode * inode;
417 inode = iget_locked(sb, ino);
420 if (inode->i_state & I_NEW) {
421 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
422 PROC_I(inode)->fd = 0;
423 PROC_I(inode)->pde = de;
426 inode->i_mode = de->mode;
427 inode->i_uid = de->uid;
428 inode->i_gid = de->gid;
431 inode->i_size = de->size;
433 inode->i_nlink = de->nlink;
435 inode->i_op = de->proc_iops;
437 if (S_ISREG(inode->i_mode)) {
439 if (!de->proc_fops->compat_ioctl)
441 &proc_reg_file_ops_no_compat;
444 inode->i_fop = &proc_reg_file_ops;
446 inode->i_fop = de->proc_fops;
449 unlock_new_inode(inode);
455 int proc_fill_super(struct super_block *s)
457 struct inode * root_inode;
459 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
460 s->s_blocksize = 1024;
461 s->s_blocksize_bits = 10;
462 s->s_magic = PROC_SUPER_MAGIC;
463 s->s_op = &proc_sops;
467 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
470 root_inode->i_uid = 0;
471 root_inode->i_gid = 0;
472 s->s_root = d_alloc_root(root_inode);
478 printk("proc_read_super: get root inode failed\n");