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>
21 #include <asm/system.h>
22 #include <asm/uaccess.h>
26 struct proc_dir_entry *de_get(struct proc_dir_entry *de)
28 atomic_inc(&de->count);
33 * Decrements the use count and checks for deferred deletion.
35 void de_put(struct proc_dir_entry *de)
38 if (!atomic_read(&de->count)) {
39 printk("de_put: entry %s already free!\n", de->name);
44 if (atomic_dec_and_test(&de->count))
50 * Decrement the use count of the proc_dir_entry.
52 static void proc_delete_inode(struct inode *inode)
54 struct proc_dir_entry *de;
56 truncate_inode_pages(&inode->i_data, 0);
58 /* Stop tracking associated processes */
59 put_pid(PROC_I(inode)->pid);
61 /* Let go of any associated proc directory entry */
62 de = PROC_I(inode)->pde;
65 module_put(de->owner);
71 struct vfsmount *proc_mnt;
73 static struct kmem_cache * proc_inode_cachep;
75 static struct inode *proc_alloc_inode(struct super_block *sb)
77 struct proc_inode *ei;
80 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
85 ei->op.proc_get_link = NULL;
87 inode = &ei->vfs_inode;
88 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
92 static void proc_destroy_inode(struct inode *inode)
94 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
97 static void init_once(struct kmem_cache * cachep, void *foo)
99 struct proc_inode *ei = (struct proc_inode *) foo;
101 inode_init_once(&ei->vfs_inode);
104 int __init proc_init_inodecache(void)
106 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
107 sizeof(struct proc_inode),
108 0, (SLAB_RECLAIM_ACCOUNT|
109 SLAB_MEM_SPREAD|SLAB_PANIC),
114 static int proc_remount(struct super_block *sb, int *flags, char *data)
116 *flags |= MS_NODIRATIME;
120 static const struct super_operations proc_sops = {
121 .alloc_inode = proc_alloc_inode,
122 .destroy_inode = proc_destroy_inode,
123 .drop_inode = generic_delete_inode,
124 .delete_inode = proc_delete_inode,
125 .statfs = simple_statfs,
126 .remount_fs = proc_remount,
129 static void __pde_users_dec(struct proc_dir_entry *pde)
132 if (pde->pde_unload_completion && pde->pde_users == 0)
133 complete(pde->pde_unload_completion);
136 static void pde_users_dec(struct proc_dir_entry *pde)
138 spin_lock(&pde->pde_unload_lock);
139 __pde_users_dec(pde);
140 spin_unlock(&pde->pde_unload_lock);
143 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
145 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
147 loff_t (*llseek)(struct file *, loff_t, int);
149 spin_lock(&pde->pde_unload_lock);
151 * remove_proc_entry() is going to delete PDE (as part of module
152 * cleanup sequence). No new callers into module allowed.
154 if (!pde->proc_fops) {
155 spin_unlock(&pde->pde_unload_lock);
159 * Bump refcount so that remove_proc_entry will wail for ->llseek to
164 * Save function pointer under lock, to protect against ->proc_fops
165 * NULL'ifying right after ->pde_unload_lock is dropped.
167 llseek = pde->proc_fops->llseek;
168 spin_unlock(&pde->pde_unload_lock);
171 llseek = default_llseek;
172 rv = llseek(file, offset, whence);
178 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
180 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
182 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
184 spin_lock(&pde->pde_unload_lock);
185 if (!pde->proc_fops) {
186 spin_unlock(&pde->pde_unload_lock);
190 read = pde->proc_fops->read;
191 spin_unlock(&pde->pde_unload_lock);
194 rv = read(file, buf, count, ppos);
200 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
202 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
204 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
206 spin_lock(&pde->pde_unload_lock);
207 if (!pde->proc_fops) {
208 spin_unlock(&pde->pde_unload_lock);
212 write = pde->proc_fops->write;
213 spin_unlock(&pde->pde_unload_lock);
216 rv = write(file, buf, count, ppos);
222 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
224 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
225 unsigned int rv = DEFAULT_POLLMASK;
226 unsigned int (*poll)(struct file *, struct poll_table_struct *);
228 spin_lock(&pde->pde_unload_lock);
229 if (!pde->proc_fops) {
230 spin_unlock(&pde->pde_unload_lock);
234 poll = pde->proc_fops->poll;
235 spin_unlock(&pde->pde_unload_lock);
238 rv = poll(file, pts);
244 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
246 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
248 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
249 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
251 spin_lock(&pde->pde_unload_lock);
252 if (!pde->proc_fops) {
253 spin_unlock(&pde->pde_unload_lock);
257 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
258 ioctl = pde->proc_fops->ioctl;
259 spin_unlock(&pde->pde_unload_lock);
261 if (unlocked_ioctl) {
262 rv = unlocked_ioctl(file, cmd, arg);
263 if (rv == -ENOIOCTLCMD)
267 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
276 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
278 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
280 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
282 spin_lock(&pde->pde_unload_lock);
283 if (!pde->proc_fops) {
284 spin_unlock(&pde->pde_unload_lock);
288 compat_ioctl = pde->proc_fops->compat_ioctl;
289 spin_unlock(&pde->pde_unload_lock);
292 rv = compat_ioctl(file, cmd, arg);
299 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
301 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
303 int (*mmap)(struct file *, struct vm_area_struct *);
305 spin_lock(&pde->pde_unload_lock);
306 if (!pde->proc_fops) {
307 spin_unlock(&pde->pde_unload_lock);
311 mmap = pde->proc_fops->mmap;
312 spin_unlock(&pde->pde_unload_lock);
315 rv = mmap(file, vma);
321 static int proc_reg_open(struct inode *inode, struct file *file)
323 struct proc_dir_entry *pde = PDE(inode);
325 int (*open)(struct inode *, struct file *);
326 int (*release)(struct inode *, struct file *);
327 struct pde_opener *pdeo;
330 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
331 * sequence. ->release won't be called because ->proc_fops will be
332 * cleared. Depending on complexity of ->release, consequences vary.
334 * We can't wait for mercy when close will be done for real, it's
335 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
336 * by hand in remove_proc_entry(). For this, save opener's credentials
339 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
343 spin_lock(&pde->pde_unload_lock);
344 if (!pde->proc_fops) {
345 spin_unlock(&pde->pde_unload_lock);
350 open = pde->proc_fops->open;
351 release = pde->proc_fops->release;
352 spin_unlock(&pde->pde_unload_lock);
355 rv = open(inode, file);
357 spin_lock(&pde->pde_unload_lock);
358 if (rv == 0 && release) {
359 /* To know what to release. */
362 /* Strictly for "too late" ->release in proc_reg_release(). */
363 pdeo->release = release;
364 list_add(&pdeo->lh, &pde->pde_openers);
367 __pde_users_dec(pde);
368 spin_unlock(&pde->pde_unload_lock);
372 static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
373 struct inode *inode, struct file *file)
375 struct pde_opener *pdeo;
377 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
378 if (pdeo->inode == inode && pdeo->file == file)
384 static int proc_reg_release(struct inode *inode, struct file *file)
386 struct proc_dir_entry *pde = PDE(inode);
388 int (*release)(struct inode *, struct file *);
389 struct pde_opener *pdeo;
391 spin_lock(&pde->pde_unload_lock);
392 pdeo = find_pde_opener(pde, inode, file);
393 if (!pde->proc_fops) {
395 * Can't simply exit, __fput() will think that everything is OK,
396 * and move on to freeing struct file. remove_proc_entry() will
397 * find slacker in opener's list and will try to do non-trivial
398 * things with struct file. Therefore, remove opener from list.
400 * But if opener is removed from list, who will ->release it?
404 spin_unlock(&pde->pde_unload_lock);
405 rv = pdeo->release(inode, file);
408 spin_unlock(&pde->pde_unload_lock);
412 release = pde->proc_fops->release;
417 spin_unlock(&pde->pde_unload_lock);
420 rv = release(inode, file);
426 static const struct file_operations proc_reg_file_ops = {
427 .llseek = proc_reg_llseek,
428 .read = proc_reg_read,
429 .write = proc_reg_write,
430 .poll = proc_reg_poll,
431 .unlocked_ioctl = proc_reg_unlocked_ioctl,
433 .compat_ioctl = proc_reg_compat_ioctl,
435 .mmap = proc_reg_mmap,
436 .open = proc_reg_open,
437 .release = proc_reg_release,
441 static const struct file_operations proc_reg_file_ops_no_compat = {
442 .llseek = proc_reg_llseek,
443 .read = proc_reg_read,
444 .write = proc_reg_write,
445 .poll = proc_reg_poll,
446 .unlocked_ioctl = proc_reg_unlocked_ioctl,
447 .mmap = proc_reg_mmap,
448 .open = proc_reg_open,
449 .release = proc_reg_release,
453 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
454 struct proc_dir_entry *de)
456 struct inode * inode;
458 if (!try_module_get(de->owner))
461 inode = iget_locked(sb, ino);
464 if (inode->i_state & I_NEW) {
465 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
466 PROC_I(inode)->fd = 0;
467 PROC_I(inode)->pde = de;
470 inode->i_mode = de->mode;
471 inode->i_uid = de->uid;
472 inode->i_gid = de->gid;
475 inode->i_size = de->size;
477 inode->i_nlink = de->nlink;
479 inode->i_op = de->proc_iops;
481 if (S_ISREG(inode->i_mode)) {
483 if (!de->proc_fops->compat_ioctl)
485 &proc_reg_file_ops_no_compat;
488 inode->i_fop = &proc_reg_file_ops;
490 inode->i_fop = de->proc_fops;
493 unlock_new_inode(inode);
495 module_put(de->owner);
499 module_put(de->owner);
504 int proc_fill_super(struct super_block *s)
506 struct inode * root_inode;
508 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
509 s->s_blocksize = 1024;
510 s->s_blocksize_bits = 10;
511 s->s_magic = PROC_SUPER_MAGIC;
512 s->s_op = &proc_sops;
516 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
519 root_inode->i_uid = 0;
520 root_inode->i_gid = 0;
521 s->s_root = d_alloc_root(root_inode);
527 printk("proc_read_super: get root inode failed\n");