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(struct kmem_cache
* cachep
, void *foo
)
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
|
122 SLAB_MEM_SPREAD
|SLAB_PANIC
),
127 static int proc_remount(struct super_block
*sb
, int *flags
, char *data
)
129 *flags
|= MS_NODIRATIME
;
133 static const struct super_operations proc_sops
= {
134 .alloc_inode
= proc_alloc_inode
,
135 .destroy_inode
= proc_destroy_inode
,
136 .read_inode
= proc_read_inode
,
137 .drop_inode
= generic_delete_inode
,
138 .delete_inode
= proc_delete_inode
,
139 .statfs
= simple_statfs
,
140 .remount_fs
= proc_remount
,
143 static void pde_users_dec(struct proc_dir_entry
*pde
)
145 spin_lock(&pde
->pde_unload_lock
);
147 if (pde
->pde_unload_completion
&& pde
->pde_users
== 0)
148 complete(pde
->pde_unload_completion
);
149 spin_unlock(&pde
->pde_unload_lock
);
152 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
154 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
156 loff_t (*llseek
)(struct file
*, loff_t
, int);
158 spin_lock(&pde
->pde_unload_lock
);
160 * remove_proc_entry() is going to delete PDE (as part of module
161 * cleanup sequence). No new callers into module allowed.
163 if (!pde
->proc_fops
) {
164 spin_unlock(&pde
->pde_unload_lock
);
168 * Bump refcount so that remove_proc_entry will wail for ->llseek to
173 * Save function pointer under lock, to protect against ->proc_fops
174 * NULL'ifying right after ->pde_unload_lock is dropped.
176 llseek
= pde
->proc_fops
->llseek
;
177 spin_unlock(&pde
->pde_unload_lock
);
180 llseek
= default_llseek
;
181 rv
= llseek(file
, offset
, whence
);
187 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
189 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
191 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
193 spin_lock(&pde
->pde_unload_lock
);
194 if (!pde
->proc_fops
) {
195 spin_unlock(&pde
->pde_unload_lock
);
199 read
= pde
->proc_fops
->read
;
200 spin_unlock(&pde
->pde_unload_lock
);
203 rv
= read(file
, buf
, count
, ppos
);
209 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
211 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
213 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
215 spin_lock(&pde
->pde_unload_lock
);
216 if (!pde
->proc_fops
) {
217 spin_unlock(&pde
->pde_unload_lock
);
221 write
= pde
->proc_fops
->write
;
222 spin_unlock(&pde
->pde_unload_lock
);
225 rv
= write(file
, buf
, count
, ppos
);
231 static unsigned int proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
233 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
234 unsigned int rv
= DEFAULT_POLLMASK
;
235 unsigned int (*poll
)(struct file
*, struct poll_table_struct
*);
237 spin_lock(&pde
->pde_unload_lock
);
238 if (!pde
->proc_fops
) {
239 spin_unlock(&pde
->pde_unload_lock
);
243 poll
= pde
->proc_fops
->poll
;
244 spin_unlock(&pde
->pde_unload_lock
);
247 rv
= poll(file
, pts
);
253 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
255 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
257 long (*unlocked_ioctl
)(struct file
*, unsigned int, unsigned long);
258 int (*ioctl
)(struct inode
*, struct file
*, unsigned int, unsigned long);
260 spin_lock(&pde
->pde_unload_lock
);
261 if (!pde
->proc_fops
) {
262 spin_unlock(&pde
->pde_unload_lock
);
266 unlocked_ioctl
= pde
->proc_fops
->unlocked_ioctl
;
267 ioctl
= pde
->proc_fops
->ioctl
;
268 spin_unlock(&pde
->pde_unload_lock
);
270 if (unlocked_ioctl
) {
271 rv
= unlocked_ioctl(file
, cmd
, arg
);
272 if (rv
== -ENOIOCTLCMD
)
276 rv
= ioctl(file
->f_path
.dentry
->d_inode
, file
, cmd
, arg
);
285 static long proc_reg_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
287 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
289 long (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
291 spin_lock(&pde
->pde_unload_lock
);
292 if (!pde
->proc_fops
) {
293 spin_unlock(&pde
->pde_unload_lock
);
297 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
298 spin_unlock(&pde
->pde_unload_lock
);
301 rv
= compat_ioctl(file
, cmd
, arg
);
308 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
310 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
312 int (*mmap
)(struct file
*, struct vm_area_struct
*);
314 spin_lock(&pde
->pde_unload_lock
);
315 if (!pde
->proc_fops
) {
316 spin_unlock(&pde
->pde_unload_lock
);
320 mmap
= pde
->proc_fops
->mmap
;
321 spin_unlock(&pde
->pde_unload_lock
);
324 rv
= mmap(file
, vma
);
330 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
332 struct proc_dir_entry
*pde
= PDE(inode
);
334 int (*open
)(struct inode
*, struct file
*);
336 spin_lock(&pde
->pde_unload_lock
);
337 if (!pde
->proc_fops
) {
338 spin_unlock(&pde
->pde_unload_lock
);
342 open
= pde
->proc_fops
->open
;
343 spin_unlock(&pde
->pde_unload_lock
);
346 rv
= open(inode
, file
);
352 static int proc_reg_release(struct inode
*inode
, struct file
*file
)
354 struct proc_dir_entry
*pde
= PDE(inode
);
356 int (*release
)(struct inode
*, struct file
*);
358 spin_lock(&pde
->pde_unload_lock
);
359 if (!pde
->proc_fops
) {
360 spin_unlock(&pde
->pde_unload_lock
);
364 release
= pde
->proc_fops
->release
;
365 spin_unlock(&pde
->pde_unload_lock
);
368 rv
= release(inode
, file
);
374 static const struct file_operations proc_reg_file_ops
= {
375 .llseek
= proc_reg_llseek
,
376 .read
= proc_reg_read
,
377 .write
= proc_reg_write
,
378 .poll
= proc_reg_poll
,
379 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
381 .compat_ioctl
= proc_reg_compat_ioctl
,
383 .mmap
= proc_reg_mmap
,
384 .open
= proc_reg_open
,
385 .release
= proc_reg_release
,
389 static const struct file_operations proc_reg_file_ops_no_compat
= {
390 .llseek
= proc_reg_llseek
,
391 .read
= proc_reg_read
,
392 .write
= proc_reg_write
,
393 .poll
= proc_reg_poll
,
394 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
395 .mmap
= proc_reg_mmap
,
396 .open
= proc_reg_open
,
397 .release
= proc_reg_release
,
401 struct inode
*proc_get_inode(struct super_block
*sb
, unsigned int ino
,
402 struct proc_dir_entry
*de
)
404 struct inode
* inode
;
406 if (de
!= NULL
&& !try_module_get(de
->owner
))
409 inode
= iget(sb
, ino
);
413 PROC_I(inode
)->fd
= 0;
414 PROC_I(inode
)->pde
= de
;
417 inode
->i_mode
= de
->mode
;
418 inode
->i_uid
= de
->uid
;
419 inode
->i_gid
= de
->gid
;
422 inode
->i_size
= de
->size
;
424 inode
->i_nlink
= de
->nlink
;
426 inode
->i_op
= de
->proc_iops
;
428 if (S_ISREG(inode
->i_mode
)) {
430 if (!de
->proc_fops
->compat_ioctl
)
432 &proc_reg_file_ops_no_compat
;
435 inode
->i_fop
= &proc_reg_file_ops
;
438 inode
->i_fop
= de
->proc_fops
;
446 module_put(de
->owner
);
451 int proc_fill_super(struct super_block
*s
)
453 struct inode
* root_inode
;
455 s
->s_flags
|= MS_NODIRATIME
| MS_NOSUID
| MS_NOEXEC
;
456 s
->s_blocksize
= 1024;
457 s
->s_blocksize_bits
= 10;
458 s
->s_magic
= PROC_SUPER_MAGIC
;
459 s
->s_op
= &proc_sops
;
463 root_inode
= proc_get_inode(s
, PROC_ROOT_INO
, &proc_root
);
466 root_inode
->i_uid
= 0;
467 root_inode
->i_gid
= 0;
468 s
->s_root
= d_alloc_root(root_inode
);
474 printk("proc_read_super: get root inode failed\n");
479 MODULE_LICENSE("GPL");