Commit | Line | Data |
---|---|---|
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 | ||
9 | #include <asm/uaccess.h> | |
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> | |
f6c7a1f3 | 19 | #include <linux/mount.h> |
07543f5c | 20 | #include <linux/pid_namespace.h> |
97412950 | 21 | #include <linux/parser.h> |
1da177e4 | 22 | |
fee781e6 AB |
23 | #include "internal.h" |
24 | ||
07543f5c PE |
25 | static int proc_test_super(struct super_block *sb, void *data) |
26 | { | |
27 | return sb->s_fs_info == data; | |
28 | } | |
29 | ||
30 | static int proc_set_super(struct super_block *sb, void *data) | |
31 | { | |
ff78fca2 AV |
32 | int err = set_anon_super(sb, NULL); |
33 | if (!err) { | |
34 | struct pid_namespace *ns = (struct pid_namespace *)data; | |
35 | sb->s_fs_info = get_pid_ns(ns); | |
36 | } | |
37 | return err; | |
07543f5c PE |
38 | } |
39 | ||
97412950 VK |
40 | enum { |
41 | Opt_err, | |
42 | }; | |
43 | ||
44 | static const match_table_t tokens = { | |
45 | {Opt_err, NULL}, | |
46 | }; | |
47 | ||
48 | static int proc_parse_options(char *options, struct pid_namespace *pid) | |
49 | { | |
50 | char *p; | |
51 | substring_t args[MAX_OPT_ARGS]; | |
52 | ||
53 | pr_debug("proc: options = %s\n", options); | |
54 | ||
55 | if (!options) | |
56 | return 1; | |
57 | ||
58 | while ((p = strsep(&options, ",")) != NULL) { | |
59 | int token; | |
60 | if (!*p) | |
61 | continue; | |
62 | ||
63 | args[0].to = args[0].from = 0; | |
64 | token = match_token(p, tokens, args); | |
65 | switch (token) { | |
66 | default: | |
67 | pr_err("proc: unrecognized mount option \"%s\" " | |
68 | "or missing value\n", p); | |
69 | return 0; | |
70 | } | |
71 | } | |
72 | ||
73 | return 1; | |
74 | } | |
75 | ||
76 | int proc_remount(struct super_block *sb, int *flags, char *data) | |
77 | { | |
78 | struct pid_namespace *pid = sb->s_fs_info; | |
79 | return !proc_parse_options(data, pid); | |
80 | } | |
81 | ||
aed1d84f AV |
82 | static struct dentry *proc_mount(struct file_system_type *fs_type, |
83 | int flags, const char *dev_name, void *data) | |
1da177e4 | 84 | { |
07543f5c PE |
85 | int err; |
86 | struct super_block *sb; | |
87 | struct pid_namespace *ns; | |
88 | struct proc_inode *ei; | |
97412950 | 89 | char *options; |
07543f5c | 90 | |
97412950 | 91 | if (flags & MS_KERNMOUNT) { |
07543f5c | 92 | ns = (struct pid_namespace *)data; |
97412950 VK |
93 | options = NULL; |
94 | } else { | |
07543f5c | 95 | ns = current->nsproxy->pid_ns; |
97412950 VK |
96 | options = data; |
97 | } | |
07543f5c PE |
98 | |
99 | sb = sget(fs_type, proc_test_super, proc_set_super, ns); | |
100 | if (IS_ERR(sb)) | |
aed1d84f | 101 | return ERR_CAST(sb); |
07543f5c PE |
102 | |
103 | if (!sb->s_root) { | |
104 | sb->s_flags = flags; | |
97412950 VK |
105 | if (!proc_parse_options(options, ns)) { |
106 | deactivate_locked_super(sb); | |
107 | return ERR_PTR(-EINVAL); | |
108 | } | |
07543f5c PE |
109 | err = proc_fill_super(sb); |
110 | if (err) { | |
6f5bbff9 | 111 | deactivate_locked_super(sb); |
aed1d84f | 112 | return ERR_PTR(err); |
07543f5c PE |
113 | } |
114 | ||
07543f5c | 115 | sb->s_flags |= MS_ACTIVE; |
07543f5c PE |
116 | } |
117 | ||
4308eebb EB |
118 | ei = PROC_I(sb->s_root->d_inode); |
119 | if (!ei->pid) { | |
120 | rcu_read_lock(); | |
121 | ei->pid = get_pid(find_pid_ns(1, ns)); | |
122 | rcu_read_unlock(); | |
123 | } | |
124 | ||
aed1d84f | 125 | return dget(sb->s_root); |
07543f5c PE |
126 | } |
127 | ||
128 | static void proc_kill_sb(struct super_block *sb) | |
129 | { | |
130 | struct pid_namespace *ns; | |
131 | ||
132 | ns = (struct pid_namespace *)sb->s_fs_info; | |
133 | kill_anon_super(sb); | |
134 | put_pid_ns(ns); | |
1da177e4 LT |
135 | } |
136 | ||
c2319540 | 137 | static struct file_system_type proc_fs_type = { |
1da177e4 | 138 | .name = "proc", |
aed1d84f | 139 | .mount = proc_mount, |
07543f5c | 140 | .kill_sb = proc_kill_sb, |
1da177e4 LT |
141 | }; |
142 | ||
1da177e4 LT |
143 | void __init proc_root_init(void) |
144 | { | |
5bcd7ff9 AD |
145 | int err; |
146 | ||
147 | proc_init_inodecache(); | |
1da177e4 LT |
148 | err = register_filesystem(&proc_fs_type); |
149 | if (err) | |
150 | return; | |
905ad269 AV |
151 | err = pid_ns_prepare_proc(&init_pid_ns); |
152 | if (err) { | |
1da177e4 LT |
153 | unregister_filesystem(&proc_fs_type); |
154 | return; | |
155 | } | |
07543f5c | 156 | |
59c7572e | 157 | proc_symlink("mounts", NULL, "self/mounts"); |
457c4cbc EB |
158 | |
159 | proc_net_init(); | |
1da177e4 LT |
160 | |
161 | #ifdef CONFIG_SYSVIPC | |
162 | proc_mkdir("sysvipc", NULL); | |
1da177e4 | 163 | #endif |
36a5aeb8 | 164 | proc_mkdir("fs", NULL); |
928b4d8c | 165 | proc_mkdir("driver", NULL); |
1da177e4 LT |
166 | proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */ |
167 | #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE) | |
168 | /* just give it a mountpoint */ | |
169 | proc_mkdir("openprom", NULL); | |
170 | #endif | |
171 | proc_tty_init(); | |
172 | #ifdef CONFIG_PROC_DEVICETREE | |
173 | proc_device_tree_init(); | |
174 | #endif | |
9c37066d | 175 | proc_mkdir("bus", NULL); |
77b14db5 | 176 | proc_sys_init(); |
1da177e4 LT |
177 | } |
178 | ||
76b6159b AV |
179 | static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat |
180 | ) | |
1da177e4 | 181 | { |
76b6159b AV |
182 | generic_fillattr(dentry->d_inode, stat); |
183 | stat->nlink = proc_root.nlink + nr_processes(); | |
184 | return 0; | |
185 | } | |
1da177e4 | 186 | |
76b6159b AV |
187 | static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd) |
188 | { | |
1da177e4 LT |
189 | if (!proc_lookup(dir, dentry, nd)) { |
190 | return NULL; | |
191 | } | |
192 | ||
193 | return proc_pid_lookup(dir, dentry, nd); | |
194 | } | |
195 | ||
196 | static int proc_root_readdir(struct file * filp, | |
197 | void * dirent, filldir_t filldir) | |
198 | { | |
199 | unsigned int nr = filp->f_pos; | |
200 | int ret; | |
201 | ||
1da177e4 LT |
202 | if (nr < FIRST_PROCESS_ENTRY) { |
203 | int error = proc_readdir(filp, dirent, filldir); | |
b4df2b92 | 204 | if (error <= 0) |
1da177e4 | 205 | return error; |
1da177e4 LT |
206 | filp->f_pos = FIRST_PROCESS_ENTRY; |
207 | } | |
1da177e4 LT |
208 | |
209 | ret = proc_pid_readdir(filp, dirent, filldir); | |
210 | return ret; | |
211 | } | |
212 | ||
213 | /* | |
214 | * The root /proc directory is special, as it has the | |
215 | * <pid> directories. Thus we don't use the generic | |
216 | * directory handling functions for that.. | |
217 | */ | |
00977a59 | 218 | static const struct file_operations proc_root_operations = { |
1da177e4 LT |
219 | .read = generic_read_dir, |
220 | .readdir = proc_root_readdir, | |
6038f373 | 221 | .llseek = default_llseek, |
1da177e4 LT |
222 | }; |
223 | ||
224 | /* | |
225 | * proc root can do almost nothing.. | |
226 | */ | |
c5ef1c42 | 227 | static const struct inode_operations proc_root_inode_operations = { |
1da177e4 | 228 | .lookup = proc_root_lookup, |
76b6159b | 229 | .getattr = proc_root_getattr, |
1da177e4 LT |
230 | }; |
231 | ||
232 | /* | |
233 | * This is the root "inode" in the /proc tree.. | |
234 | */ | |
235 | struct proc_dir_entry proc_root = { | |
236 | .low_ino = PROC_ROOT_INO, | |
237 | .namelen = 5, | |
1da177e4 LT |
238 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
239 | .nlink = 2, | |
5a622f2d | 240 | .count = ATOMIC_INIT(1), |
1da177e4 LT |
241 | .proc_iops = &proc_root_inode_operations, |
242 | .proc_fops = &proc_root_operations, | |
243 | .parent = &proc_root, | |
09570f91 | 244 | .name = "/proc", |
1da177e4 LT |
245 | }; |
246 | ||
6f4e6433 PE |
247 | int pid_ns_prepare_proc(struct pid_namespace *ns) |
248 | { | |
249 | struct vfsmount *mnt; | |
250 | ||
251 | mnt = kern_mount_data(&proc_fs_type, ns); | |
252 | if (IS_ERR(mnt)) | |
253 | return PTR_ERR(mnt); | |
254 | ||
579441a3 | 255 | ns->proc_mnt = mnt; |
6f4e6433 PE |
256 | return 0; |
257 | } | |
258 | ||
259 | void pid_ns_release_proc(struct pid_namespace *ns) | |
260 | { | |
905ad269 | 261 | kern_unmount(ns->proc_mnt); |
6f4e6433 | 262 | } |