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/sysctl.h>
20 #include <linux/slab.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
27 static void proc_evict_inode(struct inode
*inode
)
29 struct proc_dir_entry
*de
;
30 struct ctl_table_header
*head
;
32 truncate_inode_pages(&inode
->i_data
, 0);
35 /* Stop tracking associated processes */
36 put_pid(PROC_I(inode
)->pid
);
38 /* Let go of any associated proc directory entry */
39 de
= PROC_I(inode
)->pde
;
42 head
= PROC_I(inode
)->sysctl
;
44 rcu_assign_pointer(PROC_I(inode
)->sysctl
, NULL
);
45 sysctl_head_put(head
);
49 struct vfsmount
*proc_mnt
;
51 static struct kmem_cache
* proc_inode_cachep
;
53 static struct inode
*proc_alloc_inode(struct super_block
*sb
)
55 struct proc_inode
*ei
;
58 ei
= (struct proc_inode
*)kmem_cache_alloc(proc_inode_cachep
, GFP_KERNEL
);
63 ei
->op
.proc_get_link
= NULL
;
66 ei
->sysctl_entry
= NULL
;
67 inode
= &ei
->vfs_inode
;
68 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
72 static void proc_i_callback(struct rcu_head
*head
)
74 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
75 INIT_LIST_HEAD(&inode
->i_dentry
);
76 kmem_cache_free(proc_inode_cachep
, PROC_I(inode
));
79 static void proc_destroy_inode(struct inode
*inode
)
81 call_rcu(&inode
->i_rcu
, proc_i_callback
);
84 static void init_once(void *foo
)
86 struct proc_inode
*ei
= (struct proc_inode
*) foo
;
88 inode_init_once(&ei
->vfs_inode
);
91 void __init
proc_init_inodecache(void)
93 proc_inode_cachep
= kmem_cache_create("proc_inode_cache",
94 sizeof(struct proc_inode
),
95 0, (SLAB_RECLAIM_ACCOUNT
|
96 SLAB_MEM_SPREAD
|SLAB_PANIC
),
100 static const struct super_operations proc_sops
= {
101 .alloc_inode
= proc_alloc_inode
,
102 .destroy_inode
= proc_destroy_inode
,
103 .drop_inode
= generic_delete_inode
,
104 .evict_inode
= proc_evict_inode
,
105 .statfs
= simple_statfs
,
108 static void __pde_users_dec(struct proc_dir_entry
*pde
)
111 if (pde
->pde_unload_completion
&& pde
->pde_users
== 0)
112 complete(pde
->pde_unload_completion
);
115 void pde_users_dec(struct proc_dir_entry
*pde
)
117 spin_lock(&pde
->pde_unload_lock
);
118 __pde_users_dec(pde
);
119 spin_unlock(&pde
->pde_unload_lock
);
122 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
124 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
126 loff_t (*llseek
)(struct file
*, loff_t
, int);
128 spin_lock(&pde
->pde_unload_lock
);
130 * remove_proc_entry() is going to delete PDE (as part of module
131 * cleanup sequence). No new callers into module allowed.
133 if (!pde
->proc_fops
) {
134 spin_unlock(&pde
->pde_unload_lock
);
138 * Bump refcount so that remove_proc_entry will wail for ->llseek to
143 * Save function pointer under lock, to protect against ->proc_fops
144 * NULL'ifying right after ->pde_unload_lock is dropped.
146 llseek
= pde
->proc_fops
->llseek
;
147 spin_unlock(&pde
->pde_unload_lock
);
150 llseek
= default_llseek
;
151 rv
= llseek(file
, offset
, whence
);
157 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
159 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
161 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
163 spin_lock(&pde
->pde_unload_lock
);
164 if (!pde
->proc_fops
) {
165 spin_unlock(&pde
->pde_unload_lock
);
169 read
= pde
->proc_fops
->read
;
170 spin_unlock(&pde
->pde_unload_lock
);
173 rv
= read(file
, buf
, count
, ppos
);
179 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
181 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
183 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
185 spin_lock(&pde
->pde_unload_lock
);
186 if (!pde
->proc_fops
) {
187 spin_unlock(&pde
->pde_unload_lock
);
191 write
= pde
->proc_fops
->write
;
192 spin_unlock(&pde
->pde_unload_lock
);
195 rv
= write(file
, buf
, count
, ppos
);
201 static unsigned int proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
203 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
204 unsigned int rv
= DEFAULT_POLLMASK
;
205 unsigned int (*poll
)(struct file
*, struct poll_table_struct
*);
207 spin_lock(&pde
->pde_unload_lock
);
208 if (!pde
->proc_fops
) {
209 spin_unlock(&pde
->pde_unload_lock
);
213 poll
= pde
->proc_fops
->poll
;
214 spin_unlock(&pde
->pde_unload_lock
);
217 rv
= poll(file
, pts
);
223 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
225 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
227 long (*ioctl
)(struct file
*, unsigned int, unsigned long);
229 spin_lock(&pde
->pde_unload_lock
);
230 if (!pde
->proc_fops
) {
231 spin_unlock(&pde
->pde_unload_lock
);
235 ioctl
= pde
->proc_fops
->unlocked_ioctl
;
236 spin_unlock(&pde
->pde_unload_lock
);
239 rv
= ioctl(file
, cmd
, arg
);
246 static long proc_reg_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
248 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
250 long (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
252 spin_lock(&pde
->pde_unload_lock
);
253 if (!pde
->proc_fops
) {
254 spin_unlock(&pde
->pde_unload_lock
);
258 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
259 spin_unlock(&pde
->pde_unload_lock
);
262 rv
= compat_ioctl(file
, cmd
, arg
);
269 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
271 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
273 int (*mmap
)(struct file
*, struct vm_area_struct
*);
275 spin_lock(&pde
->pde_unload_lock
);
276 if (!pde
->proc_fops
) {
277 spin_unlock(&pde
->pde_unload_lock
);
281 mmap
= pde
->proc_fops
->mmap
;
282 spin_unlock(&pde
->pde_unload_lock
);
285 rv
= mmap(file
, vma
);
291 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
293 struct proc_dir_entry
*pde
= PDE(inode
);
295 int (*open
)(struct inode
*, struct file
*);
296 int (*release
)(struct inode
*, struct file
*);
297 struct pde_opener
*pdeo
;
300 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
301 * sequence. ->release won't be called because ->proc_fops will be
302 * cleared. Depending on complexity of ->release, consequences vary.
304 * We can't wait for mercy when close will be done for real, it's
305 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
306 * by hand in remove_proc_entry(). For this, save opener's credentials
309 pdeo
= kmalloc(sizeof(struct pde_opener
), GFP_KERNEL
);
313 spin_lock(&pde
->pde_unload_lock
);
314 if (!pde
->proc_fops
) {
315 spin_unlock(&pde
->pde_unload_lock
);
320 open
= pde
->proc_fops
->open
;
321 release
= pde
->proc_fops
->release
;
322 spin_unlock(&pde
->pde_unload_lock
);
325 rv
= open(inode
, file
);
327 spin_lock(&pde
->pde_unload_lock
);
328 if (rv
== 0 && release
) {
329 /* To know what to release. */
332 /* Strictly for "too late" ->release in proc_reg_release(). */
333 pdeo
->release
= release
;
334 list_add(&pdeo
->lh
, &pde
->pde_openers
);
337 __pde_users_dec(pde
);
338 spin_unlock(&pde
->pde_unload_lock
);
342 static struct pde_opener
*find_pde_opener(struct proc_dir_entry
*pde
,
343 struct inode
*inode
, struct file
*file
)
345 struct pde_opener
*pdeo
;
347 list_for_each_entry(pdeo
, &pde
->pde_openers
, lh
) {
348 if (pdeo
->inode
== inode
&& pdeo
->file
== 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
*);
359 struct pde_opener
*pdeo
;
361 spin_lock(&pde
->pde_unload_lock
);
362 pdeo
= find_pde_opener(pde
, inode
, file
);
363 if (!pde
->proc_fops
) {
365 * Can't simply exit, __fput() will think that everything is OK,
366 * and move on to freeing struct file. remove_proc_entry() will
367 * find slacker in opener's list and will try to do non-trivial
368 * things with struct file. Therefore, remove opener from list.
370 * But if opener is removed from list, who will ->release it?
374 spin_unlock(&pde
->pde_unload_lock
);
375 rv
= pdeo
->release(inode
, file
);
378 spin_unlock(&pde
->pde_unload_lock
);
382 release
= pde
->proc_fops
->release
;
387 spin_unlock(&pde
->pde_unload_lock
);
390 rv
= release(inode
, file
);
396 static const struct file_operations proc_reg_file_ops
= {
397 .llseek
= proc_reg_llseek
,
398 .read
= proc_reg_read
,
399 .write
= proc_reg_write
,
400 .poll
= proc_reg_poll
,
401 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
403 .compat_ioctl
= proc_reg_compat_ioctl
,
405 .mmap
= proc_reg_mmap
,
406 .open
= proc_reg_open
,
407 .release
= proc_reg_release
,
411 static const struct file_operations proc_reg_file_ops_no_compat
= {
412 .llseek
= proc_reg_llseek
,
413 .read
= proc_reg_read
,
414 .write
= proc_reg_write
,
415 .poll
= proc_reg_poll
,
416 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
417 .mmap
= proc_reg_mmap
,
418 .open
= proc_reg_open
,
419 .release
= proc_reg_release
,
423 struct inode
*proc_get_inode(struct super_block
*sb
, struct proc_dir_entry
*de
)
425 struct inode
* inode
;
427 inode
= iget_locked(sb
, de
->low_ino
);
430 if (inode
->i_state
& I_NEW
) {
431 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
432 PROC_I(inode
)->fd
= 0;
433 PROC_I(inode
)->pde
= de
;
436 inode
->i_mode
= de
->mode
;
437 inode
->i_uid
= de
->uid
;
438 inode
->i_gid
= de
->gid
;
441 inode
->i_size
= de
->size
;
443 inode
->i_nlink
= de
->nlink
;
445 inode
->i_op
= de
->proc_iops
;
447 if (S_ISREG(inode
->i_mode
)) {
449 if (!de
->proc_fops
->compat_ioctl
)
451 &proc_reg_file_ops_no_compat
;
454 inode
->i_fop
= &proc_reg_file_ops
;
456 inode
->i_fop
= de
->proc_fops
;
459 unlock_new_inode(inode
);
465 int proc_fill_super(struct super_block
*s
)
467 struct inode
* root_inode
;
469 s
->s_flags
|= MS_NODIRATIME
| MS_NOSUID
| MS_NOEXEC
;
470 s
->s_blocksize
= 1024;
471 s
->s_blocksize_bits
= 10;
472 s
->s_magic
= PROC_SUPER_MAGIC
;
473 s
->s_op
= &proc_sops
;
477 root_inode
= proc_get_inode(s
, &proc_root
);
480 root_inode
->i_uid
= 0;
481 root_inode
->i_gid
= 0;
482 s
->s_root
= d_alloc_root(root_inode
);
488 printk("proc_read_super: get root inode failed\n");