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/file.h>
15 #include <linux/limits.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/smp_lock.h>
20 #include <asm/system.h>
21 #include <asm/uaccess.h>
25 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)
39 if (!atomic_read(&de->count)) {
40 printk("de_put: entry %s already free!\n", de->name);
45 if (atomic_dec_and_test(&de->count)) {
47 printk("de_put: deferred delete of %s\n",
57 * Decrement the use count of the proc_dir_entry.
59 static void proc_delete_inode(struct inode *inode)
61 struct proc_dir_entry *de;
63 truncate_inode_pages(&inode->i_data, 0);
65 /* Stop tracking associated processes */
66 put_pid(PROC_I(inode)->pid);
68 /* Let go of any associated proc directory entry */
69 de = PROC_I(inode)->pde;
72 module_put(de->owner);
78 struct vfsmount *proc_mnt;
80 static void proc_read_inode(struct inode * inode)
82 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
85 static struct kmem_cache * proc_inode_cachep;
87 static struct inode *proc_alloc_inode(struct super_block *sb)
89 struct proc_inode *ei;
92 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
97 ei->op.proc_get_link = NULL;
99 inode = &ei->vfs_inode;
100 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
104 static void proc_destroy_inode(struct inode *inode)
106 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
109 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
111 struct proc_inode *ei = (struct proc_inode *) foo;
113 inode_init_once(&ei->vfs_inode);
116 int __init proc_init_inodecache(void)
118 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
119 sizeof(struct proc_inode),
120 0, (SLAB_RECLAIM_ACCOUNT|
123 if (proc_inode_cachep == NULL)
128 static int proc_remount(struct super_block *sb, int *flags, char *data)
130 *flags |= MS_NODIRATIME;
134 static const struct super_operations proc_sops = {
135 .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,
144 static void pde_users_dec(struct proc_dir_entry *pde)
146 spin_lock(&pde->pde_unload_lock);
148 if (pde->pde_unload_completion && pde->pde_users == 0)
149 complete(pde->pde_unload_completion);
150 spin_unlock(&pde->pde_unload_lock);
153 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
155 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
157 loff_t (*llseek)(struct file *, loff_t, int);
159 spin_lock(&pde->pde_unload_lock);
161 * remove_proc_entry() is going to delete PDE (as part of module
162 * cleanup sequence). No new callers into module allowed.
164 if (!pde->proc_fops) {
165 spin_unlock(&pde->pde_unload_lock);
169 * Bump refcount so that remove_proc_entry will wail for ->llseek to
174 * Save function pointer under lock, to protect against ->proc_fops
175 * NULL'ifying right after ->pde_unload_lock is dropped.
177 llseek = pde->proc_fops->llseek;
178 spin_unlock(&pde->pde_unload_lock);
181 llseek = default_llseek;
182 rv = llseek(file, offset, whence);
188 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
190 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
192 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
194 spin_lock(&pde->pde_unload_lock);
195 if (!pde->proc_fops) {
196 spin_unlock(&pde->pde_unload_lock);
200 read = pde->proc_fops->read;
201 spin_unlock(&pde->pde_unload_lock);
204 rv = read(file, buf, count, ppos);
210 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
212 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
214 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
216 spin_lock(&pde->pde_unload_lock);
217 if (!pde->proc_fops) {
218 spin_unlock(&pde->pde_unload_lock);
222 write = pde->proc_fops->write;
223 spin_unlock(&pde->pde_unload_lock);
226 rv = write(file, buf, count, ppos);
232 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
234 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
236 unsigned int (*poll)(struct file *, struct poll_table_struct *);
238 spin_lock(&pde->pde_unload_lock);
239 if (!pde->proc_fops) {
240 spin_unlock(&pde->pde_unload_lock);
244 poll = pde->proc_fops->poll;
245 spin_unlock(&pde->pde_unload_lock);
248 rv = poll(file, pts);
254 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
256 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
258 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
259 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
261 spin_lock(&pde->pde_unload_lock);
262 if (!pde->proc_fops) {
263 spin_unlock(&pde->pde_unload_lock);
267 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
268 ioctl = pde->proc_fops->ioctl;
269 spin_unlock(&pde->pde_unload_lock);
271 if (unlocked_ioctl) {
272 rv = unlocked_ioctl(file, cmd, arg);
273 if (rv == -ENOIOCTLCMD)
277 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
286 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
288 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
290 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
292 spin_lock(&pde->pde_unload_lock);
293 if (!pde->proc_fops) {
294 spin_unlock(&pde->pde_unload_lock);
298 compat_ioctl = pde->proc_fops->compat_ioctl;
299 spin_unlock(&pde->pde_unload_lock);
302 rv = compat_ioctl(file, cmd, arg);
309 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
311 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
313 int (*mmap)(struct file *, struct vm_area_struct *);
315 spin_lock(&pde->pde_unload_lock);
316 if (!pde->proc_fops) {
317 spin_unlock(&pde->pde_unload_lock);
321 mmap = pde->proc_fops->mmap;
322 spin_unlock(&pde->pde_unload_lock);
325 rv = mmap(file, vma);
331 static int proc_reg_open(struct inode *inode, struct file *file)
333 struct proc_dir_entry *pde = PDE(inode);
335 int (*open)(struct inode *, struct file *);
337 spin_lock(&pde->pde_unload_lock);
338 if (!pde->proc_fops) {
339 spin_unlock(&pde->pde_unload_lock);
343 open = pde->proc_fops->open;
344 spin_unlock(&pde->pde_unload_lock);
347 rv = open(inode, file);
353 static int proc_reg_release(struct inode *inode, struct file *file)
355 struct proc_dir_entry *pde = PDE(inode);
357 int (*release)(struct inode *, struct file *);
359 spin_lock(&pde->pde_unload_lock);
360 if (!pde->proc_fops) {
361 spin_unlock(&pde->pde_unload_lock);
365 release = pde->proc_fops->release;
366 spin_unlock(&pde->pde_unload_lock);
369 rv = release(inode, file);
375 static 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,
382 .compat_ioctl = proc_reg_compat_ioctl,
384 .mmap = proc_reg_mmap,
385 .open = proc_reg_open,
386 .release = proc_reg_release,
390 static const struct file_operations proc_reg_file_ops_no_compat = {
391 .llseek = proc_reg_llseek,
392 .read = proc_reg_read,
393 .write = proc_reg_write,
394 .poll = proc_reg_poll,
395 .unlocked_ioctl = proc_reg_unlocked_ioctl,
396 .mmap = proc_reg_mmap,
397 .open = proc_reg_open,
398 .release = proc_reg_release,
402 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
403 struct proc_dir_entry *de)
405 struct inode * inode;
407 if (de != NULL && !try_module_get(de->owner))
410 inode = iget(sb, ino);
414 PROC_I(inode)->fd = 0;
415 PROC_I(inode)->pde = de;
418 inode->i_mode = de->mode;
419 inode->i_uid = de->uid;
420 inode->i_gid = de->gid;
423 inode->i_size = de->size;
425 inode->i_nlink = de->nlink;
427 inode->i_op = de->proc_iops;
429 if (S_ISREG(inode->i_mode)) {
431 if (!de->proc_fops->compat_ioctl)
433 &proc_reg_file_ops_no_compat;
436 inode->i_fop = &proc_reg_file_ops;
439 inode->i_fop = de->proc_fops;
447 module_put(de->owner);
452 int proc_fill_super(struct super_block *s, void *data, int silent)
454 struct inode * root_inode;
456 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
457 s->s_blocksize = 1024;
458 s->s_blocksize_bits = 10;
459 s->s_magic = PROC_SUPER_MAGIC;
460 s->s_op = &proc_sops;
464 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
467 root_inode->i_uid = 0;
468 root_inode->i_gid = 0;
469 s->s_root = d_alloc_root(root_inode);
475 printk("proc_read_super: get root inode failed\n");
480 MODULE_LICENSE("GPL");