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
)
28 atomic_inc(&de
->count
);
33 * Decrements the use count and checks for deferred deletion.
35 void de_put(struct proc_dir_entry
*de
)
38 if (!atomic_read(&de
->count
)) {
39 printk("de_put: entry %s already free!\n", de
->name
);
44 if (atomic_dec_and_test(&de
->count
))
50 * Decrement the use count of the proc_dir_entry.
52 static void proc_delete_inode(struct inode
*inode
)
54 struct proc_dir_entry
*de
;
56 truncate_inode_pages(&inode
->i_data
, 0);
58 /* Stop tracking associated processes */
59 put_pid(PROC_I(inode
)->pid
);
61 /* Let go of any associated proc directory entry */
62 de
= PROC_I(inode
)->pde
;
65 module_put(de
->owner
);
71 struct vfsmount
*proc_mnt
;
73 static struct kmem_cache
* proc_inode_cachep
;
75 static struct inode
*proc_alloc_inode(struct super_block
*sb
)
77 struct proc_inode
*ei
;
80 ei
= (struct proc_inode
*)kmem_cache_alloc(proc_inode_cachep
, GFP_KERNEL
);
85 ei
->op
.proc_get_link
= NULL
;
87 inode
= &ei
->vfs_inode
;
88 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
92 static void proc_destroy_inode(struct inode
*inode
)
94 kmem_cache_free(proc_inode_cachep
, PROC_I(inode
));
97 static void init_once(struct kmem_cache
* cachep
, void *foo
)
99 struct proc_inode
*ei
= (struct proc_inode
*) foo
;
101 inode_init_once(&ei
->vfs_inode
);
104 int __init
proc_init_inodecache(void)
106 proc_inode_cachep
= kmem_cache_create("proc_inode_cache",
107 sizeof(struct proc_inode
),
108 0, (SLAB_RECLAIM_ACCOUNT
|
109 SLAB_MEM_SPREAD
|SLAB_PANIC
),
114 static int proc_remount(struct super_block
*sb
, int *flags
, char *data
)
116 *flags
|= MS_NODIRATIME
;
120 static const struct super_operations proc_sops
= {
121 .alloc_inode
= proc_alloc_inode
,
122 .destroy_inode
= proc_destroy_inode
,
123 .drop_inode
= generic_delete_inode
,
124 .delete_inode
= proc_delete_inode
,
125 .statfs
= simple_statfs
,
126 .remount_fs
= proc_remount
,
129 static void pde_users_dec(struct proc_dir_entry
*pde
)
131 spin_lock(&pde
->pde_unload_lock
);
133 if (pde
->pde_unload_completion
&& pde
->pde_users
== 0)
134 complete(pde
->pde_unload_completion
);
135 spin_unlock(&pde
->pde_unload_lock
);
138 static loff_t
proc_reg_llseek(struct file
*file
, loff_t offset
, int whence
)
140 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
142 loff_t (*llseek
)(struct file
*, loff_t
, int);
144 spin_lock(&pde
->pde_unload_lock
);
146 * remove_proc_entry() is going to delete PDE (as part of module
147 * cleanup sequence). No new callers into module allowed.
149 if (!pde
->proc_fops
) {
150 spin_unlock(&pde
->pde_unload_lock
);
154 * Bump refcount so that remove_proc_entry will wail for ->llseek to
159 * Save function pointer under lock, to protect against ->proc_fops
160 * NULL'ifying right after ->pde_unload_lock is dropped.
162 llseek
= pde
->proc_fops
->llseek
;
163 spin_unlock(&pde
->pde_unload_lock
);
166 llseek
= default_llseek
;
167 rv
= llseek(file
, offset
, whence
);
173 static ssize_t
proc_reg_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
175 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
177 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
179 spin_lock(&pde
->pde_unload_lock
);
180 if (!pde
->proc_fops
) {
181 spin_unlock(&pde
->pde_unload_lock
);
185 read
= pde
->proc_fops
->read
;
186 spin_unlock(&pde
->pde_unload_lock
);
189 rv
= read(file
, buf
, count
, ppos
);
195 static ssize_t
proc_reg_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
197 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
199 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
201 spin_lock(&pde
->pde_unload_lock
);
202 if (!pde
->proc_fops
) {
203 spin_unlock(&pde
->pde_unload_lock
);
207 write
= pde
->proc_fops
->write
;
208 spin_unlock(&pde
->pde_unload_lock
);
211 rv
= write(file
, buf
, count
, ppos
);
217 static unsigned int proc_reg_poll(struct file
*file
, struct poll_table_struct
*pts
)
219 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
220 unsigned int rv
= DEFAULT_POLLMASK
;
221 unsigned int (*poll
)(struct file
*, struct poll_table_struct
*);
223 spin_lock(&pde
->pde_unload_lock
);
224 if (!pde
->proc_fops
) {
225 spin_unlock(&pde
->pde_unload_lock
);
229 poll
= pde
->proc_fops
->poll
;
230 spin_unlock(&pde
->pde_unload_lock
);
233 rv
= poll(file
, pts
);
239 static long proc_reg_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
241 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
243 long (*unlocked_ioctl
)(struct file
*, unsigned int, unsigned long);
244 int (*ioctl
)(struct inode
*, struct file
*, unsigned int, unsigned long);
246 spin_lock(&pde
->pde_unload_lock
);
247 if (!pde
->proc_fops
) {
248 spin_unlock(&pde
->pde_unload_lock
);
252 unlocked_ioctl
= pde
->proc_fops
->unlocked_ioctl
;
253 ioctl
= pde
->proc_fops
->ioctl
;
254 spin_unlock(&pde
->pde_unload_lock
);
256 if (unlocked_ioctl
) {
257 rv
= unlocked_ioctl(file
, cmd
, arg
);
258 if (rv
== -ENOIOCTLCMD
)
262 rv
= ioctl(file
->f_path
.dentry
->d_inode
, file
, cmd
, arg
);
271 static long proc_reg_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
273 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
275 long (*compat_ioctl
)(struct file
*, unsigned int, unsigned long);
277 spin_lock(&pde
->pde_unload_lock
);
278 if (!pde
->proc_fops
) {
279 spin_unlock(&pde
->pde_unload_lock
);
283 compat_ioctl
= pde
->proc_fops
->compat_ioctl
;
284 spin_unlock(&pde
->pde_unload_lock
);
287 rv
= compat_ioctl(file
, cmd
, arg
);
294 static int proc_reg_mmap(struct file
*file
, struct vm_area_struct
*vma
)
296 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
298 int (*mmap
)(struct file
*, struct vm_area_struct
*);
300 spin_lock(&pde
->pde_unload_lock
);
301 if (!pde
->proc_fops
) {
302 spin_unlock(&pde
->pde_unload_lock
);
306 mmap
= pde
->proc_fops
->mmap
;
307 spin_unlock(&pde
->pde_unload_lock
);
310 rv
= mmap(file
, vma
);
316 static int proc_reg_open(struct inode
*inode
, struct file
*file
)
318 struct proc_dir_entry
*pde
= PDE(inode
);
320 int (*open
)(struct inode
*, struct file
*);
322 spin_lock(&pde
->pde_unload_lock
);
323 if (!pde
->proc_fops
) {
324 spin_unlock(&pde
->pde_unload_lock
);
328 open
= pde
->proc_fops
->open
;
329 spin_unlock(&pde
->pde_unload_lock
);
332 rv
= open(inode
, file
);
338 static int proc_reg_release(struct inode
*inode
, struct file
*file
)
340 struct proc_dir_entry
*pde
= PDE(inode
);
342 int (*release
)(struct inode
*, struct file
*);
344 spin_lock(&pde
->pde_unload_lock
);
345 if (!pde
->proc_fops
) {
346 spin_unlock(&pde
->pde_unload_lock
);
350 release
= pde
->proc_fops
->release
;
351 spin_unlock(&pde
->pde_unload_lock
);
354 rv
= release(inode
, file
);
360 static const struct file_operations proc_reg_file_ops
= {
361 .llseek
= proc_reg_llseek
,
362 .read
= proc_reg_read
,
363 .write
= proc_reg_write
,
364 .poll
= proc_reg_poll
,
365 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
367 .compat_ioctl
= proc_reg_compat_ioctl
,
369 .mmap
= proc_reg_mmap
,
370 .open
= proc_reg_open
,
371 .release
= proc_reg_release
,
375 static const struct file_operations proc_reg_file_ops_no_compat
= {
376 .llseek
= proc_reg_llseek
,
377 .read
= proc_reg_read
,
378 .write
= proc_reg_write
,
379 .poll
= proc_reg_poll
,
380 .unlocked_ioctl
= proc_reg_unlocked_ioctl
,
381 .mmap
= proc_reg_mmap
,
382 .open
= proc_reg_open
,
383 .release
= proc_reg_release
,
387 struct inode
*proc_get_inode(struct super_block
*sb
, unsigned int ino
,
388 struct proc_dir_entry
*de
)
390 struct inode
* inode
;
392 if (!try_module_get(de
->owner
))
395 inode
= iget_locked(sb
, ino
);
398 if (inode
->i_state
& I_NEW
) {
399 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
400 PROC_I(inode
)->fd
= 0;
401 PROC_I(inode
)->pde
= de
;
404 inode
->i_mode
= de
->mode
;
405 inode
->i_uid
= de
->uid
;
406 inode
->i_gid
= de
->gid
;
409 inode
->i_size
= de
->size
;
411 inode
->i_nlink
= de
->nlink
;
413 inode
->i_op
= de
->proc_iops
;
415 if (S_ISREG(inode
->i_mode
)) {
417 if (!de
->proc_fops
->compat_ioctl
)
419 &proc_reg_file_ops_no_compat
;
422 inode
->i_fop
= &proc_reg_file_ops
;
424 inode
->i_fop
= de
->proc_fops
;
427 unlock_new_inode(inode
);
429 module_put(de
->owner
);
433 module_put(de
->owner
);
438 int proc_fill_super(struct super_block
*s
)
440 struct inode
* root_inode
;
442 s
->s_flags
|= MS_NODIRATIME
| MS_NOSUID
| MS_NOEXEC
;
443 s
->s_blocksize
= 1024;
444 s
->s_blocksize_bits
= 10;
445 s
->s_magic
= PROC_SUPER_MAGIC
;
446 s
->s_op
= &proc_sops
;
450 root_inode
= proc_get_inode(s
, PROC_ROOT_INO
, &proc_root
);
453 root_inode
->i_uid
= 0;
454 root_inode
->i_gid
= 0;
455 s
->s_root
= d_alloc_root(root_inode
);
461 printk("proc_read_super: get root inode failed\n");