Merge tag 's390-5.7-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-block.git] / fs / proc / inode.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/proc/inode.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
efb1a57d 8#include <linux/cache.h>
1da177e4
LT
9#include <linux/time.h>
10#include <linux/proc_fs.h>
11#include <linux/kernel.h>
97412950 12#include <linux/pid_namespace.h>
1da177e4
LT
13#include <linux/mm.h>
14#include <linux/string.h>
15#include <linux/stat.h>
786d7e16 16#include <linux/completion.h>
dd23aae4 17#include <linux/poll.h>
87ebdc00 18#include <linux/printk.h>
1da177e4
LT
19#include <linux/file.h>
20#include <linux/limits.h>
21#include <linux/init.h>
22#include <linux/module.h>
9043476f 23#include <linux/sysctl.h>
97412950 24#include <linux/seq_file.h>
5a0e3ad6 25#include <linux/slab.h>
97412950 26#include <linux/mount.h>
1da177e4 27
7c0f6ba6 28#include <linux/uaccess.h>
1da177e4 29
fee781e6 30#include "internal.h"
1da177e4 31
8267952b 32static void proc_evict_inode(struct inode *inode)
1da177e4
LT
33{
34 struct proc_dir_entry *de;
dfef6dcd 35 struct ctl_table_header *head;
71448011 36 struct proc_inode *ei = PROC_I(inode);
1da177e4 37
91b0abe3 38 truncate_inode_pages_final(&inode->i_data);
dbd5768f 39 clear_inode(inode);
fef26658 40
99f89551 41 /* Stop tracking associated processes */
71448011 42 if (ei->pid) {
7bc3e6e5 43 proc_pid_evict_inode(ei);
71448011
EB
44 ei->pid = NULL;
45 }
1da177e4
LT
46
47 /* Let go of any associated proc directory entry */
71448011
EB
48 de = ei->pde;
49 if (de) {
135d5655 50 pde_put(de);
71448011
EB
51 ei->pde = NULL;
52 }
d6cffbbe 53
71448011 54 head = ei->sysctl;
dfef6dcd 55 if (head) {
71448011 56 RCU_INIT_POINTER(ei->sysctl, NULL);
d6cffbbe 57 proc_sys_evict_inode(inode, head);
dfef6dcd 58 }
1da177e4
LT
59}
60
efb1a57d 61static struct kmem_cache *proc_inode_cachep __ro_after_init;
195b8cf0 62static struct kmem_cache *pde_opener_cache __ro_after_init;
1da177e4
LT
63
64static struct inode *proc_alloc_inode(struct super_block *sb)
65{
66 struct proc_inode *ei;
1da177e4 67
f245e1c1 68 ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
1da177e4
LT
69 if (!ei)
70 return NULL;
13b41b09 71 ei->pid = NULL;
aed7a6c4 72 ei->fd = 0;
1da177e4
LT
73 ei->op.proc_get_link = NULL;
74 ei->pde = NULL;
9043476f
AV
75 ei->sysctl = NULL;
76 ei->sysctl_entry = NULL;
0afa5ca8 77 INIT_HLIST_NODE(&ei->sibling_inodes);
3d3d35b1 78 ei->ns_ops = NULL;
230f72e9 79 return &ei->vfs_inode;
1da177e4
LT
80}
81
4aa6b55c 82static void proc_free_inode(struct inode *inode)
1da177e4
LT
83{
84 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
85}
86
51cc5068 87static void init_once(void *foo)
1da177e4
LT
88{
89 struct proc_inode *ei = (struct proc_inode *) foo;
90
a35afb83 91 inode_init_once(&ei->vfs_inode);
1da177e4 92}
20c2df83 93
195b8cf0 94void __init proc_init_kmemcache(void)
1da177e4
LT
95{
96 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
97 sizeof(struct proc_inode),
fffb60f9 98 0, (SLAB_RECLAIM_ACCOUNT|
5d097056
VD
99 SLAB_MEM_SPREAD|SLAB_ACCOUNT|
100 SLAB_PANIC),
20c2df83 101 init_once);
195b8cf0
AD
102 pde_opener_cache =
103 kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
2acddbe8 104 SLAB_ACCOUNT|SLAB_PANIC, NULL);
b4884f23 105 proc_dir_entry_cache = kmem_cache_create_usercopy(
2d6e4e82
AD
106 "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
107 offsetof(struct proc_dir_entry, inline_name),
108 SIZEOF_PDE_INLINE_NAME, NULL);
109 BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
1da177e4
LT
110}
111
f90f3caf 112void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
26dbc60f
EB
113{
114 struct inode *inode;
115 struct proc_inode *ei;
116 struct hlist_node *node;
080f6276 117 struct super_block *old_sb = NULL;
26dbc60f
EB
118
119 rcu_read_lock();
120 for (;;) {
080f6276 121 struct super_block *sb;
26dbc60f
EB
122 node = hlist_first_rcu(inodes);
123 if (!node)
124 break;
125 ei = hlist_entry(node, struct proc_inode, sibling_inodes);
126 spin_lock(lock);
127 hlist_del_init_rcu(&ei->sibling_inodes);
128 spin_unlock(lock);
129
130 inode = &ei->vfs_inode;
131 sb = inode->i_sb;
080f6276 132 if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
26dbc60f
EB
133 continue;
134 inode = igrab(inode);
135 rcu_read_unlock();
080f6276
EB
136 if (sb != old_sb) {
137 if (old_sb)
138 deactivate_super(old_sb);
139 old_sb = sb;
140 }
26dbc60f 141 if (unlikely(!inode)) {
26dbc60f
EB
142 rcu_read_lock();
143 continue;
144 }
145
f90f3caf
EB
146 if (S_ISDIR(inode->i_mode)) {
147 struct dentry *dir = d_find_any_alias(inode);
148 if (dir) {
149 d_invalidate(dir);
150 dput(dir);
151 }
152 } else {
153 struct dentry *dentry;
154 while ((dentry = d_find_alias(inode))) {
155 d_invalidate(dentry);
156 dput(dentry);
157 }
158 }
26dbc60f 159 iput(inode);
26dbc60f
EB
160
161 rcu_read_lock();
162 }
163 rcu_read_unlock();
080f6276
EB
164 if (old_sb)
165 deactivate_super(old_sb);
26dbc60f
EB
166}
167
97412950
VK
168static int proc_show_options(struct seq_file *seq, struct dentry *root)
169{
0499680a
VK
170 struct super_block *sb = root->d_sb;
171 struct pid_namespace *pid = sb->s_fs_info;
172
dcb0f222
EB
173 if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
174 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
796f571b 175 if (pid->hide_pid != HIDEPID_OFF)
0499680a
VK
176 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
177
97412950
VK
178 return 0;
179}
180
60a3c3a5 181const struct super_operations proc_sops = {
1da177e4 182 .alloc_inode = proc_alloc_inode,
4aa6b55c 183 .free_inode = proc_free_inode,
1da177e4 184 .drop_inode = generic_delete_inode,
8267952b 185 .evict_inode = proc_evict_inode,
1da177e4 186 .statfs = simple_statfs,
97412950 187 .show_options = proc_show_options,
1da177e4
LT
188};
189
866ad9a7
AV
190enum {BIAS = -1U<<31};
191
192static inline int use_pde(struct proc_dir_entry *pde)
193{
15b158b4 194 return likely(atomic_inc_unless_negative(&pde->in_use));
881adb85
AD
195}
196
866ad9a7 197static void unuse_pde(struct proc_dir_entry *pde)
881adb85 198{
15b158b4 199 if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
05c0ae21 200 complete(pde->pde_unload_completion);
786d7e16
AD
201}
202
2f897424 203/* pde is locked on entry, unlocked on exit */
ca469f35
AV
204static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
205{
492b2da6
AD
206 /*
207 * close() (proc_reg_release()) can't delete an entry and proceed:
208 * ->release hook needs to be available at the right moment.
209 *
210 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
211 * "struct file" needs to be available at the right moment.
212 *
213 * Therefore, first process to enter this function does ->release() and
214 * signals its completion to the other process which does nothing.
215 */
05c0ae21 216 if (pdeo->closing) {
ca469f35 217 /* somebody else is doing that, just wait */
05c0ae21
AV
218 DECLARE_COMPLETION_ONSTACK(c);
219 pdeo->c = &c;
ca469f35 220 spin_unlock(&pde->pde_unload_lock);
05c0ae21 221 wait_for_completion(&c);
ca469f35
AV
222 } else {
223 struct file *file;
2f897424
AD
224 struct completion *c;
225
f5887c71 226 pdeo->closing = true;
ca469f35
AV
227 spin_unlock(&pde->pde_unload_lock);
228 file = pdeo->file;
d56c0d45 229 pde->proc_ops->proc_release(file_inode(file), file);
ca469f35 230 spin_lock(&pde->pde_unload_lock);
492b2da6 231 /* After ->release. */
06a0c417 232 list_del(&pdeo->lh);
2f897424
AD
233 c = pdeo->c;
234 spin_unlock(&pde->pde_unload_lock);
235 if (unlikely(c))
236 complete(c);
195b8cf0 237 kmem_cache_free(pde_opener_cache, pdeo);
05c0ae21 238 }
ca469f35
AV
239}
240
866ad9a7 241void proc_entry_rundown(struct proc_dir_entry *de)
786d7e16 242{
05c0ae21 243 DECLARE_COMPLETION_ONSTACK(c);
866ad9a7 244 /* Wait until all existing callers into module are done. */
05c0ae21
AV
245 de->pde_unload_completion = &c;
246 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
247 wait_for_completion(&c);
786d7e16 248
492b2da6
AD
249 /* ->pde_openers list can't grow from now on. */
250
05c0ae21 251 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
252 while (!list_empty(&de->pde_openers)) {
253 struct pde_opener *pdeo;
866ad9a7 254 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
ca469f35 255 close_pdeo(de, pdeo);
2f897424 256 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
257 }
258 spin_unlock(&de->pde_unload_lock);
786d7e16
AD
259}
260
866ad9a7
AV
261static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
262{
263 struct proc_dir_entry *pde = PDE(file_inode(file));
264 loff_t rv = -EINVAL;
265 if (use_pde(pde)) {
d56c0d45 266 typeof_member(struct proc_ops, proc_lseek) lseek;
9af27b28 267
d56c0d45
AD
268 lseek = pde->proc_ops->proc_lseek;
269 if (!lseek)
270 lseek = default_llseek;
271 rv = lseek(file, offset, whence);
866ad9a7
AV
272 unuse_pde(pde);
273 }
786d7e16
AD
274 return rv;
275}
276
866ad9a7 277static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
786d7e16 278{
496ad9aa 279 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 280 ssize_t rv = -EIO;
866ad9a7 281 if (use_pde(pde)) {
d56c0d45 282 typeof_member(struct proc_ops, proc_read) read;
9af27b28 283
d56c0d45 284 read = pde->proc_ops->proc_read;
866ad9a7
AV
285 if (read)
286 rv = read(file, buf, count, ppos);
287 unuse_pde(pde);
786d7e16 288 }
866ad9a7
AV
289 return rv;
290}
786d7e16 291
866ad9a7
AV
292static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
293{
866ad9a7
AV
294 struct proc_dir_entry *pde = PDE(file_inode(file));
295 ssize_t rv = -EIO;
296 if (use_pde(pde)) {
d56c0d45 297 typeof_member(struct proc_ops, proc_write) write;
9af27b28 298
d56c0d45 299 write = pde->proc_ops->proc_write;
866ad9a7
AV
300 if (write)
301 rv = write(file, buf, count, ppos);
302 unuse_pde(pde);
303 }
786d7e16
AD
304 return rv;
305}
306
076ccb76 307static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
786d7e16 308{
496ad9aa 309 struct proc_dir_entry *pde = PDE(file_inode(file));
e6c8adca 310 __poll_t rv = DEFAULT_POLLMASK;
866ad9a7 311 if (use_pde(pde)) {
d56c0d45 312 typeof_member(struct proc_ops, proc_poll) poll;
9af27b28 313
d56c0d45 314 poll = pde->proc_ops->proc_poll;
866ad9a7
AV
315 if (poll)
316 rv = poll(file, pts);
317 unuse_pde(pde);
786d7e16 318 }
786d7e16
AD
319 return rv;
320}
321
322static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
323{
496ad9aa 324 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 325 long rv = -ENOTTY;
866ad9a7 326 if (use_pde(pde)) {
d56c0d45 327 typeof_member(struct proc_ops, proc_ioctl) ioctl;
9af27b28 328
d56c0d45 329 ioctl = pde->proc_ops->proc_ioctl;
866ad9a7
AV
330 if (ioctl)
331 rv = ioctl(file, cmd, arg);
332 unuse_pde(pde);
786d7e16 333 }
786d7e16
AD
334 return rv;
335}
336
337#ifdef CONFIG_COMPAT
338static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
339{
496ad9aa 340 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 341 long rv = -ENOTTY;
866ad9a7 342 if (use_pde(pde)) {
d56c0d45 343 typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
9af27b28 344
d56c0d45 345 compat_ioctl = pde->proc_ops->proc_compat_ioctl;
866ad9a7
AV
346 if (compat_ioctl)
347 rv = compat_ioctl(file, cmd, arg);
348 unuse_pde(pde);
786d7e16 349 }
786d7e16
AD
350 return rv;
351}
352#endif
353
354static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
355{
496ad9aa 356 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 357 int rv = -EIO;
866ad9a7 358 if (use_pde(pde)) {
d56c0d45 359 typeof_member(struct proc_ops, proc_mmap) mmap;
9af27b28 360
d56c0d45 361 mmap = pde->proc_ops->proc_mmap;
866ad9a7
AV
362 if (mmap)
363 rv = mmap(file, vma);
364 unuse_pde(pde);
786d7e16 365 }
786d7e16
AD
366 return rv;
367}
368
5721cf84
HD
369static unsigned long
370proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
371 unsigned long len, unsigned long pgoff,
372 unsigned long flags)
c4fe2448
AD
373{
374 struct proc_dir_entry *pde = PDE(file_inode(file));
2cbe3b0a 375 unsigned long rv = -EIO;
ae5758a1 376
c4fe2448 377 if (use_pde(pde)) {
d56c0d45 378 typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
ae5758a1 379
d56c0d45 380 get_area = pde->proc_ops->proc_get_unmapped_area;
fad1a86e 381#ifdef CONFIG_MMU
ae5758a1
JB
382 if (!get_area)
383 get_area = current->mm->get_unmapped_area;
fad1a86e 384#endif
ae5758a1 385
5721cf84
HD
386 if (get_area)
387 rv = get_area(file, orig_addr, len, pgoff, flags);
ae5758a1
JB
388 else
389 rv = orig_addr;
c4fe2448
AD
390 unuse_pde(pde);
391 }
392 return rv;
393}
394
786d7e16
AD
395static int proc_reg_open(struct inode *inode, struct file *file)
396{
397 struct proc_dir_entry *pde = PDE(inode);
398 int rv = 0;
d56c0d45
AD
399 typeof_member(struct proc_ops, proc_open) open;
400 typeof_member(struct proc_ops, proc_release) release;
881adb85
AD
401 struct pde_opener *pdeo;
402
403 /*
492b2da6
AD
404 * Ensure that
405 * 1) PDE's ->release hook will be called no matter what
406 * either normally by close()/->release, or forcefully by
407 * rmmod/remove_proc_entry.
408 *
409 * 2) rmmod isn't blocked by opening file in /proc and sitting on
410 * the descriptor (including "rmmod foo </proc/foo" scenario).
881adb85 411 *
492b2da6 412 * Save every "struct file" with custom ->release hook.
881adb85 413 */
e7a6e291 414 if (!use_pde(pde))
d2857e79 415 return -ENOENT;
e7a6e291 416
d56c0d45 417 release = pde->proc_ops->proc_release;
e7a6e291 418 if (release) {
195b8cf0 419 pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
e7a6e291
AD
420 if (!pdeo) {
421 rv = -ENOMEM;
422 goto out_unuse;
423 }
424 }
786d7e16 425
d56c0d45 426 open = pde->proc_ops->proc_open;
786d7e16
AD
427 if (open)
428 rv = open(inode, file);
429
e7a6e291
AD
430 if (release) {
431 if (rv == 0) {
432 /* To know what to release. */
433 pdeo->file = file;
434 pdeo->closing = false;
435 pdeo->c = NULL;
436 spin_lock(&pde->pde_unload_lock);
437 list_add(&pdeo->lh, &pde->pde_openers);
438 spin_unlock(&pde->pde_unload_lock);
439 } else
195b8cf0 440 kmem_cache_free(pde_opener_cache, pdeo);
e7a6e291 441 }
05c0ae21 442
e7a6e291 443out_unuse:
05c0ae21 444 unuse_pde(pde);
786d7e16
AD
445 return rv;
446}
447
448static int proc_reg_release(struct inode *inode, struct file *file)
449{
450 struct proc_dir_entry *pde = PDE(inode);
881adb85 451 struct pde_opener *pdeo;
786d7e16 452 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
453 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
454 if (pdeo->file == file) {
455 close_pdeo(pde, pdeo);
2f897424 456 return 0;
ca469f35 457 }
881adb85 458 }
786d7e16 459 spin_unlock(&pde->pde_unload_lock);
ca469f35 460 return 0;
786d7e16
AD
461}
462
463static const struct file_operations proc_reg_file_ops = {
464 .llseek = proc_reg_llseek,
465 .read = proc_reg_read,
466 .write = proc_reg_write,
467 .poll = proc_reg_poll,
468 .unlocked_ioctl = proc_reg_unlocked_ioctl,
469#ifdef CONFIG_COMPAT
470 .compat_ioctl = proc_reg_compat_ioctl,
471#endif
472 .mmap = proc_reg_mmap,
c4fe2448 473 .get_unmapped_area = proc_reg_get_unmapped_area,
786d7e16
AD
474 .open = proc_reg_open,
475 .release = proc_reg_release,
476};
477
778f3dd5
DM
478#ifdef CONFIG_COMPAT
479static const struct file_operations proc_reg_file_ops_no_compat = {
480 .llseek = proc_reg_llseek,
481 .read = proc_reg_read,
482 .write = proc_reg_write,
483 .poll = proc_reg_poll,
484 .unlocked_ioctl = proc_reg_unlocked_ioctl,
485 .mmap = proc_reg_mmap,
c4fe2448 486 .get_unmapped_area = proc_reg_get_unmapped_area,
778f3dd5
DM
487 .open = proc_reg_open,
488 .release = proc_reg_release,
489};
490#endif
491
fceef393
AV
492static void proc_put_link(void *p)
493{
494 unuse_pde(p);
495}
496
6b255391 497static const char *proc_get_link(struct dentry *dentry,
fceef393
AV
498 struct inode *inode,
499 struct delayed_call *done)
7e0e953b 500{
6b255391 501 struct proc_dir_entry *pde = PDE(inode);
15b158b4 502 if (!use_pde(pde))
7e0e953b 503 return ERR_PTR(-EINVAL);
fceef393 504 set_delayed_call(done, proc_put_link, pde);
680baacb 505 return pde->data;
7e0e953b
AV
506}
507
7e0e953b 508const struct inode_operations proc_link_inode_operations = {
6b255391 509 .get_link = proc_get_link,
7e0e953b
AV
510};
511
6d1b6e4e 512struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
1da177e4 513{
51f0885e 514 struct inode *inode = new_inode_pseudo(sb);
1da177e4 515
51f0885e
LT
516 if (inode) {
517 inode->i_ino = de->low_ino;
078cd827 518 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
a1d4aebb 519 PROC_I(inode)->pde = de;
5e971dce 520
eb6d38d5
EB
521 if (is_empty_pde(de)) {
522 make_empty_dir_inode(inode);
523 return inode;
524 }
5e971dce
AD
525 if (de->mode) {
526 inode->i_mode = de->mode;
527 inode->i_uid = de->uid;
528 inode->i_gid = de->gid;
529 }
530 if (de->size)
531 inode->i_size = de->size;
532 if (de->nlink)
bfe86848 533 set_nlink(inode, de->nlink);
d56c0d45
AD
534
535 if (S_ISREG(inode->i_mode)) {
536 inode->i_op = de->proc_iops;
537 inode->i_fop = &proc_reg_file_ops;
778f3dd5 538#ifdef CONFIG_COMPAT
d56c0d45
AD
539 if (!de->proc_ops->proc_compat_ioctl) {
540 inode->i_fop = &proc_reg_file_ops_no_compat;
778f3dd5 541 }
d56c0d45
AD
542#endif
543 } else if (S_ISDIR(inode->i_mode)) {
544 inode->i_op = de->proc_iops;
545 inode->i_fop = de->proc_dir_ops;
546 } else if (S_ISLNK(inode->i_mode)) {
547 inode->i_op = de->proc_iops;
548 inode->i_fop = NULL;
549 } else
550 BUG();
99b76233 551 } else
135d5655 552 pde_put(de);
1da177e4 553 return inode;
d3d009cb 554}