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_i_callback(struct rcu_head *head)
70 struct inode *inode = container_of(head, struct inode, i_rcu);
71 INIT_LIST_HEAD(&inode->i_dentry);
72 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
75 static void proc_destroy_inode(struct inode *inode)
77 call_rcu(&inode->i_rcu, proc_i_callback);
80 static void init_once(void *foo)
82 struct proc_inode *ei = (struct proc_inode *) foo;
84 inode_init_once(&ei->vfs_inode);
87 void __init proc_init_inodecache(void)
89 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
90 sizeof(struct proc_inode),
91 0, (SLAB_RECLAIM_ACCOUNT|
92 SLAB_MEM_SPREAD|SLAB_PANIC),
96 static const struct super_operations proc_sops = {
97 .alloc_inode = proc_alloc_inode,
98 .destroy_inode = proc_destroy_inode,
99 .drop_inode = generic_delete_inode,
100 .evict_inode = proc_evict_inode,
101 .statfs = simple_statfs,
104 static void __pde_users_dec(struct proc_dir_entry *pde)
107 if (pde->pde_unload_completion && pde->pde_users == 0)
108 complete(pde->pde_unload_completion);
111 void pde_users_dec(struct proc_dir_entry *pde)
113 spin_lock(&pde->pde_unload_lock);
114 __pde_users_dec(pde);
115 spin_unlock(&pde->pde_unload_lock);
118 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
120 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
122 loff_t (*llseek)(struct file *, loff_t, int);
124 spin_lock(&pde->pde_unload_lock);
126 * remove_proc_entry() is going to delete PDE (as part of module
127 * cleanup sequence). No new callers into module allowed.
129 if (!pde->proc_fops) {
130 spin_unlock(&pde->pde_unload_lock);
134 * Bump refcount so that remove_proc_entry will wail for ->llseek to
139 * Save function pointer under lock, to protect against ->proc_fops
140 * NULL'ifying right after ->pde_unload_lock is dropped.
142 llseek = pde->proc_fops->llseek;
143 spin_unlock(&pde->pde_unload_lock);
146 llseek = default_llseek;
147 rv = llseek(file, offset, whence);
153 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
155 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
157 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
159 spin_lock(&pde->pde_unload_lock);
160 if (!pde->proc_fops) {
161 spin_unlock(&pde->pde_unload_lock);
165 read = pde->proc_fops->read;
166 spin_unlock(&pde->pde_unload_lock);
169 rv = read(file, buf, count, ppos);
175 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
177 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
179 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
181 spin_lock(&pde->pde_unload_lock);
182 if (!pde->proc_fops) {
183 spin_unlock(&pde->pde_unload_lock);
187 write = pde->proc_fops->write;
188 spin_unlock(&pde->pde_unload_lock);
191 rv = write(file, buf, count, ppos);
197 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
199 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
200 unsigned int rv = DEFAULT_POLLMASK;
201 unsigned int (*poll)(struct file *, struct poll_table_struct *);
203 spin_lock(&pde->pde_unload_lock);
204 if (!pde->proc_fops) {
205 spin_unlock(&pde->pde_unload_lock);
209 poll = pde->proc_fops->poll;
210 spin_unlock(&pde->pde_unload_lock);
213 rv = poll(file, pts);
219 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
221 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
223 long (*ioctl)(struct file *, unsigned int, unsigned long);
225 spin_lock(&pde->pde_unload_lock);
226 if (!pde->proc_fops) {
227 spin_unlock(&pde->pde_unload_lock);
231 ioctl = pde->proc_fops->unlocked_ioctl;
232 spin_unlock(&pde->pde_unload_lock);
235 rv = ioctl(file, cmd, arg);
242 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
244 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
246 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
248 spin_lock(&pde->pde_unload_lock);
249 if (!pde->proc_fops) {
250 spin_unlock(&pde->pde_unload_lock);
254 compat_ioctl = pde->proc_fops->compat_ioctl;
255 spin_unlock(&pde->pde_unload_lock);
258 rv = compat_ioctl(file, cmd, arg);
265 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
267 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
269 int (*mmap)(struct file *, struct vm_area_struct *);
271 spin_lock(&pde->pde_unload_lock);
272 if (!pde->proc_fops) {
273 spin_unlock(&pde->pde_unload_lock);
277 mmap = pde->proc_fops->mmap;
278 spin_unlock(&pde->pde_unload_lock);
281 rv = mmap(file, vma);
287 static int proc_reg_open(struct inode *inode, struct file *file)
289 struct proc_dir_entry *pde = PDE(inode);
291 int (*open)(struct inode *, struct file *);
292 int (*release)(struct inode *, struct file *);
293 struct pde_opener *pdeo;
296 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
297 * sequence. ->release won't be called because ->proc_fops will be
298 * cleared. Depending on complexity of ->release, consequences vary.
300 * We can't wait for mercy when close will be done for real, it's
301 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
302 * by hand in remove_proc_entry(). For this, save opener's credentials
305 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
309 spin_lock(&pde->pde_unload_lock);
310 if (!pde->proc_fops) {
311 spin_unlock(&pde->pde_unload_lock);
316 open = pde->proc_fops->open;
317 release = pde->proc_fops->release;
318 spin_unlock(&pde->pde_unload_lock);
321 rv = open(inode, file);
323 spin_lock(&pde->pde_unload_lock);
324 if (rv == 0 && release) {
325 /* To know what to release. */
328 /* Strictly for "too late" ->release in proc_reg_release(). */
329 pdeo->release = release;
330 list_add(&pdeo->lh, &pde->pde_openers);
333 __pde_users_dec(pde);
334 spin_unlock(&pde->pde_unload_lock);
338 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
339 struct inode *inode, struct file *file)
341 struct pde_opener *pdeo;
343 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
344 if (pdeo->inode == inode && pdeo->file == file)
350 static int proc_reg_release(struct inode *inode, struct file *file)
352 struct proc_dir_entry *pde = PDE(inode);
354 int (*release)(struct inode *, struct file *);
355 struct pde_opener *pdeo;
357 spin_lock(&pde->pde_unload_lock);
358 pdeo = find_pde_opener(pde, inode, file);
359 if (!pde->proc_fops) {
361 * Can't simply exit, __fput() will think that everything is OK,
362 * and move on to freeing struct file. remove_proc_entry() will
363 * find slacker in opener's list and will try to do non-trivial
364 * things with struct file. Therefore, remove opener from list.
366 * But if opener is removed from list, who will ->release it?
370 spin_unlock(&pde->pde_unload_lock);
371 rv = pdeo->release(inode, file);
374 spin_unlock(&pde->pde_unload_lock);
378 release = pde->proc_fops->release;
383 spin_unlock(&pde->pde_unload_lock);
386 rv = release(inode, file);
392 static const struct file_operations proc_reg_file_ops = {
393 .llseek = proc_reg_llseek,
394 .read = proc_reg_read,
395 .write = proc_reg_write,
396 .poll = proc_reg_poll,
397 .unlocked_ioctl = proc_reg_unlocked_ioctl,
399 .compat_ioctl = proc_reg_compat_ioctl,
401 .mmap = proc_reg_mmap,
402 .open = proc_reg_open,
403 .release = proc_reg_release,
407 static const struct file_operations proc_reg_file_ops_no_compat = {
408 .llseek = proc_reg_llseek,
409 .read = proc_reg_read,
410 .write = proc_reg_write,
411 .poll = proc_reg_poll,
412 .unlocked_ioctl = proc_reg_unlocked_ioctl,
413 .mmap = proc_reg_mmap,
414 .open = proc_reg_open,
415 .release = proc_reg_release,
419 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
420 struct proc_dir_entry *de)
422 struct inode * inode;
424 inode = iget_locked(sb, ino);
427 if (inode->i_state & I_NEW) {
428 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
429 PROC_I(inode)->fd = 0;
430 PROC_I(inode)->pde = de;
433 inode->i_mode = de->mode;
434 inode->i_uid = de->uid;
435 inode->i_gid = de->gid;
438 inode->i_size = de->size;
440 inode->i_nlink = de->nlink;
442 inode->i_op = de->proc_iops;
444 if (S_ISREG(inode->i_mode)) {
446 if (!de->proc_fops->compat_ioctl)
448 &proc_reg_file_ops_no_compat;
451 inode->i_fop = &proc_reg_file_ops;
453 inode->i_fop = de->proc_fops;
456 unlock_new_inode(inode);
462 int proc_fill_super(struct super_block *s)
464 struct inode * root_inode;
466 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
467 s->s_blocksize = 1024;
468 s->s_blocksize_bits = 10;
469 s->s_magic = PROC_SUPER_MAGIC;
470 s->s_op = &proc_sops;
474 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
477 root_inode->i_uid = 0;
478 root_inode->i_gid = 0;
479 s->s_root = d_alloc_root(root_inode);
485 printk("proc_read_super: get root inode failed\n");