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
;
31 const struct proc_ns_operations
*ns_ops
;
34 truncate_inode_pages(&inode
->i_data
, 0);
37 /* Stop tracking associated processes */
38 put_pid(PROC_I(inode
)->pid
);
40 /* Let go of any associated proc directory entry */
41 de
= PROC_I(inode
)->pde
;
44 head
= PROC_I(inode
)->sysctl
;
46 rcu_assign_pointer(PROC_I(inode
)->sysctl
, NULL
);
47 sysctl_head_put(head
);
49 /* Release any associated namespace */
50 ns_ops
= PROC_I(inode
)->ns_ops
;
51 ns
= PROC_I(inode
)->ns
;
56 static struct kmem_cache
* proc_inode_cachep
;
58 static struct inode
*proc_alloc_inode(struct super_block
*sb
)
60 struct proc_inode
*ei
;
63 ei
= (struct proc_inode
*)kmem_cache_alloc(proc_inode_cachep
, GFP_KERNEL
);
68 ei
->op
.proc_get_link
= NULL
;
71 ei
->sysctl_entry
= NULL
;
74 inode
= &ei
->vfs_inode
;
75 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
79 static void proc_i_callback(struct rcu_head
*head
)
81 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
82 INIT_LIST_HEAD(&inode
->i_dentry
);
83 kmem_cache_free(proc_inode_cachep
, PROC_I(inode
));
86 static void proc_destroy_inode(struct inode
*inode
)
88 call_rcu(&inode
->i_rcu
, proc_i_callback
);
91 static void init_once(void *foo
)
93 struct proc_inode
*ei
= (struct proc_inode
*) foo
;
95 inode_init_once(&ei
->vfs_inode
);
98 void __init
proc_init_inodecache(void)
100 proc_inode_cachep
= kmem_cache_create("proc_inode_cache",
101 sizeof(struct proc_inode
),
102 0, (SLAB_RECLAIM_ACCOUNT
|
103 SLAB_MEM_SPREAD
|SLAB_PANIC
),
107 static const struct super_operations proc_sops
= {
108 .alloc_inode
= proc_alloc_inode
,
109 .destroy_inode
= proc_destroy_inode
,
110 .drop_inode
= generic_delete_inode
,
111 .evict_inode
= proc_evict_inode
,
112 .statfs
= simple_statfs
,
115 static void __pde_users_dec(struct proc_dir_entry
*pde
)
118 if (pde
->pde_unload_completion
&& pde
->pde_users
== 0)
119 complete(pde
->pde_unload_completion
);
122 void pde_users_dec(struct proc_dir_entry
*pde
)
124 spin_lock(&pde
->pde_unload_lock
);
125 __pde_users_dec(pde
);
126 spin_unlock(&pde
->pde_unload_lock
);
129 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
131 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
133 loff_t (*llseek
)(struct file
*, loff_t
, int);
135 spin_lock(&pde
->pde_unload_lock
);
137 * remove_proc_entry() is going to delete PDE (as part of module
138 * cleanup sequence). No new callers into module allowed.
140 if (!pde
->proc_fops
) {
141 spin_unlock(&pde
->pde_unload_lock
);
145 * Bump refcount so that remove_proc_entry will wail for ->llseek to
150 * Save function pointer under lock, to protect against ->proc_fops
151 * NULL'ifying right after ->pde_unload_lock is dropped.
153 llseek
= pde
->proc_fops
->llseek
;
154 spin_unlock(&pde
->pde_unload_lock
);
157 llseek
= default_llseek
;
158 rv
= llseek(file
, offset
, whence
);
164 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
166 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
168 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
170 spin_lock(&pde
->pde_unload_lock
);
171 if (!pde
->proc_fops
) {
172 spin_unlock(&pde
->pde_unload_lock
);
176 read
= pde
->proc_fops
->read
;
177 spin_unlock(&pde
->pde_unload_lock
);
180 rv
= read(file
, buf
, count
, ppos
);
186 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
188 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
190 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
192 spin_lock(&pde
->pde_unload_lock
);
193 if (!pde
->proc_fops
) {
194 spin_unlock(&pde
->pde_unload_lock
);
198 write
= pde
->proc_fops
->write
;
199 spin_unlock(&pde
->pde_unload_lock
);
202 rv
= write(file
, buf
, count
, ppos
);
208 static unsigned int proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
210 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
211 unsigned int rv
= DEFAULT_POLLMASK
;
212 unsigned int (*poll
)(struct file
*, struct poll_table_struct
*);
214 spin_lock(&pde
->pde_unload_lock
);
215 if (!pde
->proc_fops
) {
216 spin_unlock(&pde
->pde_unload_lock
);
220 poll
= pde
->proc_fops
->poll
;
221 spin_unlock(&pde
->pde_unload_lock
);
224 rv
= poll(file
, pts
);
230 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
232 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
234 long (*ioctl
)(struct file
*, unsigned int, unsigned long);
236 spin_lock(&pde
->pde_unload_lock
);
237 if (!pde
->proc_fops
) {
238 spin_unlock(&pde
->pde_unload_lock
);
242 ioctl
= pde
->proc_fops
->unlocked_ioctl
;
243 spin_unlock(&pde
->pde_unload_lock
);
246 rv
= ioctl(file
, cmd
, arg
);
253 static long proc_reg_compat_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 (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
259 spin_lock(&pde
->pde_unload_lock
);
260 if (!pde
->proc_fops
) {
261 spin_unlock(&pde
->pde_unload_lock
);
265 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
266 spin_unlock(&pde
->pde_unload_lock
);
269 rv
= compat_ioctl(file
, cmd
, arg
);
276 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
278 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
280 int (*mmap
)(struct file
*, struct vm_area_struct
*);
282 spin_lock(&pde
->pde_unload_lock
);
283 if (!pde
->proc_fops
) {
284 spin_unlock(&pde
->pde_unload_lock
);
288 mmap
= pde
->proc_fops
->mmap
;
289 spin_unlock(&pde
->pde_unload_lock
);
292 rv
= mmap(file
, vma
);
298 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
300 struct proc_dir_entry
*pde
= PDE(inode
);
302 int (*open
)(struct inode
*, struct file
*);
303 int (*release
)(struct inode
*, struct file
*);
304 struct pde_opener
*pdeo
;
307 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
308 * sequence. ->release won't be called because ->proc_fops will be
309 * cleared. Depending on complexity of ->release, consequences vary.
311 * We can't wait for mercy when close will be done for real, it's
312 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
313 * by hand in remove_proc_entry(). For this, save opener's credentials
316 pdeo
= kmalloc(sizeof(struct pde_opener
), GFP_KERNEL
);
320 spin_lock(&pde
->pde_unload_lock
);
321 if (!pde
->proc_fops
) {
322 spin_unlock(&pde
->pde_unload_lock
);
327 open
= pde
->proc_fops
->open
;
328 release
= pde
->proc_fops
->release
;
329 spin_unlock(&pde
->pde_unload_lock
);
332 rv
= open(inode
, file
);
334 spin_lock(&pde
->pde_unload_lock
);
335 if (rv
== 0 && release
) {
336 /* To know what to release. */
339 /* Strictly for "too late" ->release in proc_reg_release(). */
340 pdeo
->release
= release
;
341 list_add(&pdeo
->lh
, &pde
->pde_openers
);
344 __pde_users_dec(pde
);
345 spin_unlock(&pde
->pde_unload_lock
);
349 static struct pde_opener
*find_pde_opener(struct proc_dir_entry
*pde
,
350 struct inode
*inode
, struct file
*file
)
352 struct pde_opener
*pdeo
;
354 list_for_each_entry(pdeo
, &pde
->pde_openers
, lh
) {
355 if (pdeo
->inode
== inode
&& pdeo
->file
== file
)
361 static int proc_reg_release(struct inode
*inode
, struct file
*file
)
363 struct proc_dir_entry
*pde
= PDE(inode
);
365 int (*release
)(struct inode
*, struct file
*);
366 struct pde_opener
*pdeo
;
368 spin_lock(&pde
->pde_unload_lock
);
369 pdeo
= find_pde_opener(pde
, inode
, file
);
370 if (!pde
->proc_fops
) {
372 * Can't simply exit, __fput() will think that everything is OK,
373 * and move on to freeing struct file. remove_proc_entry() will
374 * find slacker in opener's list and will try to do non-trivial
375 * things with struct file. Therefore, remove opener from list.
377 * But if opener is removed from list, who will ->release it?
381 spin_unlock(&pde
->pde_unload_lock
);
382 rv
= pdeo
->release(inode
, file
);
385 spin_unlock(&pde
->pde_unload_lock
);
389 release
= pde
->proc_fops
->release
;
394 spin_unlock(&pde
->pde_unload_lock
);
397 rv
= release(inode
, file
);
403 static const struct file_operations proc_reg_file_ops
= {
404 .llseek
= proc_reg_llseek
,
405 .read
= proc_reg_read
,
406 .write
= proc_reg_write
,
407 .poll
= proc_reg_poll
,
408 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
410 .compat_ioctl
= proc_reg_compat_ioctl
,
412 .mmap
= proc_reg_mmap
,
413 .open
= proc_reg_open
,
414 .release
= proc_reg_release
,
418 static const struct file_operations proc_reg_file_ops_no_compat
= {
419 .llseek
= proc_reg_llseek
,
420 .read
= proc_reg_read
,
421 .write
= proc_reg_write
,
422 .poll
= proc_reg_poll
,
423 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
424 .mmap
= proc_reg_mmap
,
425 .open
= proc_reg_open
,
426 .release
= proc_reg_release
,
430 struct inode
*proc_get_inode(struct super_block
*sb
, struct proc_dir_entry
*de
)
432 struct inode
* inode
;
434 inode
= iget_locked(sb
, de
->low_ino
);
437 if (inode
->i_state
& I_NEW
) {
438 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
439 PROC_I(inode
)->fd
= 0;
440 PROC_I(inode
)->pde
= de
;
443 inode
->i_mode
= de
->mode
;
444 inode
->i_uid
= de
->uid
;
445 inode
->i_gid
= de
->gid
;
448 inode
->i_size
= de
->size
;
450 inode
->i_nlink
= de
->nlink
;
452 inode
->i_op
= de
->proc_iops
;
454 if (S_ISREG(inode
->i_mode
)) {
456 if (!de
->proc_fops
->compat_ioctl
)
458 &proc_reg_file_ops_no_compat
;
461 inode
->i_fop
= &proc_reg_file_ops
;
463 inode
->i_fop
= de
->proc_fops
;
466 unlock_new_inode(inode
);
472 int proc_fill_super(struct super_block
*s
)
474 struct inode
* root_inode
;
476 s
->s_flags
|= MS_NODIRATIME
| MS_NOSUID
| MS_NOEXEC
;
477 s
->s_blocksize
= 1024;
478 s
->s_blocksize_bits
= 10;
479 s
->s_magic
= PROC_SUPER_MAGIC
;
480 s
->s_op
= &proc_sops
;
484 root_inode
= proc_get_inode(s
, &proc_root
);
487 root_inode
->i_uid
= 0;
488 root_inode
->i_gid
= 0;
489 s
->s_root
= d_alloc_root(root_inode
);
495 printk("proc_read_super: get root inode failed\n");