]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/proc/inode.c
copy address of proc_ns_ops into ns_common
[mirror_ubuntu-zesty-kernel.git] / fs / proc / inode.c
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>
10 #include <linux/pid_namespace.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/completion.h>
15 #include <linux/poll.h>
16 #include <linux/printk.h>
17 #include <linux/file.h>
18 #include <linux/limits.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/sysctl.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/mount.h>
25 #include <linux/magic.h>
26
27 #include <asm/uaccess.h>
28
29 #include "internal.h"
30
31 static void proc_evict_inode(struct inode *inode)
32 {
33 struct proc_dir_entry *de;
34 struct ctl_table_header *head;
35 struct ns_common *ns;
36
37 truncate_inode_pages_final(&inode->i_data);
38 clear_inode(inode);
39
40 /* Stop tracking associated processes */
41 put_pid(PROC_I(inode)->pid);
42
43 /* Let go of any associated proc directory entry */
44 de = PROC_I(inode)->pde;
45 if (de)
46 pde_put(de);
47 head = PROC_I(inode)->sysctl;
48 if (head) {
49 RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
50 sysctl_head_put(head);
51 }
52 /* Release any associated namespace */
53 ns = PROC_I(inode)->ns.ns;
54 if (ns && ns->ops)
55 ns->ops->put(ns);
56 }
57
58 static struct kmem_cache * proc_inode_cachep;
59
60 static struct inode *proc_alloc_inode(struct super_block *sb)
61 {
62 struct proc_inode *ei;
63 struct inode *inode;
64
65 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
66 if (!ei)
67 return NULL;
68 ei->pid = NULL;
69 ei->fd = 0;
70 ei->op.proc_get_link = NULL;
71 ei->pde = NULL;
72 ei->sysctl = NULL;
73 ei->sysctl_entry = NULL;
74 ei->ns.ns = NULL;
75 ei->ns.ns_ops = NULL;
76 inode = &ei->vfs_inode;
77 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
78 return inode;
79 }
80
81 static void proc_i_callback(struct rcu_head *head)
82 {
83 struct inode *inode = container_of(head, struct inode, i_rcu);
84 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
85 }
86
87 static void proc_destroy_inode(struct inode *inode)
88 {
89 call_rcu(&inode->i_rcu, proc_i_callback);
90 }
91
92 static void init_once(void *foo)
93 {
94 struct proc_inode *ei = (struct proc_inode *) foo;
95
96 inode_init_once(&ei->vfs_inode);
97 }
98
99 void __init proc_init_inodecache(void)
100 {
101 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
102 sizeof(struct proc_inode),
103 0, (SLAB_RECLAIM_ACCOUNT|
104 SLAB_MEM_SPREAD|SLAB_PANIC),
105 init_once);
106 }
107
108 static int proc_show_options(struct seq_file *seq, struct dentry *root)
109 {
110 struct super_block *sb = root->d_sb;
111 struct pid_namespace *pid = sb->s_fs_info;
112
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));
115 if (pid->hide_pid != 0)
116 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
117
118 return 0;
119 }
120
121 static const struct super_operations proc_sops = {
122 .alloc_inode = proc_alloc_inode,
123 .destroy_inode = proc_destroy_inode,
124 .drop_inode = generic_delete_inode,
125 .evict_inode = proc_evict_inode,
126 .statfs = simple_statfs,
127 .remount_fs = proc_remount,
128 .show_options = proc_show_options,
129 };
130
131 enum {BIAS = -1U<<31};
132
133 static inline int use_pde(struct proc_dir_entry *pde)
134 {
135 return atomic_inc_unless_negative(&pde->in_use);
136 }
137
138 static void unuse_pde(struct proc_dir_entry *pde)
139 {
140 if (atomic_dec_return(&pde->in_use) == BIAS)
141 complete(pde->pde_unload_completion);
142 }
143
144 /* pde is locked */
145 static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
146 {
147 if (pdeo->closing) {
148 /* somebody else is doing that, just wait */
149 DECLARE_COMPLETION_ONSTACK(c);
150 pdeo->c = &c;
151 spin_unlock(&pde->pde_unload_lock);
152 wait_for_completion(&c);
153 spin_lock(&pde->pde_unload_lock);
154 } else {
155 struct file *file;
156 pdeo->closing = 1;
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);
162 if (pdeo->c)
163 complete(pdeo->c);
164 kfree(pdeo);
165 }
166 }
167
168 void proc_entry_rundown(struct proc_dir_entry *de)
169 {
170 DECLARE_COMPLETION_ONSTACK(c);
171 /* Wait until all existing callers into module are done. */
172 de->pde_unload_completion = &c;
173 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
174 wait_for_completion(&c);
175
176 spin_lock(&de->pde_unload_lock);
177 while (!list_empty(&de->pde_openers)) {
178 struct pde_opener *pdeo;
179 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
180 close_pdeo(de, pdeo);
181 }
182 spin_unlock(&de->pde_unload_lock);
183 }
184
185 static 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 }
197 return rv;
198 }
199
200 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
201 {
202 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
203 struct proc_dir_entry *pde = PDE(file_inode(file));
204 ssize_t rv = -EIO;
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);
210 }
211 return rv;
212 }
213
214 static 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 }
225 return rv;
226 }
227
228 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
229 {
230 struct proc_dir_entry *pde = PDE(file_inode(file));
231 unsigned int rv = DEFAULT_POLLMASK;
232 unsigned int (*poll)(struct file *, struct poll_table_struct *);
233 if (use_pde(pde)) {
234 poll = pde->proc_fops->poll;
235 if (poll)
236 rv = poll(file, pts);
237 unuse_pde(pde);
238 }
239 return rv;
240 }
241
242 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
243 {
244 struct proc_dir_entry *pde = PDE(file_inode(file));
245 long rv = -ENOTTY;
246 long (*ioctl)(struct file *, unsigned int, unsigned long);
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);
252 }
253 return rv;
254 }
255
256 #ifdef CONFIG_COMPAT
257 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
258 {
259 struct proc_dir_entry *pde = PDE(file_inode(file));
260 long rv = -ENOTTY;
261 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
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);
267 }
268 return rv;
269 }
270 #endif
271
272 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
273 {
274 struct proc_dir_entry *pde = PDE(file_inode(file));
275 int rv = -EIO;
276 int (*mmap)(struct file *, struct vm_area_struct *);
277 if (use_pde(pde)) {
278 mmap = pde->proc_fops->mmap;
279 if (mmap)
280 rv = mmap(file, vma);
281 unuse_pde(pde);
282 }
283 return rv;
284 }
285
286 static unsigned long
287 proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
288 unsigned long len, unsigned long pgoff,
289 unsigned long flags)
290 {
291 struct proc_dir_entry *pde = PDE(file_inode(file));
292 unsigned long rv = -EIO;
293
294 if (use_pde(pde)) {
295 typeof(proc_reg_get_unmapped_area) *get_area;
296
297 get_area = pde->proc_fops->get_unmapped_area;
298 #ifdef CONFIG_MMU
299 if (!get_area)
300 get_area = current->mm->get_unmapped_area;
301 #endif
302
303 if (get_area)
304 rv = get_area(file, orig_addr, len, pgoff, flags);
305 else
306 rv = orig_addr;
307 unuse_pde(pde);
308 }
309 return rv;
310 }
311
312 static 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 *);
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 */
330 pdeo = kzalloc(sizeof(struct pde_opener), GFP_KERNEL);
331 if (!pdeo)
332 return -ENOMEM;
333
334 if (!use_pde(pde)) {
335 kfree(pdeo);
336 return -ENOENT;
337 }
338 open = pde->proc_fops->open;
339 release = pde->proc_fops->release;
340
341 if (open)
342 rv = open(inode, file);
343
344 if (rv == 0 && release) {
345 /* To know what to release. */
346 pdeo->file = file;
347 /* Strictly for "too late" ->release in proc_reg_release(). */
348 spin_lock(&pde->pde_unload_lock);
349 list_add(&pdeo->lh, &pde->pde_openers);
350 spin_unlock(&pde->pde_unload_lock);
351 } else
352 kfree(pdeo);
353
354 unuse_pde(pde);
355 return rv;
356 }
357
358 static int proc_reg_release(struct inode *inode, struct file *file)
359 {
360 struct proc_dir_entry *pde = PDE(inode);
361 struct pde_opener *pdeo;
362 spin_lock(&pde->pde_unload_lock);
363 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
364 if (pdeo->file == file) {
365 close_pdeo(pde, pdeo);
366 break;
367 }
368 }
369 spin_unlock(&pde->pde_unload_lock);
370 return 0;
371 }
372
373 static 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,
383 .get_unmapped_area = proc_reg_get_unmapped_area,
384 .open = proc_reg_open,
385 .release = proc_reg_release,
386 };
387
388 #ifdef CONFIG_COMPAT
389 static 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,
396 .get_unmapped_area = proc_reg_get_unmapped_area,
397 .open = proc_reg_open,
398 .release = proc_reg_release,
399 };
400 #endif
401
402 struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
403 {
404 struct inode *inode = new_inode_pseudo(sb);
405
406 if (inode) {
407 inode->i_ino = de->low_ino;
408 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
409 PROC_I(inode)->pde = de;
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)
419 set_nlink(inode, de->nlink);
420 WARN_ON(!de->proc_iops);
421 inode->i_op = de->proc_iops;
422 if (de->proc_fops) {
423 if (S_ISREG(inode->i_mode)) {
424 #ifdef CONFIG_COMPAT
425 if (!de->proc_fops->compat_ioctl)
426 inode->i_fop =
427 &proc_reg_file_ops_no_compat;
428 else
429 #endif
430 inode->i_fop = &proc_reg_file_ops;
431 } else {
432 inode->i_fop = de->proc_fops;
433 }
434 }
435 } else
436 pde_put(de);
437 return inode;
438 }
439
440 int proc_fill_super(struct super_block *s)
441 {
442 struct inode *root_inode;
443 int ret;
444
445 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
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
452 pde_get(&proc_root);
453 root_inode = proc_get_inode(s, &proc_root);
454 if (!root_inode) {
455 pr_err("proc_fill_super: get root inode failed\n");
456 return -ENOMEM;
457 }
458
459 s->s_root = d_make_root(root_inode);
460 if (!s->s_root) {
461 pr_err("proc_fill_super: allocate dentry failed\n");
462 return -ENOMEM;
463 }
464
465 ret = proc_setup_self(s);
466 if (ret) {
467 return ret;
468 }
469 return proc_setup_thread_self(s);
470 }