sched/headers: Prepare to remove <linux/cred.h> inclusion from <linux/sched.h>
[linux-block.git] / fs / proc / root.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/proc/root.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc root directory handling functions
7 */
8
7c0f6ba6 9#include <linux/uaccess.h>
1da177e4
LT
10
11#include <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
1da177e4 15#include <linux/init.h>
914e2637 16#include <linux/sched.h>
1da177e4
LT
17#include <linux/module.h>
18#include <linux/bitops.h>
87a8ebd6 19#include <linux/user_namespace.h>
f6c7a1f3 20#include <linux/mount.h>
07543f5c 21#include <linux/pid_namespace.h>
97412950 22#include <linux/parser.h>
5b825c3a 23#include <linux/cred.h>
1da177e4 24
fee781e6
AB
25#include "internal.h"
26
97412950 27enum {
0499680a 28 Opt_gid, Opt_hidepid, Opt_err,
97412950
VK
29};
30
31static const match_table_t tokens = {
0499680a
VK
32 {Opt_hidepid, "hidepid=%u"},
33 {Opt_gid, "gid=%u"},
97412950
VK
34 {Opt_err, NULL},
35};
36
e94591d0 37int proc_parse_options(char *options, struct pid_namespace *pid)
97412950
VK
38{
39 char *p;
40 substring_t args[MAX_OPT_ARGS];
0499680a 41 int option;
97412950
VK
42
43 if (!options)
44 return 1;
45
46 while ((p = strsep(&options, ",")) != NULL) {
47 int token;
48 if (!*p)
49 continue;
50
9fb88442 51 args[0].to = args[0].from = NULL;
97412950
VK
52 token = match_token(p, tokens, args);
53 switch (token) {
0499680a
VK
54 case Opt_gid:
55 if (match_int(&args[0], &option))
56 return 0;
dcb0f222 57 pid->pid_gid = make_kgid(current_user_ns(), option);
0499680a
VK
58 break;
59 case Opt_hidepid:
60 if (match_int(&args[0], &option))
61 return 0;
796f571b
LW
62 if (option < HIDEPID_OFF ||
63 option > HIDEPID_INVISIBLE) {
0499680a
VK
64 pr_err("proc: hidepid value must be between 0 and 2.\n");
65 return 0;
66 }
67 pid->hide_pid = option;
68 break;
97412950
VK
69 default:
70 pr_err("proc: unrecognized mount option \"%s\" "
71 "or missing value\n", p);
72 return 0;
73 }
74 }
75
76 return 1;
77}
78
79int proc_remount(struct super_block *sb, int *flags, char *data)
80{
81 struct pid_namespace *pid = sb->s_fs_info;
02b9984d
TT
82
83 sync_filesystem(sb);
97412950
VK
84 return !proc_parse_options(data, pid);
85}
86
aed1d84f
AV
87static struct dentry *proc_mount(struct file_system_type *fs_type,
88 int flags, const char *dev_name, void *data)
1da177e4 89{
07543f5c 90 struct pid_namespace *ns;
07543f5c 91
97412950 92 if (flags & MS_KERNMOUNT) {
e94591d0
EB
93 ns = data;
94 data = NULL;
97412950 95 } else {
17cf22c3 96 ns = task_active_pid_ns(current);
07543f5c
PE
97 }
98
e94591d0 99 return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
07543f5c
PE
100}
101
102static void proc_kill_sb(struct super_block *sb)
103{
104 struct pid_namespace *ns;
105
106 ns = (struct pid_namespace *)sb->s_fs_info;
021ada7d
AV
107 if (ns->proc_self)
108 dput(ns->proc_self);
0097875b
EB
109 if (ns->proc_thread_self)
110 dput(ns->proc_thread_self);
07543f5c
PE
111 kill_anon_super(sb);
112 put_pid_ns(ns);
1da177e4
LT
113}
114
c2319540 115static struct file_system_type proc_fs_type = {
1da177e4 116 .name = "proc",
aed1d84f 117 .mount = proc_mount,
07543f5c 118 .kill_sb = proc_kill_sb,
8654df4e 119 .fs_flags = FS_USERNS_MOUNT,
1da177e4
LT
120};
121
1da177e4
LT
122void __init proc_root_init(void)
123{
5bcd7ff9
AD
124 int err;
125
126 proc_init_inodecache();
1270dd8d 127 set_proc_pid_nlink();
1da177e4
LT
128 err = register_filesystem(&proc_fs_type);
129 if (err)
130 return;
07543f5c 131
e656d8a6 132 proc_self_init();
0097875b 133 proc_thread_self_init();
155134fe 134 proc_symlink("mounts", NULL, "self/mounts");
457c4cbc
EB
135
136 proc_net_init();
1da177e4
LT
137
138#ifdef CONFIG_SYSVIPC
139 proc_mkdir("sysvipc", NULL);
1da177e4 140#endif
36a5aeb8 141 proc_mkdir("fs", NULL);
928b4d8c 142 proc_mkdir("driver", NULL);
eb6d38d5 143 proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
1da177e4
LT
144#if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
145 /* just give it a mountpoint */
eb6d38d5 146 proc_create_mount_point("openprom");
1da177e4
LT
147#endif
148 proc_tty_init();
9c37066d 149 proc_mkdir("bus", NULL);
77b14db5 150 proc_sys_init();
1da177e4
LT
151}
152
76b6159b
AV
153static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
154)
1da177e4 155{
2b0143b5 156 generic_fillattr(d_inode(dentry), stat);
76b6159b
AV
157 stat->nlink = proc_root.nlink + nr_processes();
158 return 0;
159}
1da177e4 160
00cd8dd3 161static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
76b6159b 162{
335eb531 163 if (!proc_pid_lookup(dir, dentry, flags))
1da177e4 164 return NULL;
1da177e4 165
335eb531 166 return proc_lookup(dir, dentry, flags);
1da177e4
LT
167}
168
f0c3b509 169static int proc_root_readdir(struct file *file, struct dir_context *ctx)
1da177e4 170{
f0c3b509 171 if (ctx->pos < FIRST_PROCESS_ENTRY) {
94fc5d9d
RG
172 int error = proc_readdir(file, ctx);
173 if (unlikely(error <= 0))
174 return error;
f0c3b509 175 ctx->pos = FIRST_PROCESS_ENTRY;
1da177e4 176 }
1da177e4 177
f0c3b509 178 return proc_pid_readdir(file, ctx);
1da177e4
LT
179}
180
181/*
182 * The root /proc directory is special, as it has the
183 * <pid> directories. Thus we don't use the generic
184 * directory handling functions for that..
185 */
00977a59 186static const struct file_operations proc_root_operations = {
1da177e4 187 .read = generic_read_dir,
f50752ea
AV
188 .iterate_shared = proc_root_readdir,
189 .llseek = generic_file_llseek,
1da177e4
LT
190};
191
192/*
193 * proc root can do almost nothing..
194 */
c5ef1c42 195static const struct inode_operations proc_root_inode_operations = {
1da177e4 196 .lookup = proc_root_lookup,
76b6159b 197 .getattr = proc_root_getattr,
1da177e4
LT
198};
199
200/*
201 * This is the root "inode" in the /proc tree..
202 */
203struct proc_dir_entry proc_root = {
204 .low_ino = PROC_ROOT_INO,
205 .namelen = 5,
1da177e4
LT
206 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
207 .nlink = 2,
5a622f2d 208 .count = ATOMIC_INIT(1),
1da177e4
LT
209 .proc_iops = &proc_root_inode_operations,
210 .proc_fops = &proc_root_operations,
211 .parent = &proc_root,
710585d4 212 .subdir = RB_ROOT,
09570f91 213 .name = "/proc",
1da177e4
LT
214};
215
6f4e6433
PE
216int pid_ns_prepare_proc(struct pid_namespace *ns)
217{
218 struct vfsmount *mnt;
219
220 mnt = kern_mount_data(&proc_fs_type, ns);
221 if (IS_ERR(mnt))
222 return PTR_ERR(mnt);
223
579441a3 224 ns->proc_mnt = mnt;
6f4e6433
PE
225 return 0;
226}
227
228void pid_ns_release_proc(struct pid_namespace *ns)
229{
905ad269 230 kern_unmount(ns->proc_mnt);
6f4e6433 231}