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)
29 atomic_inc(&de->count);
34 * Decrements the use count and checks for deferred deletion.
36 void de_put(struct proc_dir_entry *de)
40 if (!atomic_read(&de->count)) {
41 printk("de_put: entry %s already free!\n", de->name);
46 if (atomic_dec_and_test(&de->count)) {
48 printk("de_put: deferred delete of %s\n",
58 * Decrement the use count of the proc_dir_entry.
60 static void proc_delete_inode(struct inode *inode)
62 struct proc_dir_entry *de;
64 truncate_inode_pages(&inode->i_data, 0);
66 /* Stop tracking associated processes */
67 put_pid(PROC_I(inode)->pid);
69 /* Let go of any associated proc directory entry */
70 de = PROC_I(inode)->pde;
73 module_put(de->owner);
79 struct vfsmount *proc_mnt;
81 static void proc_read_inode(struct inode * inode)
83 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
86 static struct kmem_cache * proc_inode_cachep;
88 static struct inode *proc_alloc_inode(struct super_block *sb)
90 struct proc_inode *ei;
93 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
98 ei->op.proc_get_link = NULL;
100 inode = &ei->vfs_inode;
101 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
105 static void proc_destroy_inode(struct inode *inode)
107 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
110 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
112 struct proc_inode *ei = (struct proc_inode *) foo;
114 inode_init_once(&ei->vfs_inode);
117 int __init proc_init_inodecache(void)
119 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
120 sizeof(struct proc_inode),
121 0, (SLAB_RECLAIM_ACCOUNT|
124 if (proc_inode_cachep == NULL)
129 static int proc_remount(struct super_block *sb, int *flags, char *data)
131 *flags |= MS_NODIRATIME;
135 static const struct super_operations proc_sops = {
136 .alloc_inode = proc_alloc_inode,
137 .destroy_inode = proc_destroy_inode,
138 .read_inode = proc_read_inode,
139 .drop_inode = generic_delete_inode,
140 .delete_inode = proc_delete_inode,
141 .statfs = simple_statfs,
142 .remount_fs = proc_remount,
145 static void pde_users_dec(struct proc_dir_entry *pde)
147 spin_lock(&pde->pde_unload_lock);
149 if (pde->pde_unload_completion && pde->pde_users == 0)
150 complete(pde->pde_unload_completion);
151 spin_unlock(&pde->pde_unload_lock);
154 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
156 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
158 loff_t (*llseek)(struct file *, loff_t, int);
160 spin_lock(&pde->pde_unload_lock);
162 * remove_proc_entry() is going to delete PDE (as part of module
163 * cleanup sequence). No new callers into module allowed.
165 if (!pde->proc_fops) {
166 spin_unlock(&pde->pde_unload_lock);
170 * Bump refcount so that remove_proc_entry will wail for ->llseek to
175 * Save function pointer under lock, to protect against ->proc_fops
176 * NULL'ifying right after ->pde_unload_lock is dropped.
178 llseek = pde->proc_fops->llseek;
179 spin_unlock(&pde->pde_unload_lock);
182 llseek = default_llseek;
183 rv = llseek(file, offset, whence);
189 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
191 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
193 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
195 spin_lock(&pde->pde_unload_lock);
196 if (!pde->proc_fops) {
197 spin_unlock(&pde->pde_unload_lock);
201 read = pde->proc_fops->read;
202 spin_unlock(&pde->pde_unload_lock);
205 rv = read(file, buf, count, ppos);
211 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
213 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
215 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
217 spin_lock(&pde->pde_unload_lock);
218 if (!pde->proc_fops) {
219 spin_unlock(&pde->pde_unload_lock);
223 write = pde->proc_fops->write;
224 spin_unlock(&pde->pde_unload_lock);
227 rv = write(file, buf, count, ppos);
233 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
235 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
236 unsigned int rv = DEFAULT_POLLMASK;
237 unsigned int (*poll)(struct file *, struct poll_table_struct *);
239 spin_lock(&pde->pde_unload_lock);
240 if (!pde->proc_fops) {
241 spin_unlock(&pde->pde_unload_lock);
245 poll = pde->proc_fops->poll;
246 spin_unlock(&pde->pde_unload_lock);
249 rv = poll(file, pts);
255 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
257 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
259 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
260 int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
262 spin_lock(&pde->pde_unload_lock);
263 if (!pde->proc_fops) {
264 spin_unlock(&pde->pde_unload_lock);
268 unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
269 ioctl = pde->proc_fops->ioctl;
270 spin_unlock(&pde->pde_unload_lock);
272 if (unlocked_ioctl) {
273 rv = unlocked_ioctl(file, cmd, arg);
274 if (rv == -ENOIOCTLCMD)
278 rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
287 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
289 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
291 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
293 spin_lock(&pde->pde_unload_lock);
294 if (!pde->proc_fops) {
295 spin_unlock(&pde->pde_unload_lock);
299 compat_ioctl = pde->proc_fops->compat_ioctl;
300 spin_unlock(&pde->pde_unload_lock);
303 rv = compat_ioctl(file, cmd, arg);
310 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
312 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
314 int (*mmap)(struct file *, struct vm_area_struct *);
316 spin_lock(&pde->pde_unload_lock);
317 if (!pde->proc_fops) {
318 spin_unlock(&pde->pde_unload_lock);
322 mmap = pde->proc_fops->mmap;
323 spin_unlock(&pde->pde_unload_lock);
326 rv = mmap(file, vma);
332 static int proc_reg_open(struct inode *inode, struct file *file)
334 struct proc_dir_entry *pde = PDE(inode);
336 int (*open)(struct inode *, struct file *);
338 spin_lock(&pde->pde_unload_lock);
339 if (!pde->proc_fops) {
340 spin_unlock(&pde->pde_unload_lock);
344 open = pde->proc_fops->open;
345 spin_unlock(&pde->pde_unload_lock);
348 rv = open(inode, file);
354 static int proc_reg_release(struct inode *inode, struct file *file)
356 struct proc_dir_entry *pde = PDE(inode);
358 int (*release)(struct inode *, struct file *);
360 spin_lock(&pde->pde_unload_lock);
361 if (!pde->proc_fops) {
362 spin_unlock(&pde->pde_unload_lock);
366 release = pde->proc_fops->release;
367 spin_unlock(&pde->pde_unload_lock);
370 rv = release(inode, file);
376 static const struct file_operations proc_reg_file_ops = {
377 .llseek = proc_reg_llseek,
378 .read = proc_reg_read,
379 .write = proc_reg_write,
380 .poll = proc_reg_poll,
381 .unlocked_ioctl = proc_reg_unlocked_ioctl,
383 .compat_ioctl = proc_reg_compat_ioctl,
385 .mmap = proc_reg_mmap,
386 .open = proc_reg_open,
387 .release = proc_reg_release,
391 static const struct file_operations proc_reg_file_ops_no_compat = {
392 .llseek = proc_reg_llseek,
393 .read = proc_reg_read,
394 .write = proc_reg_write,
395 .poll = proc_reg_poll,
396 .unlocked_ioctl = proc_reg_unlocked_ioctl,
397 .mmap = proc_reg_mmap,
398 .open = proc_reg_open,
399 .release = proc_reg_release,
403 struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
404 struct proc_dir_entry *de)
406 struct inode * inode;
408 if (de != NULL && !try_module_get(de->owner))
411 inode = iget(sb, ino);
415 PROC_I(inode)->fd = 0;
416 PROC_I(inode)->pde = de;
419 inode->i_mode = de->mode;
420 inode->i_uid = de->uid;
421 inode->i_gid = de->gid;
424 inode->i_size = de->size;
426 inode->i_nlink = de->nlink;
428 inode->i_op = de->proc_iops;
430 if (S_ISREG(inode->i_mode)) {
432 if (!de->proc_fops->compat_ioctl)
434 &proc_reg_file_ops_no_compat;
437 inode->i_fop = &proc_reg_file_ops;
440 inode->i_fop = de->proc_fops;
448 module_put(de->owner);
453 int proc_fill_super(struct super_block *s, void *data, int silent)
455 struct inode * root_inode;
457 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
458 s->s_blocksize = 1024;
459 s->s_blocksize_bits = 10;
460 s->s_magic = PROC_SUPER_MAGIC;
461 s->s_op = &proc_sops;
465 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
468 root_inode->i_uid = 0;
469 root_inode->i_gid = 0;
470 s->s_root = d_alloc_root(root_inode);
476 printk("proc_read_super: get root inode failed\n");
481 MODULE_LICENSE("GPL");