MIPS: Fix LLVM build issue.
[linux-2.6-block.git] / fs / proc / inode.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/proc/inode.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/time.h>
8#include <linux/proc_fs.h>
9#include <linux/kernel.h>
97412950 10#include <linux/pid_namespace.h>
1da177e4
LT
11#include <linux/mm.h>
12#include <linux/string.h>
13#include <linux/stat.h>
786d7e16 14#include <linux/completion.h>
dd23aae4 15#include <linux/poll.h>
87ebdc00 16#include <linux/printk.h>
1da177e4
LT
17#include <linux/file.h>
18#include <linux/limits.h>
19#include <linux/init.h>
20#include <linux/module.h>
9043476f 21#include <linux/sysctl.h>
97412950 22#include <linux/seq_file.h>
5a0e3ad6 23#include <linux/slab.h>
97412950 24#include <linux/mount.h>
303eb7e2 25#include <linux/magic.h>
1da177e4 26
1da177e4
LT
27#include <asm/uaccess.h>
28
fee781e6 29#include "internal.h"
1da177e4 30
8267952b 31static void proc_evict_inode(struct inode *inode)
1da177e4
LT
32{
33 struct proc_dir_entry *de;
dfef6dcd 34 struct ctl_table_header *head;
1da177e4 35
91b0abe3 36 truncate_inode_pages_final(&inode->i_data);
dbd5768f 37 clear_inode(inode);
fef26658 38
99f89551 39 /* Stop tracking associated processes */
13b41b09 40 put_pid(PROC_I(inode)->pid);
1da177e4
LT
41
42 /* Let go of any associated proc directory entry */
6bee55f9 43 de = PDE(inode);
99b76233 44 if (de)
135d5655 45 pde_put(de);
dfef6dcd
AV
46 head = PROC_I(inode)->sysctl;
47 if (head) {
1c44dbc8 48 RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
dfef6dcd
AV
49 sysctl_head_put(head);
50 }
1da177e4
LT
51}
52
e18b890b 53static struct kmem_cache * proc_inode_cachep;
1da177e4
LT
54
55static struct inode *proc_alloc_inode(struct super_block *sb)
56{
57 struct proc_inode *ei;
58 struct inode *inode;
59
e94b1766 60 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
1da177e4
LT
61 if (!ei)
62 return NULL;
13b41b09 63 ei->pid = NULL;
aed7a6c4 64 ei->fd = 0;
1da177e4
LT
65 ei->op.proc_get_link = NULL;
66 ei->pde = NULL;
9043476f
AV
67 ei->sysctl = NULL;
68 ei->sysctl_entry = NULL;
3d3d35b1 69 ei->ns_ops = NULL;
1da177e4
LT
70 inode = &ei->vfs_inode;
71 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
72 return inode;
73}
74
fa0d7e3d 75static void proc_i_callback(struct rcu_head *head)
1da177e4 76{
fa0d7e3d 77 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
78 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
79}
80
fa0d7e3d
NP
81static void proc_destroy_inode(struct inode *inode)
82{
83 call_rcu(&inode->i_rcu, proc_i_callback);
84}
85
51cc5068 86static void init_once(void *foo)
1da177e4
LT
87{
88 struct proc_inode *ei = (struct proc_inode *) foo;
89
a35afb83 90 inode_init_once(&ei->vfs_inode);
1da177e4 91}
20c2df83 92
5bcd7ff9 93void __init proc_init_inodecache(void)
1da177e4
LT
94{
95 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
96 sizeof(struct proc_inode),
fffb60f9 97 0, (SLAB_RECLAIM_ACCOUNT|
040b5c6f 98 SLAB_MEM_SPREAD|SLAB_PANIC),
20c2df83 99 init_once);
1da177e4
LT
100}
101
97412950
VK
102static int proc_show_options(struct seq_file *seq, struct dentry *root)
103{
0499680a
VK
104 struct super_block *sb = root->d_sb;
105 struct pid_namespace *pid = sb->s_fs_info;
106
dcb0f222
EB
107 if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
108 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
0499680a
VK
109 if (pid->hide_pid != 0)
110 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
111
97412950
VK
112 return 0;
113}
114
ee9b6d61 115static const struct super_operations proc_sops = {
1da177e4
LT
116 .alloc_inode = proc_alloc_inode,
117 .destroy_inode = proc_destroy_inode,
1da177e4 118 .drop_inode = generic_delete_inode,
8267952b 119 .evict_inode = proc_evict_inode,
1da177e4 120 .statfs = simple_statfs,
97412950
VK
121 .remount_fs = proc_remount,
122 .show_options = proc_show_options,
1da177e4
LT
123};
124
866ad9a7
AV
125enum {BIAS = -1U<<31};
126
127static inline int use_pde(struct proc_dir_entry *pde)
128{
05c0ae21 129 return atomic_inc_unless_negative(&pde->in_use);
881adb85
AD
130}
131
866ad9a7 132static void unuse_pde(struct proc_dir_entry *pde)
881adb85 133{
05c0ae21
AV
134 if (atomic_dec_return(&pde->in_use) == BIAS)
135 complete(pde->pde_unload_completion);
786d7e16
AD
136}
137
ca469f35
AV
138/* pde is locked */
139static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
140{
05c0ae21 141 if (pdeo->closing) {
ca469f35 142 /* somebody else is doing that, just wait */
05c0ae21
AV
143 DECLARE_COMPLETION_ONSTACK(c);
144 pdeo->c = &c;
ca469f35 145 spin_unlock(&pde->pde_unload_lock);
05c0ae21 146 wait_for_completion(&c);
ca469f35 147 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
148 } else {
149 struct file *file;
05c0ae21 150 pdeo->closing = 1;
ca469f35
AV
151 spin_unlock(&pde->pde_unload_lock);
152 file = pdeo->file;
153 pde->proc_fops->release(file_inode(file), file);
154 spin_lock(&pde->pde_unload_lock);
155 list_del_init(&pdeo->lh);
05c0ae21
AV
156 if (pdeo->c)
157 complete(pdeo->c);
ca469f35 158 kfree(pdeo);
05c0ae21 159 }
ca469f35
AV
160}
161
866ad9a7 162void proc_entry_rundown(struct proc_dir_entry *de)
786d7e16 163{
05c0ae21 164 DECLARE_COMPLETION_ONSTACK(c);
866ad9a7 165 /* Wait until all existing callers into module are done. */
05c0ae21
AV
166 de->pde_unload_completion = &c;
167 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
168 wait_for_completion(&c);
786d7e16 169
05c0ae21 170 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
171 while (!list_empty(&de->pde_openers)) {
172 struct pde_opener *pdeo;
866ad9a7 173 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
ca469f35 174 close_pdeo(de, pdeo);
866ad9a7
AV
175 }
176 spin_unlock(&de->pde_unload_lock);
786d7e16
AD
177}
178
866ad9a7
AV
179static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
180{
181 struct proc_dir_entry *pde = PDE(file_inode(file));
182 loff_t rv = -EINVAL;
183 if (use_pde(pde)) {
184 loff_t (*llseek)(struct file *, loff_t, int);
185 llseek = pde->proc_fops->llseek;
186 if (!llseek)
187 llseek = default_llseek;
188 rv = llseek(file, offset, whence);
189 unuse_pde(pde);
190 }
786d7e16
AD
191 return rv;
192}
193
866ad9a7 194static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
786d7e16 195{
866ad9a7 196 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
496ad9aa 197 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 198 ssize_t rv = -EIO;
866ad9a7
AV
199 if (use_pde(pde)) {
200 read = pde->proc_fops->read;
201 if (read)
202 rv = read(file, buf, count, ppos);
203 unuse_pde(pde);
786d7e16 204 }
866ad9a7
AV
205 return rv;
206}
786d7e16 207
866ad9a7
AV
208static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
209{
210 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
211 struct proc_dir_entry *pde = PDE(file_inode(file));
212 ssize_t rv = -EIO;
213 if (use_pde(pde)) {
214 write = pde->proc_fops->write;
215 if (write)
216 rv = write(file, buf, count, ppos);
217 unuse_pde(pde);
218 }
786d7e16
AD
219 return rv;
220}
221
222static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
223{
496ad9aa 224 struct proc_dir_entry *pde = PDE(file_inode(file));
dd23aae4 225 unsigned int rv = DEFAULT_POLLMASK;
786d7e16 226 unsigned int (*poll)(struct file *, struct poll_table_struct *);
866ad9a7
AV
227 if (use_pde(pde)) {
228 poll = pde->proc_fops->poll;
229 if (poll)
230 rv = poll(file, pts);
231 unuse_pde(pde);
786d7e16 232 }
786d7e16
AD
233 return rv;
234}
235
236static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
237{
496ad9aa 238 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 239 long rv = -ENOTTY;
b19dd42f 240 long (*ioctl)(struct file *, unsigned int, unsigned long);
866ad9a7
AV
241 if (use_pde(pde)) {
242 ioctl = pde->proc_fops->unlocked_ioctl;
243 if (ioctl)
244 rv = ioctl(file, cmd, arg);
245 unuse_pde(pde);
786d7e16 246 }
786d7e16
AD
247 return rv;
248}
249
250#ifdef CONFIG_COMPAT
251static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
252{
496ad9aa 253 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16
AD
254 long rv = -ENOTTY;
255 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
866ad9a7
AV
256 if (use_pde(pde)) {
257 compat_ioctl = pde->proc_fops->compat_ioctl;
258 if (compat_ioctl)
259 rv = compat_ioctl(file, cmd, arg);
260 unuse_pde(pde);
786d7e16 261 }
786d7e16
AD
262 return rv;
263}
264#endif
265
266static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
267{
496ad9aa 268 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16
AD
269 int rv = -EIO;
270 int (*mmap)(struct file *, struct vm_area_struct *);
866ad9a7
AV
271 if (use_pde(pde)) {
272 mmap = pde->proc_fops->mmap;
273 if (mmap)
274 rv = mmap(file, vma);
275 unuse_pde(pde);
786d7e16 276 }
786d7e16
AD
277 return rv;
278}
279
5721cf84
HD
280static unsigned long
281proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
282 unsigned long len, unsigned long pgoff,
283 unsigned long flags)
c4fe2448
AD
284{
285 struct proc_dir_entry *pde = PDE(file_inode(file));
2cbe3b0a 286 unsigned long rv = -EIO;
ae5758a1 287
c4fe2448 288 if (use_pde(pde)) {
ae5758a1
JB
289 typeof(proc_reg_get_unmapped_area) *get_area;
290
291 get_area = pde->proc_fops->get_unmapped_area;
fad1a86e 292#ifdef CONFIG_MMU
ae5758a1
JB
293 if (!get_area)
294 get_area = current->mm->get_unmapped_area;
fad1a86e 295#endif
ae5758a1 296
5721cf84
HD
297 if (get_area)
298 rv = get_area(file, orig_addr, len, pgoff, flags);
ae5758a1
JB
299 else
300 rv = orig_addr;
c4fe2448
AD
301 unuse_pde(pde);
302 }
303 return rv;
304}
305
786d7e16
AD
306static int proc_reg_open(struct inode *inode, struct file *file)
307{
308 struct proc_dir_entry *pde = PDE(inode);
309 int rv = 0;
310 int (*open)(struct inode *, struct file *);
881adb85
AD
311 int (*release)(struct inode *, struct file *);
312 struct pde_opener *pdeo;
313
314 /*
315 * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
316 * sequence. ->release won't be called because ->proc_fops will be
317 * cleared. Depending on complexity of ->release, consequences vary.
318 *
319 * We can't wait for mercy when close will be done for real, it's
320 * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
321 * by hand in remove_proc_entry(). For this, save opener's credentials
322 * for later.
323 */
05c0ae21 324 pdeo = kzalloc(sizeof(struct pde_opener), GFP_KERNEL);
881adb85
AD
325 if (!pdeo)
326 return -ENOMEM;
786d7e16 327
866ad9a7 328 if (!use_pde(pde)) {
881adb85 329 kfree(pdeo);
d2857e79 330 return -ENOENT;
786d7e16 331 }
786d7e16 332 open = pde->proc_fops->open;
881adb85 333 release = pde->proc_fops->release;
786d7e16
AD
334
335 if (open)
336 rv = open(inode, file);
337
881adb85
AD
338 if (rv == 0 && release) {
339 /* To know what to release. */
881adb85
AD
340 pdeo->file = file;
341 /* Strictly for "too late" ->release in proc_reg_release(). */
05c0ae21 342 spin_lock(&pde->pde_unload_lock);
881adb85 343 list_add(&pdeo->lh, &pde->pde_openers);
05c0ae21 344 spin_unlock(&pde->pde_unload_lock);
881adb85
AD
345 } else
346 kfree(pdeo);
05c0ae21
AV
347
348 unuse_pde(pde);
786d7e16
AD
349 return rv;
350}
351
352static int proc_reg_release(struct inode *inode, struct file *file)
353{
354 struct proc_dir_entry *pde = PDE(inode);
881adb85 355 struct pde_opener *pdeo;
786d7e16 356 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
357 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
358 if (pdeo->file == file) {
359 close_pdeo(pde, pdeo);
360 break;
361 }
881adb85 362 }
786d7e16 363 spin_unlock(&pde->pde_unload_lock);
ca469f35 364 return 0;
786d7e16
AD
365}
366
367static const struct file_operations proc_reg_file_ops = {
368 .llseek = proc_reg_llseek,
369 .read = proc_reg_read,
370 .write = proc_reg_write,
371 .poll = proc_reg_poll,
372 .unlocked_ioctl = proc_reg_unlocked_ioctl,
373#ifdef CONFIG_COMPAT
374 .compat_ioctl = proc_reg_compat_ioctl,
375#endif
376 .mmap = proc_reg_mmap,
c4fe2448 377 .get_unmapped_area = proc_reg_get_unmapped_area,
786d7e16
AD
378 .open = proc_reg_open,
379 .release = proc_reg_release,
380};
381
778f3dd5
DM
382#ifdef CONFIG_COMPAT
383static const struct file_operations proc_reg_file_ops_no_compat = {
384 .llseek = proc_reg_llseek,
385 .read = proc_reg_read,
386 .write = proc_reg_write,
387 .poll = proc_reg_poll,
388 .unlocked_ioctl = proc_reg_unlocked_ioctl,
389 .mmap = proc_reg_mmap,
c4fe2448 390 .get_unmapped_area = proc_reg_get_unmapped_area,
778f3dd5
DM
391 .open = proc_reg_open,
392 .release = proc_reg_release,
393};
394#endif
395
6e77137b 396static const char *proc_follow_link(struct dentry *dentry, void **cookie)
7e0e953b 397{
2b0143b5 398 struct proc_dir_entry *pde = PDE(d_inode(dentry));
7e0e953b
AV
399 if (unlikely(!use_pde(pde)))
400 return ERR_PTR(-EINVAL);
680baacb
AV
401 *cookie = pde;
402 return pde->data;
7e0e953b
AV
403}
404
5f2c4179 405static void proc_put_link(struct inode *unused, void *p)
7e0e953b
AV
406{
407 unuse_pde(p);
408}
409
410const struct inode_operations proc_link_inode_operations = {
411 .readlink = generic_readlink,
412 .follow_link = proc_follow_link,
413 .put_link = proc_put_link,
414};
415
6d1b6e4e 416struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
1da177e4 417{
51f0885e 418 struct inode *inode = new_inode_pseudo(sb);
1da177e4 419
51f0885e
LT
420 if (inode) {
421 inode->i_ino = de->low_ino;
a1d4aebb 422 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
a1d4aebb 423 PROC_I(inode)->pde = de;
5e971dce 424
eb6d38d5
EB
425 if (is_empty_pde(de)) {
426 make_empty_dir_inode(inode);
427 return inode;
428 }
5e971dce
AD
429 if (de->mode) {
430 inode->i_mode = de->mode;
431 inode->i_uid = de->uid;
432 inode->i_gid = de->gid;
433 }
434 if (de->size)
435 inode->i_size = de->size;
436 if (de->nlink)
bfe86848 437 set_nlink(inode, de->nlink);
b6cdc731
AV
438 WARN_ON(!de->proc_iops);
439 inode->i_op = de->proc_iops;
5e971dce
AD
440 if (de->proc_fops) {
441 if (S_ISREG(inode->i_mode)) {
778f3dd5 442#ifdef CONFIG_COMPAT
5e971dce
AD
443 if (!de->proc_fops->compat_ioctl)
444 inode->i_fop =
445 &proc_reg_file_ops_no_compat;
446 else
778f3dd5 447#endif
5e971dce
AD
448 inode->i_fop = &proc_reg_file_ops;
449 } else {
450 inode->i_fop = de->proc_fops;
778f3dd5 451 }
786d7e16 452 }
99b76233 453 } else
135d5655 454 pde_put(de);
1da177e4 455 return inode;
d3d009cb 456}
1da177e4 457
07543f5c 458int proc_fill_super(struct super_block *s)
1da177e4 459{
87e0aab3 460 struct inode *root_inode;
0097875b 461 int ret;
87e0aab3 462
92d03285 463 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
1da177e4
LT
464 s->s_blocksize = 1024;
465 s->s_blocksize_bits = 10;
466 s->s_magic = PROC_SUPER_MAGIC;
467 s->s_op = &proc_sops;
468 s->s_time_gran = 1;
469
135d5655 470 pde_get(&proc_root);
87e0aab3
MP
471 root_inode = proc_get_inode(s, &proc_root);
472 if (!root_inode) {
87ebdc00 473 pr_err("proc_fill_super: get root inode failed\n");
87e0aab3
MP
474 return -ENOMEM;
475 }
1da177e4 476
87e0aab3
MP
477 s->s_root = d_make_root(root_inode);
478 if (!s->s_root) {
87ebdc00 479 pr_err("proc_fill_super: allocate dentry failed\n");
87e0aab3
MP
480 return -ENOMEM;
481 }
482
0097875b
EB
483 ret = proc_setup_self(s);
484 if (ret) {
485 return ret;
486 }
487 return proc_setup_thread_self(s);
1da177e4 488}