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/smp_lock.h>
20 #include <linux/sysctl.h>
21 #include <linux/slab.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
28 static void proc_evict_inode(struct inode *inode)
30 struct proc_dir_entry *de;
32 truncate_inode_pages(&inode->i_data, 0);
35 /* Stop tracking associated processes */
36 put_pid(PROC_I(inode)->pid);
38 /* Let go of any associated proc directory entry */
39 de = PROC_I(inode)->pde;
42 if (PROC_I(inode)->sysctl)
43 sysctl_head_put(PROC_I(inode)->sysctl);
46 struct vfsmount *proc_mnt;
48 static struct kmem_cache * proc_inode_cachep;
50 static struct inode *proc_alloc_inode(struct super_block *sb)
52 struct proc_inode *ei;
55 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
60 ei->op.proc_get_link = NULL;
63 ei->sysctl_entry = NULL;
64 inode = &ei->vfs_inode;
65 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
69 static void proc_destroy_inode(struct inode *inode)
71 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
74 static void init_once(void *foo)
76 struct proc_inode *ei = (struct proc_inode *) foo;
78 inode_init_once(&ei->vfs_inode);
81 void __init proc_init_inodecache(void)
83 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
84 sizeof(struct proc_inode),
85 0, (SLAB_RECLAIM_ACCOUNT|
86 SLAB_MEM_SPREAD|SLAB_PANIC),
90 static const struct super_operations proc_sops = {
91 .alloc_inode = proc_alloc_inode,
92 .destroy_inode = proc_destroy_inode,
93 .drop_inode = generic_delete_inode,
94 .evict_inode = proc_evict_inode,
95 .statfs = simple_statfs,
98 static void __pde_users_dec(struct proc_dir_entry *pde)
101 if (pde->pde_unload_completion && pde->pde_users == 0)
102 complete(pde->pde_unload_completion);
105 void pde_users_dec(struct proc_dir_entry *pde)
107 spin_lock(&pde->pde_unload_lock);
108 __pde_users_dec(pde);
109 spin_unlock(&pde->pde_unload_lock);
112 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
114 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
116 loff_t (*llseek)(struct file *, loff_t, int);
118 spin_lock(&pde->pde_unload_lock);
120 * remove_proc_entry() is going to delete PDE (as part of module
121 * cleanup sequence). No new callers into module allowed.
123 if (!pde->proc_fops) {
124 spin_unlock(&pde->pde_unload_lock);
128 * Bump refcount so that remove_proc_entry will wail for ->llseek to
133 * Save function pointer under lock, to protect against ->proc_fops
134 * NULL'ifying right after ->pde_unload_lock is dropped.
136 llseek = pde->proc_fops->llseek;
137 spin_unlock(&pde->pde_unload_lock);
140 llseek = default_llseek;
141 rv = llseek(file, offset, whence);
147 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
149 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
151 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
153 spin_lock(&pde->pde_unload_lock);
154 if (!pde->proc_fops) {
155 spin_unlock(&pde->pde_unload_lock);
159 read = pde->proc_fops->read;
160 spin_unlock(&pde->pde_unload_lock);
163 rv = read(file, buf, count, ppos);
169 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
171 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
173 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
175 spin_lock(&pde->pde_unload_lock);
176 if (!pde->proc_fops) {
177 spin_unlock(&pde->pde_unload_lock);
181 write = pde->proc_fops->write;
182 spin_unlock(&pde->pde_unload_lock);
185 rv = write(file, buf, count, ppos);
191 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
193 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
194 unsigned int rv = DEFAULT_POLLMASK;
195 unsigned int (*poll)(struct file *, struct poll_table_struct *);
197 spin_lock(&pde->pde_unload_lock);
198 if (!pde->proc_fops) {
199 spin_unlock(&pde->pde_unload_lock);
203 poll = pde->proc_fops->poll;
204 spin_unlock(&pde->pde_unload_lock);
207 rv = poll(file, pts);
213 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
215 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
217 long (*ioctl)(struct file *, unsigned int, unsigned long);
219 spin_lock(&pde->pde_unload_lock);
220 if (!pde->proc_fops) {
221 spin_unlock(&pde->pde_unload_lock);
225 ioctl = pde->proc_fops->unlocked_ioctl;
226 spin_unlock(&pde->pde_unload_lock);
229 rv = ioctl(file, cmd, arg);
236 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
238 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
240 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
242 spin_lock(&pde->pde_unload_lock);
243 if (!pde->proc_fops) {
244 spin_unlock(&pde->pde_unload_lock);
248 compat_ioctl = pde->proc_fops->compat_ioctl;
249 spin_unlock(&pde->pde_unload_lock);
252 rv = compat_ioctl(file, cmd, arg);
259 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
261 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
263 int (*mmap)(struct file *, struct vm_area_struct *);
265 spin_lock(&pde->pde_unload_lock);
266 if (!pde->proc_fops) {
267 spin_unlock(&pde->pde_unload_lock);
271 mmap = pde->proc_fops->mmap;
272 spin_unlock(&pde->pde_unload_lock);
275 rv = mmap(file, vma);
281 static int proc_reg_open(struct inode *inode, struct file *file)
283 struct proc_dir_entry *pde = PDE(inode);
285 int (*open)(struct inode *, struct file *);
286 int (*release)(struct inode *, struct file *);
287 struct pde_opener *pdeo;
290 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
291 * sequence. ->release won't be called because ->proc_fops will be
292 * cleared. Depending on complexity of ->release, consequences vary.
294 * We can't wait for mercy when close will be done for real, it's
295 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
296 * by hand in remove_proc_entry(). For this, save opener's credentials
299 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
303 spin_lock(&pde->pde_unload_lock);
304 if (!pde->proc_fops) {
305 spin_unlock(&pde->pde_unload_lock);
310 open = pde->proc_fops->open;
311 release = pde->proc_fops->release;
312 spin_unlock(&pde->pde_unload_lock);
315 rv = open(inode, file);
317 spin_lock(&pde->pde_unload_lock);
318 if (rv == 0 && release) {
319 /* To know what to release. */
322 /* Strictly for "too late" ->release in proc_reg_release(). */
323 pdeo->release = release;
324 list_add(&pdeo->lh, &pde->pde_openers);
327 __pde_users_dec(pde);
328 spin_unlock(&pde->pde_unload_lock);
332 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
333 struct inode *inode, struct file *file)
335 struct pde_opener *pdeo;
337 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
338 if (pdeo->inode == inode && pdeo->file == file)
344 static int proc_reg_release(struct inode *inode, struct file *file)
346 struct proc_dir_entry *pde = PDE(inode);
348 int (*release)(struct inode *, struct file *);
349 struct pde_opener *pdeo;
351 spin_lock(&pde->pde_unload_lock);
352 pdeo = find_pde_opener(pde, inode, file);
353 if (!pde->proc_fops) {
355 * Can't simply exit, __fput() will think that everything is OK,
356 * and move on to freeing struct file. remove_proc_entry() will
357 * find slacker in opener's list and will try to do non-trivial
358 * things with struct file. Therefore, remove opener from list.
360 * But if opener is removed from list, who will ->release it?
364 spin_unlock(&pde->pde_unload_lock);
365 rv = pdeo->release(inode, file);
368 spin_unlock(&pde->pde_unload_lock);
372 release = pde->proc_fops->release;
377 spin_unlock(&pde->pde_unload_lock);
380 rv = release(inode, file);
386 static const struct file_operations proc_reg_file_ops = {
387 .llseek = proc_reg_llseek,
388 .read = proc_reg_read,
389 .write = proc_reg_write,
390 .poll = proc_reg_poll,
391 .unlocked_ioctl = proc_reg_unlocked_ioctl,
393 .compat_ioctl = proc_reg_compat_ioctl,
395 .mmap = proc_reg_mmap,
396 .open = proc_reg_open,
397 .release = proc_reg_release,
401 static const struct file_operations proc_reg_file_ops_no_compat = {
402 .llseek = proc_reg_llseek,
403 .read = proc_reg_read,
404 .write = proc_reg_write,
405 .poll = proc_reg_poll,
406 .unlocked_ioctl = proc_reg_unlocked_ioctl,
407 .mmap = proc_reg_mmap,
408 .open = proc_reg_open,
409 .release = proc_reg_release,
413 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
414 struct proc_dir_entry *de)
416 struct inode * inode;
418 inode = iget_locked(sb, ino);
421 if (inode->i_state & I_NEW) {
422 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
423 PROC_I(inode)->fd = 0;
424 PROC_I(inode)->pde = de;
427 inode->i_mode = de->mode;
428 inode->i_uid = de->uid;
429 inode->i_gid = de->gid;
432 inode->i_size = de->size;
434 inode->i_nlink = de->nlink;
436 inode->i_op = de->proc_iops;
438 if (S_ISREG(inode->i_mode)) {
440 if (!de->proc_fops->compat_ioctl)
442 &proc_reg_file_ops_no_compat;
445 inode->i_fop = &proc_reg_file_ops;
447 inode->i_fop = de->proc_fops;
450 unlock_new_inode(inode);
456 int proc_fill_super(struct super_block *s)
458 struct inode * root_inode;
460 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
461 s->s_blocksize = 1024;
462 s->s_blocksize_bits = 10;
463 s->s_magic = PROC_SUPER_MAGIC;
464 s->s_op = &proc_sops;
468 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
471 root_inode->i_uid = 0;
472 root_inode->i_gid = 0;
473 s->s_root = d_alloc_root(root_inode);
479 printk("proc_read_super: get root inode failed\n");