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