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
))
53 * Decrement the use count of the proc_dir_entry.
55 static void proc_delete_inode(struct inode
*inode
)
57 struct proc_dir_entry
*de
;
59 truncate_inode_pages(&inode
->i_data
, 0);
61 /* Stop tracking associated processes */
62 put_pid(PROC_I(inode
)->pid
);
64 /* Let go of any associated proc directory entry */
65 de
= PROC_I(inode
)->pde
;
68 module_put(de
->owner
);
74 struct vfsmount
*proc_mnt
;
76 static void proc_read_inode(struct inode
* inode
)
78 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
81 static struct kmem_cache
* proc_inode_cachep
;
83 static struct inode
*proc_alloc_inode(struct super_block
*sb
)
85 struct proc_inode
*ei
;
88 ei
= (struct proc_inode
*)kmem_cache_alloc(proc_inode_cachep
, GFP_KERNEL
);
93 ei
->op
.proc_get_link
= NULL
;
95 inode
= &ei
->vfs_inode
;
96 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
100 static void proc_destroy_inode(struct inode
*inode
)
102 kmem_cache_free(proc_inode_cachep
, PROC_I(inode
));
105 static void init_once(struct kmem_cache
* cachep
, void *foo
)
107 struct proc_inode
*ei
= (struct proc_inode
*) foo
;
109 inode_init_once(&ei
->vfs_inode
);
112 int __init
proc_init_inodecache(void)
114 proc_inode_cachep
= kmem_cache_create("proc_inode_cache",
115 sizeof(struct proc_inode
),
116 0, (SLAB_RECLAIM_ACCOUNT
|
117 SLAB_MEM_SPREAD
|SLAB_PANIC
),
122 static int proc_remount(struct super_block
*sb
, int *flags
, char *data
)
124 *flags
|= MS_NODIRATIME
;
128 static const struct super_operations proc_sops
= {
129 .alloc_inode
= proc_alloc_inode
,
130 .destroy_inode
= proc_destroy_inode
,
131 .read_inode
= proc_read_inode
,
132 .drop_inode
= generic_delete_inode
,
133 .delete_inode
= proc_delete_inode
,
134 .statfs
= simple_statfs
,
135 .remount_fs
= proc_remount
,
138 static void pde_users_dec(struct proc_dir_entry
*pde
)
140 spin_lock(&pde
->pde_unload_lock
);
142 if (pde
->pde_unload_completion
&& pde
->pde_users
== 0)
143 complete(pde
->pde_unload_completion
);
144 spin_unlock(&pde
->pde_unload_lock
);
147 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
149 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
151 loff_t (*llseek
)(struct file
*, loff_t
, int);
153 spin_lock(&pde
->pde_unload_lock
);
155 * remove_proc_entry() is going to delete PDE (as part of module
156 * cleanup sequence). No new callers into module allowed.
158 if (!pde
->proc_fops
) {
159 spin_unlock(&pde
->pde_unload_lock
);
163 * Bump refcount so that remove_proc_entry will wail for ->llseek to
168 * Save function pointer under lock, to protect against ->proc_fops
169 * NULL'ifying right after ->pde_unload_lock is dropped.
171 llseek
= pde
->proc_fops
->llseek
;
172 spin_unlock(&pde
->pde_unload_lock
);
175 llseek
= default_llseek
;
176 rv
= llseek(file
, offset
, whence
);
182 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
184 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
186 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
188 spin_lock(&pde
->pde_unload_lock
);
189 if (!pde
->proc_fops
) {
190 spin_unlock(&pde
->pde_unload_lock
);
194 read
= pde
->proc_fops
->read
;
195 spin_unlock(&pde
->pde_unload_lock
);
198 rv
= read(file
, buf
, count
, ppos
);
204 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
206 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
208 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
210 spin_lock(&pde
->pde_unload_lock
);
211 if (!pde
->proc_fops
) {
212 spin_unlock(&pde
->pde_unload_lock
);
216 write
= pde
->proc_fops
->write
;
217 spin_unlock(&pde
->pde_unload_lock
);
220 rv
= write(file
, buf
, count
, ppos
);
226 static unsigned int proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
228 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
229 unsigned int rv
= DEFAULT_POLLMASK
;
230 unsigned int (*poll
)(struct file
*, struct poll_table_struct
*);
232 spin_lock(&pde
->pde_unload_lock
);
233 if (!pde
->proc_fops
) {
234 spin_unlock(&pde
->pde_unload_lock
);
238 poll
= pde
->proc_fops
->poll
;
239 spin_unlock(&pde
->pde_unload_lock
);
242 rv
= poll(file
, pts
);
248 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
250 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
252 long (*unlocked_ioctl
)(struct file
*, unsigned int, unsigned long);
253 int (*ioctl
)(struct inode
*, struct file
*, unsigned int, unsigned long);
255 spin_lock(&pde
->pde_unload_lock
);
256 if (!pde
->proc_fops
) {
257 spin_unlock(&pde
->pde_unload_lock
);
261 unlocked_ioctl
= pde
->proc_fops
->unlocked_ioctl
;
262 ioctl
= pde
->proc_fops
->ioctl
;
263 spin_unlock(&pde
->pde_unload_lock
);
265 if (unlocked_ioctl
) {
266 rv
= unlocked_ioctl(file
, cmd
, arg
);
267 if (rv
== -ENOIOCTLCMD
)
271 rv
= ioctl(file
->f_path
.dentry
->d_inode
, file
, cmd
, arg
);
280 static long proc_reg_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
282 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
284 long (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
286 spin_lock(&pde
->pde_unload_lock
);
287 if (!pde
->proc_fops
) {
288 spin_unlock(&pde
->pde_unload_lock
);
292 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
293 spin_unlock(&pde
->pde_unload_lock
);
296 rv
= compat_ioctl(file
, cmd
, arg
);
303 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
305 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
307 int (*mmap
)(struct file
*, struct vm_area_struct
*);
309 spin_lock(&pde
->pde_unload_lock
);
310 if (!pde
->proc_fops
) {
311 spin_unlock(&pde
->pde_unload_lock
);
315 mmap
= pde
->proc_fops
->mmap
;
316 spin_unlock(&pde
->pde_unload_lock
);
319 rv
= mmap(file
, vma
);
325 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
327 struct proc_dir_entry
*pde
= PDE(inode
);
329 int (*open
)(struct inode
*, struct file
*);
331 spin_lock(&pde
->pde_unload_lock
);
332 if (!pde
->proc_fops
) {
333 spin_unlock(&pde
->pde_unload_lock
);
337 open
= pde
->proc_fops
->open
;
338 spin_unlock(&pde
->pde_unload_lock
);
341 rv
= open(inode
, file
);
347 static int proc_reg_release(struct inode
*inode
, struct file
*file
)
349 struct proc_dir_entry
*pde
= PDE(inode
);
351 int (*release
)(struct inode
*, struct file
*);
353 spin_lock(&pde
->pde_unload_lock
);
354 if (!pde
->proc_fops
) {
355 spin_unlock(&pde
->pde_unload_lock
);
359 release
= pde
->proc_fops
->release
;
360 spin_unlock(&pde
->pde_unload_lock
);
363 rv
= release(inode
, file
);
369 static const struct file_operations proc_reg_file_ops
= {
370 .llseek
= proc_reg_llseek
,
371 .read
= proc_reg_read
,
372 .write
= proc_reg_write
,
373 .poll
= proc_reg_poll
,
374 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
376 .compat_ioctl
= proc_reg_compat_ioctl
,
378 .mmap
= proc_reg_mmap
,
379 .open
= proc_reg_open
,
380 .release
= proc_reg_release
,
384 static const struct file_operations proc_reg_file_ops_no_compat
= {
385 .llseek
= proc_reg_llseek
,
386 .read
= proc_reg_read
,
387 .write
= proc_reg_write
,
388 .poll
= proc_reg_poll
,
389 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
390 .mmap
= proc_reg_mmap
,
391 .open
= proc_reg_open
,
392 .release
= proc_reg_release
,
396 struct inode
*proc_get_inode(struct super_block
*sb
, unsigned int ino
,
397 struct proc_dir_entry
*de
)
399 struct inode
* inode
;
401 if (de
!= NULL
&& !try_module_get(de
->owner
))
404 inode
= iget(sb
, ino
);
408 PROC_I(inode
)->fd
= 0;
409 PROC_I(inode
)->pde
= de
;
412 inode
->i_mode
= de
->mode
;
413 inode
->i_uid
= de
->uid
;
414 inode
->i_gid
= de
->gid
;
417 inode
->i_size
= de
->size
;
419 inode
->i_nlink
= de
->nlink
;
421 inode
->i_op
= de
->proc_iops
;
423 if (S_ISREG(inode
->i_mode
)) {
425 if (!de
->proc_fops
->compat_ioctl
)
427 &proc_reg_file_ops_no_compat
;
430 inode
->i_fop
= &proc_reg_file_ops
;
433 inode
->i_fop
= de
->proc_fops
;
441 module_put(de
->owner
);
446 int proc_fill_super(struct super_block
*s
)
448 struct inode
* root_inode
;
450 s
->s_flags
|= MS_NODIRATIME
| MS_NOSUID
| MS_NOEXEC
;
451 s
->s_blocksize
= 1024;
452 s
->s_blocksize_bits
= 10;
453 s
->s_magic
= PROC_SUPER_MAGIC
;
454 s
->s_op
= &proc_sops
;
458 root_inode
= proc_get_inode(s
, PROC_ROOT_INO
, &proc_root
);
461 root_inode
->i_uid
= 0;
462 root_inode
->i_gid
= 0;
463 s
->s_root
= d_alloc_root(root_inode
);
469 printk("proc_read_super: get root inode failed\n");
474 MODULE_LICENSE("GPL");