]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/proc/base.c
Merge branch 'akpm'
[karo-tx-linux.git] / fs / proc / base.c
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49
50 #include <asm/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
65 #include <linux/mm.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
89 #endif
90 #include <trace/events/oom.h>
91 #include "internal.h"
92
93 /* NOTE:
94  *      Implementing inode permission operations in /proc is almost
95  *      certainly an error.  Permission checks need to happen during
96  *      each system call not at open time.  The reason is that most of
97  *      what we wish to check for permissions in /proc varies at runtime.
98  *
99  *      The classic example of a problem is opening file descriptors
100  *      in /proc for a task before it execs a suid executable.
101  */
102
103 struct pid_entry {
104         char *name;
105         int len;
106         mode_t mode;
107         const struct inode_operations *iop;
108         const struct file_operations *fop;
109         union proc_op op;
110 };
111
112 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
113         .name = (NAME),                                 \
114         .len  = sizeof(NAME) - 1,                       \
115         .mode = MODE,                                   \
116         .iop  = IOP,                                    \
117         .fop  = FOP,                                    \
118         .op   = OP,                                     \
119 }
120
121 #define DIR(NAME, MODE, iops, fops)     \
122         NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123 #define LNK(NAME, get_link)                                     \
124         NOD(NAME, (S_IFLNK|S_IRWXUGO),                          \
125                 &proc_pid_link_inode_operations, NULL,          \
126                 { .proc_get_link = get_link } )
127 #define REG(NAME, MODE, fops)                           \
128         NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129 #define INF(NAME, MODE, read)                           \
130         NOD(NAME, (S_IFREG|(MODE)),                     \
131                 NULL, &proc_info_file_operations,       \
132                 { .proc_read = read } )
133 #define ONE(NAME, MODE, show)                           \
134         NOD(NAME, (S_IFREG|(MODE)),                     \
135                 NULL, &proc_single_file_operations,     \
136                 { .proc_show = show } )
137
138 static int proc_fd_permission(struct inode *inode, int mask);
139
140 /*
141  * Count the number of hardlinks for the pid_entry table, excluding the .
142  * and .. links.
143  */
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145         unsigned int n)
146 {
147         unsigned int i;
148         unsigned int count;
149
150         count = 0;
151         for (i = 0; i < n; ++i) {
152                 if (S_ISDIR(entries[i].mode))
153                         ++count;
154         }
155
156         return count;
157 }
158
159 static int get_task_root(struct task_struct *task, struct path *root)
160 {
161         int result = -ENOENT;
162
163         task_lock(task);
164         if (task->fs) {
165                 get_fs_root(task->fs, root);
166                 result = 0;
167         }
168         task_unlock(task);
169         return result;
170 }
171
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
173 {
174         struct task_struct *task = get_proc_task(dentry->d_inode);
175         int result = -ENOENT;
176
177         if (task) {
178                 task_lock(task);
179                 if (task->fs) {
180                         get_fs_pwd(task->fs, path);
181                         result = 0;
182                 }
183                 task_unlock(task);
184                 put_task_struct(task);
185         }
186         return result;
187 }
188
189 static int proc_root_link(struct dentry *dentry, struct path *path)
190 {
191         struct task_struct *task = get_proc_task(dentry->d_inode);
192         int result = -ENOENT;
193
194         if (task) {
195                 result = get_task_root(task, path);
196                 put_task_struct(task);
197         }
198         return result;
199 }
200
201 static struct mm_struct *__check_mem_permission(struct task_struct *task)
202 {
203         struct mm_struct *mm;
204
205         mm = get_task_mm(task);
206         if (!mm)
207                 return ERR_PTR(-EINVAL);
208
209         /*
210          * A task can always look at itself, in case it chooses
211          * to use system calls instead of load instructions.
212          */
213         if (task == current)
214                 return mm;
215
216         /*
217          * If current is actively ptrace'ing, and would also be
218          * permitted to freshly attach with ptrace now, permit it.
219          */
220         if (task_is_stopped_or_traced(task)) {
221                 int match;
222                 rcu_read_lock();
223                 match = (ptrace_parent(task) == current);
224                 rcu_read_unlock();
225                 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
226                         return mm;
227         }
228
229         /*
230          * No one else is allowed.
231          */
232         mmput(mm);
233         return ERR_PTR(-EPERM);
234 }
235
236 /*
237  * If current may access user memory in @task return a reference to the
238  * corresponding mm, otherwise ERR_PTR.
239  */
240 static struct mm_struct *check_mem_permission(struct task_struct *task)
241 {
242         struct mm_struct *mm;
243         int err;
244
245         /*
246          * Avoid racing if task exec's as we might get a new mm but validate
247          * against old credentials.
248          */
249         err = mutex_lock_killable(&task->signal->cred_guard_mutex);
250         if (err)
251                 return ERR_PTR(err);
252
253         mm = __check_mem_permission(task);
254         mutex_unlock(&task->signal->cred_guard_mutex);
255
256         return mm;
257 }
258
259 struct mm_struct *mm_for_maps(struct task_struct *task)
260 {
261         struct mm_struct *mm;
262         int err;
263
264         err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
265         if (err)
266                 return ERR_PTR(err);
267
268         mm = get_task_mm(task);
269         if (mm && mm != current->mm &&
270                         !ptrace_may_access(task, PTRACE_MODE_READ)) {
271                 mmput(mm);
272                 mm = ERR_PTR(-EACCES);
273         }
274         mutex_unlock(&task->signal->cred_guard_mutex);
275
276         return mm;
277 }
278
279 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
280 {
281         int res = 0;
282         unsigned int len;
283         struct mm_struct *mm = get_task_mm(task);
284         if (!mm)
285                 goto out;
286         if (!mm->arg_end)
287                 goto out_mm;    /* Shh! No looking before we're done */
288
289         len = mm->arg_end - mm->arg_start;
290  
291         if (len > PAGE_SIZE)
292                 len = PAGE_SIZE;
293  
294         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
295
296         // If the nul at the end of args has been overwritten, then
297         // assume application is using setproctitle(3).
298         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
299                 len = strnlen(buffer, res);
300                 if (len < res) {
301                     res = len;
302                 } else {
303                         len = mm->env_end - mm->env_start;
304                         if (len > PAGE_SIZE - res)
305                                 len = PAGE_SIZE - res;
306                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
307                         res = strnlen(buffer, res);
308                 }
309         }
310 out_mm:
311         mmput(mm);
312 out:
313         return res;
314 }
315
316 static int proc_pid_auxv(struct task_struct *task, char *buffer)
317 {
318         struct mm_struct *mm = mm_for_maps(task);
319         int res = PTR_ERR(mm);
320         if (mm && !IS_ERR(mm)) {
321                 unsigned int nwords = 0;
322                 do {
323                         nwords += 2;
324                 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
325                 res = nwords * sizeof(mm->saved_auxv[0]);
326                 if (res > PAGE_SIZE)
327                         res = PAGE_SIZE;
328                 memcpy(buffer, mm->saved_auxv, res);
329                 mmput(mm);
330         }
331         return res;
332 }
333
334
335 #ifdef CONFIG_KALLSYMS
336 /*
337  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
338  * Returns the resolved symbol.  If that fails, simply return the address.
339  */
340 static int proc_pid_wchan(struct task_struct *task, char *buffer)
341 {
342         unsigned long wchan;
343         char symname[KSYM_NAME_LEN];
344
345         wchan = get_wchan(task);
346
347         if (lookup_symbol_name(wchan, symname) < 0)
348                 if (!ptrace_may_access(task, PTRACE_MODE_READ))
349                         return 0;
350                 else
351                         return sprintf(buffer, "%lu", wchan);
352         else
353                 return sprintf(buffer, "%s", symname);
354 }
355 #endif /* CONFIG_KALLSYMS */
356
357 static int lock_trace(struct task_struct *task)
358 {
359         int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
360         if (err)
361                 return err;
362         if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
363                 mutex_unlock(&task->signal->cred_guard_mutex);
364                 return -EPERM;
365         }
366         return 0;
367 }
368
369 static void unlock_trace(struct task_struct *task)
370 {
371         mutex_unlock(&task->signal->cred_guard_mutex);
372 }
373
374 #ifdef CONFIG_STACKTRACE
375
376 #define MAX_STACK_TRACE_DEPTH   64
377
378 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
379                           struct pid *pid, struct task_struct *task)
380 {
381         struct stack_trace trace;
382         unsigned long *entries;
383         int err;
384         int i;
385
386         entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
387         if (!entries)
388                 return -ENOMEM;
389
390         trace.nr_entries        = 0;
391         trace.max_entries       = MAX_STACK_TRACE_DEPTH;
392         trace.entries           = entries;
393         trace.skip              = 0;
394
395         err = lock_trace(task);
396         if (!err) {
397                 save_stack_trace_tsk(task, &trace);
398
399                 for (i = 0; i < trace.nr_entries; i++) {
400                         seq_printf(m, "[<%pK>] %pS\n",
401                                    (void *)entries[i], (void *)entries[i]);
402                 }
403                 unlock_trace(task);
404         }
405         kfree(entries);
406
407         return err;
408 }
409 #endif
410
411 #ifdef CONFIG_SCHEDSTATS
412 /*
413  * Provides /proc/PID/schedstat
414  */
415 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
416 {
417         return sprintf(buffer, "%llu %llu %lu\n",
418                         (unsigned long long)task->se.sum_exec_runtime,
419                         (unsigned long long)task->sched_info.run_delay,
420                         task->sched_info.pcount);
421 }
422 #endif
423
424 #ifdef CONFIG_LATENCYTOP
425 static int lstats_show_proc(struct seq_file *m, void *v)
426 {
427         int i;
428         struct inode *inode = m->private;
429         struct task_struct *task = get_proc_task(inode);
430
431         if (!task)
432                 return -ESRCH;
433         seq_puts(m, "Latency Top version : v0.1\n");
434         for (i = 0; i < 32; i++) {
435                 struct latency_record *lr = &task->latency_record[i];
436                 if (lr->backtrace[0]) {
437                         int q;
438                         seq_printf(m, "%i %li %li",
439                                    lr->count, lr->time, lr->max);
440                         for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
441                                 unsigned long bt = lr->backtrace[q];
442                                 if (!bt)
443                                         break;
444                                 if (bt == ULONG_MAX)
445                                         break;
446                                 seq_printf(m, " %ps", (void *)bt);
447                         }
448                         seq_putc(m, '\n');
449                 }
450
451         }
452         put_task_struct(task);
453         return 0;
454 }
455
456 static int lstats_open(struct inode *inode, struct file *file)
457 {
458         return single_open(file, lstats_show_proc, inode);
459 }
460
461 static ssize_t lstats_write(struct file *file, const char __user *buf,
462                             size_t count, loff_t *offs)
463 {
464         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
465
466         if (!task)
467                 return -ESRCH;
468         clear_all_latency_tracing(task);
469         put_task_struct(task);
470
471         return count;
472 }
473
474 static const struct file_operations proc_lstats_operations = {
475         .open           = lstats_open,
476         .read           = seq_read,
477         .write          = lstats_write,
478         .llseek         = seq_lseek,
479         .release        = single_release,
480 };
481
482 #endif
483
484 static int proc_oom_score(struct task_struct *task, char *buffer)
485 {
486         unsigned long points = 0;
487
488         read_lock(&tasklist_lock);
489         if (pid_alive(task))
490                 points = oom_badness(task, NULL, NULL,
491                                         totalram_pages + total_swap_pages);
492         read_unlock(&tasklist_lock);
493         return sprintf(buffer, "%lu\n", points);
494 }
495
496 struct limit_names {
497         char *name;
498         char *unit;
499 };
500
501 static const struct limit_names lnames[RLIM_NLIMITS] = {
502         [RLIMIT_CPU] = {"Max cpu time", "seconds"},
503         [RLIMIT_FSIZE] = {"Max file size", "bytes"},
504         [RLIMIT_DATA] = {"Max data size", "bytes"},
505         [RLIMIT_STACK] = {"Max stack size", "bytes"},
506         [RLIMIT_CORE] = {"Max core file size", "bytes"},
507         [RLIMIT_RSS] = {"Max resident set", "bytes"},
508         [RLIMIT_NPROC] = {"Max processes", "processes"},
509         [RLIMIT_NOFILE] = {"Max open files", "files"},
510         [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
511         [RLIMIT_AS] = {"Max address space", "bytes"},
512         [RLIMIT_LOCKS] = {"Max file locks", "locks"},
513         [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
514         [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
515         [RLIMIT_NICE] = {"Max nice priority", NULL},
516         [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
517         [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
518 };
519
520 /* Display limits for a process */
521 static int proc_pid_limits(struct task_struct *task, char *buffer)
522 {
523         unsigned int i;
524         int count = 0;
525         unsigned long flags;
526         char *bufptr = buffer;
527
528         struct rlimit rlim[RLIM_NLIMITS];
529
530         if (!lock_task_sighand(task, &flags))
531                 return 0;
532         memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
533         unlock_task_sighand(task, &flags);
534
535         /*
536          * print the file header
537          */
538         count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
539                         "Limit", "Soft Limit", "Hard Limit", "Units");
540
541         for (i = 0; i < RLIM_NLIMITS; i++) {
542                 if (rlim[i].rlim_cur == RLIM_INFINITY)
543                         count += sprintf(&bufptr[count], "%-25s %-20s ",
544                                          lnames[i].name, "unlimited");
545                 else
546                         count += sprintf(&bufptr[count], "%-25s %-20lu ",
547                                          lnames[i].name, rlim[i].rlim_cur);
548
549                 if (rlim[i].rlim_max == RLIM_INFINITY)
550                         count += sprintf(&bufptr[count], "%-20s ", "unlimited");
551                 else
552                         count += sprintf(&bufptr[count], "%-20lu ",
553                                          rlim[i].rlim_max);
554
555                 if (lnames[i].unit)
556                         count += sprintf(&bufptr[count], "%-10s\n",
557                                          lnames[i].unit);
558                 else
559                         count += sprintf(&bufptr[count], "\n");
560         }
561
562         return count;
563 }
564
565 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566 static int proc_pid_syscall(struct task_struct *task, char *buffer)
567 {
568         long nr;
569         unsigned long args[6], sp, pc;
570         int res = lock_trace(task);
571         if (res)
572                 return res;
573
574         if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
575                 res = sprintf(buffer, "running\n");
576         else if (nr < 0)
577                 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
578         else
579                 res = sprintf(buffer,
580                        "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
581                        nr,
582                        args[0], args[1], args[2], args[3], args[4], args[5],
583                        sp, pc);
584         unlock_trace(task);
585         return res;
586 }
587 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
588
589 /************************************************************************/
590 /*                       Here the fs part begins                        */
591 /************************************************************************/
592
593 /* permission checks */
594 static int proc_fd_access_allowed(struct inode *inode)
595 {
596         struct task_struct *task;
597         int allowed = 0;
598         /* Allow access to a task's file descriptors if it is us or we
599          * may use ptrace attach to the process and find out that
600          * information.
601          */
602         task = get_proc_task(inode);
603         if (task) {
604                 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
605                 put_task_struct(task);
606         }
607         return allowed;
608 }
609
610 int proc_setattr(struct dentry *dentry, struct iattr *attr)
611 {
612         int error;
613         struct inode *inode = dentry->d_inode;
614
615         if (attr->ia_valid & ATTR_MODE)
616                 return -EPERM;
617
618         error = inode_change_ok(inode, attr);
619         if (error)
620                 return error;
621
622         if ((attr->ia_valid & ATTR_SIZE) &&
623             attr->ia_size != i_size_read(inode)) {
624                 error = vmtruncate(inode, attr->ia_size);
625                 if (error)
626                         return error;
627         }
628
629         setattr_copy(inode, attr);
630         mark_inode_dirty(inode);
631         return 0;
632 }
633
634 /*
635  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
636  * or euid/egid (for hide_pid_min=2)?
637  */
638 static bool has_pid_permissions(struct pid_namespace *pid,
639                                  struct task_struct *task,
640                                  int hide_pid_min)
641 {
642         if (pid->hide_pid < hide_pid_min)
643                 return true;
644         if (in_group_p(pid->pid_gid))
645                 return true;
646         return ptrace_may_access(task, PTRACE_MODE_READ);
647 }
648
649
650 static int proc_pid_permission(struct inode *inode, int mask)
651 {
652         struct pid_namespace *pid = inode->i_sb->s_fs_info;
653         struct task_struct *task;
654         bool has_perms;
655
656         task = get_proc_task(inode);
657         has_perms = has_pid_permissions(pid, task, 1);
658         put_task_struct(task);
659
660         if (!has_perms) {
661                 if (pid->hide_pid == 2) {
662                         /*
663                          * Let's make getdents(), stat(), and open()
664                          * consistent with each other.  If a process
665                          * may not stat() a file, it shouldn't be seen
666                          * in procfs at all.
667                          */
668                         return -ENOENT;
669                 }
670
671                 return -EPERM;
672         }
673         return generic_permission(inode, mask);
674 }
675
676
677
678 static const struct inode_operations proc_def_inode_operations = {
679         .setattr        = proc_setattr,
680 };
681
682 static int mounts_open_common(struct inode *inode, struct file *file,
683                               const struct seq_operations *op)
684 {
685         struct task_struct *task = get_proc_task(inode);
686         struct nsproxy *nsp;
687         struct mnt_namespace *ns = NULL;
688         struct path root;
689         struct proc_mounts *p;
690         int ret = -EINVAL;
691
692         if (task) {
693                 rcu_read_lock();
694                 nsp = task_nsproxy(task);
695                 if (nsp) {
696                         ns = nsp->mnt_ns;
697                         if (ns)
698                                 get_mnt_ns(ns);
699                 }
700                 rcu_read_unlock();
701                 if (ns && get_task_root(task, &root) == 0)
702                         ret = 0;
703                 put_task_struct(task);
704         }
705
706         if (!ns)
707                 goto err;
708         if (ret)
709                 goto err_put_ns;
710
711         ret = -ENOMEM;
712         p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
713         if (!p)
714                 goto err_put_path;
715
716         file->private_data = &p->m;
717         ret = seq_open(file, op);
718         if (ret)
719                 goto err_free;
720
721         p->m.private = p;
722         p->ns = ns;
723         p->root = root;
724         p->m.poll_event = ns->event;
725
726         return 0;
727
728  err_free:
729         kfree(p);
730  err_put_path:
731         path_put(&root);
732  err_put_ns:
733         put_mnt_ns(ns);
734  err:
735         return ret;
736 }
737
738 static int mounts_release(struct inode *inode, struct file *file)
739 {
740         struct proc_mounts *p = file->private_data;
741         path_put(&p->root);
742         put_mnt_ns(p->ns);
743         return seq_release(inode, file);
744 }
745
746 static unsigned mounts_poll(struct file *file, poll_table *wait)
747 {
748         struct proc_mounts *p = file->private_data;
749         unsigned res = POLLIN | POLLRDNORM;
750
751         poll_wait(file, &p->ns->poll, wait);
752         if (mnt_had_events(p))
753                 res |= POLLERR | POLLPRI;
754
755         return res;
756 }
757
758 static int mounts_open(struct inode *inode, struct file *file)
759 {
760         return mounts_open_common(inode, file, &mounts_op);
761 }
762
763 static const struct file_operations proc_mounts_operations = {
764         .open           = mounts_open,
765         .read           = seq_read,
766         .llseek         = seq_lseek,
767         .release        = mounts_release,
768         .poll           = mounts_poll,
769 };
770
771 static int mountinfo_open(struct inode *inode, struct file *file)
772 {
773         return mounts_open_common(inode, file, &mountinfo_op);
774 }
775
776 static const struct file_operations proc_mountinfo_operations = {
777         .open           = mountinfo_open,
778         .read           = seq_read,
779         .llseek         = seq_lseek,
780         .release        = mounts_release,
781         .poll           = mounts_poll,
782 };
783
784 static int mountstats_open(struct inode *inode, struct file *file)
785 {
786         return mounts_open_common(inode, file, &mountstats_op);
787 }
788
789 static const struct file_operations proc_mountstats_operations = {
790         .open           = mountstats_open,
791         .read           = seq_read,
792         .llseek         = seq_lseek,
793         .release        = mounts_release,
794 };
795
796 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
797
798 static ssize_t proc_info_read(struct file * file, char __user * buf,
799                           size_t count, loff_t *ppos)
800 {
801         struct inode * inode = file->f_path.dentry->d_inode;
802         unsigned long page;
803         ssize_t length;
804         struct task_struct *task = get_proc_task(inode);
805
806         length = -ESRCH;
807         if (!task)
808                 goto out_no_task;
809
810         if (count > PROC_BLOCK_SIZE)
811                 count = PROC_BLOCK_SIZE;
812
813         length = -ENOMEM;
814         if (!(page = __get_free_page(GFP_TEMPORARY)))
815                 goto out;
816
817         length = PROC_I(inode)->op.proc_read(task, (char*)page);
818
819         if (length >= 0)
820                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
821         free_page(page);
822 out:
823         put_task_struct(task);
824 out_no_task:
825         return length;
826 }
827
828 static const struct file_operations proc_info_file_operations = {
829         .read           = proc_info_read,
830         .llseek         = generic_file_llseek,
831 };
832
833 static int proc_single_show(struct seq_file *m, void *v)
834 {
835         struct inode *inode = m->private;
836         struct pid_namespace *ns;
837         struct pid *pid;
838         struct task_struct *task;
839         int ret;
840
841         ns = inode->i_sb->s_fs_info;
842         pid = proc_pid(inode);
843         task = get_pid_task(pid, PIDTYPE_PID);
844         if (!task)
845                 return -ESRCH;
846
847         ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
848
849         put_task_struct(task);
850         return ret;
851 }
852
853 static int proc_single_open(struct inode *inode, struct file *filp)
854 {
855         return single_open(filp, proc_single_show, inode);
856 }
857
858 static const struct file_operations proc_single_file_operations = {
859         .open           = proc_single_open,
860         .read           = seq_read,
861         .llseek         = seq_lseek,
862         .release        = single_release,
863 };
864
865 static int mem_open(struct inode* inode, struct file* file)
866 {
867         file->private_data = (void*)((long)current->self_exec_id);
868         /* OK to pass negative loff_t, we can catch out-of-range */
869         file->f_mode |= FMODE_UNSIGNED_OFFSET;
870         return 0;
871 }
872
873 static ssize_t mem_read(struct file * file, char __user * buf,
874                         size_t count, loff_t *ppos)
875 {
876         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
877         char *page;
878         unsigned long src = *ppos;
879         int ret = -ESRCH;
880         struct mm_struct *mm;
881
882         if (!task)
883                 goto out_no_task;
884
885         ret = -ENOMEM;
886         page = (char *)__get_free_page(GFP_TEMPORARY);
887         if (!page)
888                 goto out;
889
890         mm = check_mem_permission(task);
891         ret = PTR_ERR(mm);
892         if (IS_ERR(mm))
893                 goto out_free;
894
895         ret = -EIO;
896  
897         if (file->private_data != (void*)((long)current->self_exec_id))
898                 goto out_put;
899
900         ret = 0;
901  
902         while (count > 0) {
903                 int this_len, retval;
904
905                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
906                 retval = access_remote_vm(mm, src, page, this_len, 0);
907                 if (!retval) {
908                         if (!ret)
909                                 ret = -EIO;
910                         break;
911                 }
912
913                 if (copy_to_user(buf, page, retval)) {
914                         ret = -EFAULT;
915                         break;
916                 }
917  
918                 ret += retval;
919                 src += retval;
920                 buf += retval;
921                 count -= retval;
922         }
923         *ppos = src;
924
925 out_put:
926         mmput(mm);
927 out_free:
928         free_page((unsigned long) page);
929 out:
930         put_task_struct(task);
931 out_no_task:
932         return ret;
933 }
934
935 static ssize_t mem_write(struct file * file, const char __user *buf,
936                          size_t count, loff_t *ppos)
937 {
938         int copied;
939         char *page;
940         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
941         unsigned long dst = *ppos;
942         struct mm_struct *mm;
943
944         copied = -ESRCH;
945         if (!task)
946                 goto out_no_task;
947
948         copied = -ENOMEM;
949         page = (char *)__get_free_page(GFP_TEMPORARY);
950         if (!page)
951                 goto out_task;
952
953         mm = check_mem_permission(task);
954         copied = PTR_ERR(mm);
955         if (IS_ERR(mm))
956                 goto out_free;
957
958         copied = -EIO;
959         if (file->private_data != (void *)((long)current->self_exec_id))
960                 goto out_mm;
961
962         copied = 0;
963         while (count > 0) {
964                 int this_len, retval;
965
966                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
967                 if (copy_from_user(page, buf, this_len)) {
968                         copied = -EFAULT;
969                         break;
970                 }
971                 retval = access_remote_vm(mm, dst, page, this_len, 1);
972                 if (!retval) {
973                         if (!copied)
974                                 copied = -EIO;
975                         break;
976                 }
977                 copied += retval;
978                 buf += retval;
979                 dst += retval;
980                 count -= retval;                        
981         }
982         *ppos = dst;
983
984 out_mm:
985         mmput(mm);
986 out_free:
987         free_page((unsigned long) page);
988 out_task:
989         put_task_struct(task);
990 out_no_task:
991         return copied;
992 }
993
994 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
995 {
996         switch (orig) {
997         case 0:
998                 file->f_pos = offset;
999                 break;
1000         case 1:
1001                 file->f_pos += offset;
1002                 break;
1003         default:
1004                 return -EINVAL;
1005         }
1006         force_successful_syscall_return();
1007         return file->f_pos;
1008 }
1009
1010 static const struct file_operations proc_mem_operations = {
1011         .llseek         = mem_lseek,
1012         .read           = mem_read,
1013         .write          = mem_write,
1014         .open           = mem_open,
1015 };
1016
1017 static ssize_t environ_read(struct file *file, char __user *buf,
1018                         size_t count, loff_t *ppos)
1019 {
1020         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1021         char *page;
1022         unsigned long src = *ppos;
1023         int ret = -ESRCH;
1024         struct mm_struct *mm;
1025
1026         if (!task)
1027                 goto out_no_task;
1028
1029         ret = -ENOMEM;
1030         page = (char *)__get_free_page(GFP_TEMPORARY);
1031         if (!page)
1032                 goto out;
1033
1034
1035         mm = mm_for_maps(task);
1036         ret = PTR_ERR(mm);
1037         if (!mm || IS_ERR(mm))
1038                 goto out_free;
1039
1040         ret = 0;
1041         while (count > 0) {
1042                 int this_len, retval, max_len;
1043
1044                 this_len = mm->env_end - (mm->env_start + src);
1045
1046                 if (this_len <= 0)
1047                         break;
1048
1049                 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1050                 this_len = (this_len > max_len) ? max_len : this_len;
1051
1052                 retval = access_process_vm(task, (mm->env_start + src),
1053                         page, this_len, 0);
1054
1055                 if (retval <= 0) {
1056                         ret = retval;
1057                         break;
1058                 }
1059
1060                 if (copy_to_user(buf, page, retval)) {
1061                         ret = -EFAULT;
1062                         break;
1063                 }
1064
1065                 ret += retval;
1066                 src += retval;
1067                 buf += retval;
1068                 count -= retval;
1069         }
1070         *ppos = src;
1071
1072         mmput(mm);
1073 out_free:
1074         free_page((unsigned long) page);
1075 out:
1076         put_task_struct(task);
1077 out_no_task:
1078         return ret;
1079 }
1080
1081 static const struct file_operations proc_environ_operations = {
1082         .read           = environ_read,
1083         .llseek         = generic_file_llseek,
1084 };
1085
1086 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1087                                 size_t count, loff_t *ppos)
1088 {
1089         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1090         char buffer[PROC_NUMBUF];
1091         size_t len;
1092         int oom_adjust = OOM_DISABLE;
1093         unsigned long flags;
1094
1095         if (!task)
1096                 return -ESRCH;
1097
1098         if (lock_task_sighand(task, &flags)) {
1099                 oom_adjust = task->signal->oom_adj;
1100                 unlock_task_sighand(task, &flags);
1101         }
1102
1103         put_task_struct(task);
1104
1105         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1106
1107         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1108 }
1109
1110 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1111                                 size_t count, loff_t *ppos)
1112 {
1113         struct task_struct *task;
1114         char buffer[PROC_NUMBUF];
1115         int oom_adjust;
1116         unsigned long flags;
1117         int err;
1118
1119         memset(buffer, 0, sizeof(buffer));
1120         if (count > sizeof(buffer) - 1)
1121                 count = sizeof(buffer) - 1;
1122         if (copy_from_user(buffer, buf, count)) {
1123                 err = -EFAULT;
1124                 goto out;
1125         }
1126
1127         err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1128         if (err)
1129                 goto out;
1130         if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1131              oom_adjust != OOM_DISABLE) {
1132                 err = -EINVAL;
1133                 goto out;
1134         }
1135
1136         task = get_proc_task(file->f_path.dentry->d_inode);
1137         if (!task) {
1138                 err = -ESRCH;
1139                 goto out;
1140         }
1141
1142         task_lock(task);
1143         if (!task->mm) {
1144                 err = -EINVAL;
1145                 goto err_task_lock;
1146         }
1147
1148         if (!lock_task_sighand(task, &flags)) {
1149                 err = -ESRCH;
1150                 goto err_task_lock;
1151         }
1152
1153         if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1154                 err = -EACCES;
1155                 goto err_sighand;
1156         }
1157
1158         /*
1159          * Warn that /proc/pid/oom_adj is deprecated, see
1160          * Documentation/feature-removal-schedule.txt.
1161          */
1162         printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1163                   current->comm, task_pid_nr(current), task_pid_nr(task),
1164                   task_pid_nr(task));
1165         task->signal->oom_adj = oom_adjust;
1166         /*
1167          * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1168          * value is always attainable.
1169          */
1170         if (task->signal->oom_adj == OOM_ADJUST_MAX)
1171                 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1172         else
1173                 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1174                                                                 -OOM_DISABLE;
1175         trace_oom_score_adj_update(task);
1176 err_sighand:
1177         unlock_task_sighand(task, &flags);
1178 err_task_lock:
1179         task_unlock(task);
1180         put_task_struct(task);
1181 out:
1182         return err < 0 ? err : count;
1183 }
1184
1185 static const struct file_operations proc_oom_adjust_operations = {
1186         .read           = oom_adjust_read,
1187         .write          = oom_adjust_write,
1188         .llseek         = generic_file_llseek,
1189 };
1190
1191 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1192                                         size_t count, loff_t *ppos)
1193 {
1194         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1195         char buffer[PROC_NUMBUF];
1196         int oom_score_adj = OOM_SCORE_ADJ_MIN;
1197         unsigned long flags;
1198         size_t len;
1199
1200         if (!task)
1201                 return -ESRCH;
1202         if (lock_task_sighand(task, &flags)) {
1203                 oom_score_adj = task->signal->oom_score_adj;
1204                 unlock_task_sighand(task, &flags);
1205         }
1206         put_task_struct(task);
1207         len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1208         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1209 }
1210
1211 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1212                                         size_t count, loff_t *ppos)
1213 {
1214         struct task_struct *task;
1215         char buffer[PROC_NUMBUF];
1216         unsigned long flags;
1217         int oom_score_adj;
1218         int err;
1219
1220         memset(buffer, 0, sizeof(buffer));
1221         if (count > sizeof(buffer) - 1)
1222                 count = sizeof(buffer) - 1;
1223         if (copy_from_user(buffer, buf, count)) {
1224                 err = -EFAULT;
1225                 goto out;
1226         }
1227
1228         err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1229         if (err)
1230                 goto out;
1231         if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1232                         oom_score_adj > OOM_SCORE_ADJ_MAX) {
1233                 err = -EINVAL;
1234                 goto out;
1235         }
1236
1237         task = get_proc_task(file->f_path.dentry->d_inode);
1238         if (!task) {
1239                 err = -ESRCH;
1240                 goto out;
1241         }
1242
1243         task_lock(task);
1244         if (!task->mm) {
1245                 err = -EINVAL;
1246                 goto err_task_lock;
1247         }
1248
1249         if (!lock_task_sighand(task, &flags)) {
1250                 err = -ESRCH;
1251                 goto err_task_lock;
1252         }
1253
1254         if (oom_score_adj < task->signal->oom_score_adj_min &&
1255                         !capable(CAP_SYS_RESOURCE)) {
1256                 err = -EACCES;
1257                 goto err_sighand;
1258         }
1259
1260         task->signal->oom_score_adj = oom_score_adj;
1261         if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1262                 task->signal->oom_score_adj_min = oom_score_adj;
1263         trace_oom_score_adj_update(task);
1264         /*
1265          * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1266          * always attainable.
1267          */
1268         if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1269                 task->signal->oom_adj = OOM_DISABLE;
1270         else
1271                 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1272                                                         OOM_SCORE_ADJ_MAX;
1273 err_sighand:
1274         unlock_task_sighand(task, &flags);
1275 err_task_lock:
1276         task_unlock(task);
1277         put_task_struct(task);
1278 out:
1279         return err < 0 ? err : count;
1280 }
1281
1282 static const struct file_operations proc_oom_score_adj_operations = {
1283         .read           = oom_score_adj_read,
1284         .write          = oom_score_adj_write,
1285         .llseek         = default_llseek,
1286 };
1287
1288 #ifdef CONFIG_AUDITSYSCALL
1289 #define TMPBUFLEN 21
1290 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1291                                   size_t count, loff_t *ppos)
1292 {
1293         struct inode * inode = file->f_path.dentry->d_inode;
1294         struct task_struct *task = get_proc_task(inode);
1295         ssize_t length;
1296         char tmpbuf[TMPBUFLEN];
1297
1298         if (!task)
1299                 return -ESRCH;
1300         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1301                                 audit_get_loginuid(task));
1302         put_task_struct(task);
1303         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1304 }
1305
1306 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1307                                    size_t count, loff_t *ppos)
1308 {
1309         struct inode * inode = file->f_path.dentry->d_inode;
1310         char *page, *tmp;
1311         ssize_t length;
1312         uid_t loginuid;
1313
1314         if (!capable(CAP_AUDIT_CONTROL))
1315                 return -EPERM;
1316
1317         rcu_read_lock();
1318         if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1319                 rcu_read_unlock();
1320                 return -EPERM;
1321         }
1322         rcu_read_unlock();
1323
1324         if (count >= PAGE_SIZE)
1325                 count = PAGE_SIZE - 1;
1326
1327         if (*ppos != 0) {
1328                 /* No partial writes. */
1329                 return -EINVAL;
1330         }
1331         page = (char*)__get_free_page(GFP_TEMPORARY);
1332         if (!page)
1333                 return -ENOMEM;
1334         length = -EFAULT;
1335         if (copy_from_user(page, buf, count))
1336                 goto out_free_page;
1337
1338         page[count] = '\0';
1339         loginuid = simple_strtoul(page, &tmp, 10);
1340         if (tmp == page) {
1341                 length = -EINVAL;
1342                 goto out_free_page;
1343
1344         }
1345         length = audit_set_loginuid(current, loginuid);
1346         if (likely(length == 0))
1347                 length = count;
1348
1349 out_free_page:
1350         free_page((unsigned long) page);
1351         return length;
1352 }
1353
1354 static const struct file_operations proc_loginuid_operations = {
1355         .read           = proc_loginuid_read,
1356         .write          = proc_loginuid_write,
1357         .llseek         = generic_file_llseek,
1358 };
1359
1360 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1361                                   size_t count, loff_t *ppos)
1362 {
1363         struct inode * inode = file->f_path.dentry->d_inode;
1364         struct task_struct *task = get_proc_task(inode);
1365         ssize_t length;
1366         char tmpbuf[TMPBUFLEN];
1367
1368         if (!task)
1369                 return -ESRCH;
1370         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1371                                 audit_get_sessionid(task));
1372         put_task_struct(task);
1373         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1374 }
1375
1376 static const struct file_operations proc_sessionid_operations = {
1377         .read           = proc_sessionid_read,
1378         .llseek         = generic_file_llseek,
1379 };
1380 #endif
1381
1382 #ifdef CONFIG_FAULT_INJECTION
1383 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1384                                       size_t count, loff_t *ppos)
1385 {
1386         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1387         char buffer[PROC_NUMBUF];
1388         size_t len;
1389         int make_it_fail;
1390
1391         if (!task)
1392                 return -ESRCH;
1393         make_it_fail = task->make_it_fail;
1394         put_task_struct(task);
1395
1396         len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1397
1398         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1399 }
1400
1401 static ssize_t proc_fault_inject_write(struct file * file,
1402                         const char __user * buf, size_t count, loff_t *ppos)
1403 {
1404         struct task_struct *task;
1405         char buffer[PROC_NUMBUF], *end;
1406         int make_it_fail;
1407
1408         if (!capable(CAP_SYS_RESOURCE))
1409                 return -EPERM;
1410         memset(buffer, 0, sizeof(buffer));
1411         if (count > sizeof(buffer) - 1)
1412                 count = sizeof(buffer) - 1;
1413         if (copy_from_user(buffer, buf, count))
1414                 return -EFAULT;
1415         make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1416         if (*end)
1417                 return -EINVAL;
1418         task = get_proc_task(file->f_dentry->d_inode);
1419         if (!task)
1420                 return -ESRCH;
1421         task->make_it_fail = make_it_fail;
1422         put_task_struct(task);
1423
1424         return count;
1425 }
1426
1427 static const struct file_operations proc_fault_inject_operations = {
1428         .read           = proc_fault_inject_read,
1429         .write          = proc_fault_inject_write,
1430         .llseek         = generic_file_llseek,
1431 };
1432 #endif
1433
1434
1435 #ifdef CONFIG_SCHED_DEBUG
1436 /*
1437  * Print out various scheduling related per-task fields:
1438  */
1439 static int sched_show(struct seq_file *m, void *v)
1440 {
1441         struct inode *inode = m->private;
1442         struct task_struct *p;
1443
1444         p = get_proc_task(inode);
1445         if (!p)
1446                 return -ESRCH;
1447         proc_sched_show_task(p, m);
1448
1449         put_task_struct(p);
1450
1451         return 0;
1452 }
1453
1454 static ssize_t
1455 sched_write(struct file *file, const char __user *buf,
1456             size_t count, loff_t *offset)
1457 {
1458         struct inode *inode = file->f_path.dentry->d_inode;
1459         struct task_struct *p;
1460
1461         p = get_proc_task(inode);
1462         if (!p)
1463                 return -ESRCH;
1464         proc_sched_set_task(p);
1465
1466         put_task_struct(p);
1467
1468         return count;
1469 }
1470
1471 static int sched_open(struct inode *inode, struct file *filp)
1472 {
1473         return single_open(filp, sched_show, inode);
1474 }
1475
1476 static const struct file_operations proc_pid_sched_operations = {
1477         .open           = sched_open,
1478         .read           = seq_read,
1479         .write          = sched_write,
1480         .llseek         = seq_lseek,
1481         .release        = single_release,
1482 };
1483
1484 #endif
1485
1486 #ifdef CONFIG_SCHED_AUTOGROUP
1487 /*
1488  * Print out autogroup related information:
1489  */
1490 static int sched_autogroup_show(struct seq_file *m, void *v)
1491 {
1492         struct inode *inode = m->private;
1493         struct task_struct *p;
1494
1495         p = get_proc_task(inode);
1496         if (!p)
1497                 return -ESRCH;
1498         proc_sched_autogroup_show_task(p, m);
1499
1500         put_task_struct(p);
1501
1502         return 0;
1503 }
1504
1505 static ssize_t
1506 sched_autogroup_write(struct file *file, const char __user *buf,
1507             size_t count, loff_t *offset)
1508 {
1509         struct inode *inode = file->f_path.dentry->d_inode;
1510         struct task_struct *p;
1511         char buffer[PROC_NUMBUF];
1512         int nice;
1513         int err;
1514
1515         memset(buffer, 0, sizeof(buffer));
1516         if (count > sizeof(buffer) - 1)
1517                 count = sizeof(buffer) - 1;
1518         if (copy_from_user(buffer, buf, count))
1519                 return -EFAULT;
1520
1521         err = kstrtoint(strstrip(buffer), 0, &nice);
1522         if (err < 0)
1523                 return err;
1524
1525         p = get_proc_task(inode);
1526         if (!p)
1527                 return -ESRCH;
1528
1529         err = nice;
1530         err = proc_sched_autogroup_set_nice(p, &err);
1531         if (err)
1532                 count = err;
1533
1534         put_task_struct(p);
1535
1536         return count;
1537 }
1538
1539 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1540 {
1541         int ret;
1542
1543         ret = single_open(filp, sched_autogroup_show, NULL);
1544         if (!ret) {
1545                 struct seq_file *m = filp->private_data;
1546
1547                 m->private = inode;
1548         }
1549         return ret;
1550 }
1551
1552 static const struct file_operations proc_pid_sched_autogroup_operations = {
1553         .open           = sched_autogroup_open,
1554         .read           = seq_read,
1555         .write          = sched_autogroup_write,
1556         .llseek         = seq_lseek,
1557         .release        = single_release,
1558 };
1559
1560 #endif /* CONFIG_SCHED_AUTOGROUP */
1561
1562 static ssize_t comm_write(struct file *file, const char __user *buf,
1563                                 size_t count, loff_t *offset)
1564 {
1565         struct inode *inode = file->f_path.dentry->d_inode;
1566         struct task_struct *p;
1567         char buffer[TASK_COMM_LEN];
1568
1569         memset(buffer, 0, sizeof(buffer));
1570         if (count > sizeof(buffer) - 1)
1571                 count = sizeof(buffer) - 1;
1572         if (copy_from_user(buffer, buf, count))
1573                 return -EFAULT;
1574
1575         p = get_proc_task(inode);
1576         if (!p)
1577                 return -ESRCH;
1578
1579         if (same_thread_group(current, p))
1580                 set_task_comm(p, buffer);
1581         else
1582                 count = -EINVAL;
1583
1584         put_task_struct(p);
1585
1586         return count;
1587 }
1588
1589 static int comm_show(struct seq_file *m, void *v)
1590 {
1591         struct inode *inode = m->private;
1592         struct task_struct *p;
1593
1594         p = get_proc_task(inode);
1595         if (!p)
1596                 return -ESRCH;
1597
1598         task_lock(p);
1599         seq_printf(m, "%s\n", p->comm);
1600         task_unlock(p);
1601
1602         put_task_struct(p);
1603
1604         return 0;
1605 }
1606
1607 static int comm_open(struct inode *inode, struct file *filp)
1608 {
1609         return single_open(filp, comm_show, inode);
1610 }
1611
1612 static const struct file_operations proc_pid_set_comm_operations = {
1613         .open           = comm_open,
1614         .read           = seq_read,
1615         .write          = comm_write,
1616         .llseek         = seq_lseek,
1617         .release        = single_release,
1618 };
1619
1620 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1621 {
1622         struct task_struct *task;
1623         struct mm_struct *mm;
1624         struct file *exe_file;
1625
1626         task = get_proc_task(dentry->d_inode);
1627         if (!task)
1628                 return -ENOENT;
1629         mm = get_task_mm(task);
1630         put_task_struct(task);
1631         if (!mm)
1632                 return -ENOENT;
1633         exe_file = get_mm_exe_file(mm);
1634         mmput(mm);
1635         if (exe_file) {
1636                 *exe_path = exe_file->f_path;
1637                 path_get(&exe_file->f_path);
1638                 fput(exe_file);
1639                 return 0;
1640         } else
1641                 return -ENOENT;
1642 }
1643
1644 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1645 {
1646         struct inode *inode = dentry->d_inode;
1647         int error = -EACCES;
1648
1649         /* We don't need a base pointer in the /proc filesystem */
1650         path_put(&nd->path);
1651
1652         /* Are we allowed to snoop on the tasks file descriptors? */
1653         if (!proc_fd_access_allowed(inode))
1654                 goto out;
1655
1656         error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1657 out:
1658         return ERR_PTR(error);
1659 }
1660
1661 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1662 {
1663         char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1664         char *pathname;
1665         int len;
1666
1667         if (!tmp)
1668                 return -ENOMEM;
1669
1670         pathname = d_path(path, tmp, PAGE_SIZE);
1671         len = PTR_ERR(pathname);
1672         if (IS_ERR(pathname))
1673                 goto out;
1674         len = tmp + PAGE_SIZE - 1 - pathname;
1675
1676         if (len > buflen)
1677                 len = buflen;
1678         if (copy_to_user(buffer, pathname, len))
1679                 len = -EFAULT;
1680  out:
1681         free_page((unsigned long)tmp);
1682         return len;
1683 }
1684
1685 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1686 {
1687         int error = -EACCES;
1688         struct inode *inode = dentry->d_inode;
1689         struct path path;
1690
1691         /* Are we allowed to snoop on the tasks file descriptors? */
1692         if (!proc_fd_access_allowed(inode))
1693                 goto out;
1694
1695         error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1696         if (error)
1697                 goto out;
1698
1699         error = do_proc_readlink(&path, buffer, buflen);
1700         path_put(&path);
1701 out:
1702         return error;
1703 }
1704
1705 static const struct inode_operations proc_pid_link_inode_operations = {
1706         .readlink       = proc_pid_readlink,
1707         .follow_link    = proc_pid_follow_link,
1708         .setattr        = proc_setattr,
1709 };
1710
1711
1712 /* building an inode */
1713
1714 static int task_dumpable(struct task_struct *task)
1715 {
1716         int dumpable = 0;
1717         struct mm_struct *mm;
1718
1719         task_lock(task);
1720         mm = task->mm;
1721         if (mm)
1722                 dumpable = get_dumpable(mm);
1723         task_unlock(task);
1724         if(dumpable == 1)
1725                 return 1;
1726         return 0;
1727 }
1728
1729 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1730 {
1731         struct inode * inode;
1732         struct proc_inode *ei;
1733         const struct cred *cred;
1734
1735         /* We need a new inode */
1736
1737         inode = new_inode(sb);
1738         if (!inode)
1739                 goto out;
1740
1741         /* Common stuff */
1742         ei = PROC_I(inode);
1743         inode->i_ino = get_next_ino();
1744         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1745         inode->i_op = &proc_def_inode_operations;
1746
1747         /*
1748          * grab the reference to task.
1749          */
1750         ei->pid = get_task_pid(task, PIDTYPE_PID);
1751         if (!ei->pid)
1752                 goto out_unlock;
1753
1754         if (task_dumpable(task)) {
1755                 rcu_read_lock();
1756                 cred = __task_cred(task);
1757                 inode->i_uid = cred->euid;
1758                 inode->i_gid = cred->egid;
1759                 rcu_read_unlock();
1760         }
1761         security_task_to_inode(task, inode);
1762
1763 out:
1764         return inode;
1765
1766 out_unlock:
1767         iput(inode);
1768         return NULL;
1769 }
1770
1771 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1772 {
1773         struct inode *inode = dentry->d_inode;
1774         struct task_struct *task;
1775         const struct cred *cred;
1776         struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1777
1778         generic_fillattr(inode, stat);
1779
1780         rcu_read_lock();
1781         stat->uid = 0;
1782         stat->gid = 0;
1783         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1784         if (task) {
1785                 if (!has_pid_permissions(pid, task, 2)) {
1786                         rcu_read_unlock();
1787                         /*
1788                          * This doesn't prevent learning whether PID exists,
1789                          * it only makes getattr() consistent with readdir().
1790                          */
1791                         return -ENOENT;
1792                 }
1793                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1794                     task_dumpable(task)) {
1795                         cred = __task_cred(task);
1796                         stat->uid = cred->euid;
1797                         stat->gid = cred->egid;
1798                 }
1799         }
1800         rcu_read_unlock();
1801         return 0;
1802 }
1803
1804 /* dentry stuff */
1805
1806 /*
1807  *      Exceptional case: normally we are not allowed to unhash a busy
1808  * directory. In this case, however, we can do it - no aliasing problems
1809  * due to the way we treat inodes.
1810  *
1811  * Rewrite the inode's ownerships here because the owning task may have
1812  * performed a setuid(), etc.
1813  *
1814  * Before the /proc/pid/status file was created the only way to read
1815  * the effective uid of a /process was to stat /proc/pid.  Reading
1816  * /proc/pid/status is slow enough that procps and other packages
1817  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1818  * made this apply to all per process world readable and executable
1819  * directories.
1820  */
1821 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1822 {
1823         struct inode *inode;
1824         struct task_struct *task;
1825         const struct cred *cred;
1826
1827         if (nd && nd->flags & LOOKUP_RCU)
1828                 return -ECHILD;
1829
1830         inode = dentry->d_inode;
1831         task = get_proc_task(inode);
1832
1833         if (task) {
1834                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1835                     task_dumpable(task)) {
1836                         rcu_read_lock();
1837                         cred = __task_cred(task);
1838                         inode->i_uid = cred->euid;
1839                         inode->i_gid = cred->egid;
1840                         rcu_read_unlock();
1841                 } else {
1842                         inode->i_uid = 0;
1843                         inode->i_gid = 0;
1844                 }
1845                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1846                 security_task_to_inode(task, inode);
1847                 put_task_struct(task);
1848                 return 1;
1849         }
1850         d_drop(dentry);
1851         return 0;
1852 }
1853
1854 static int pid_delete_dentry(const struct dentry * dentry)
1855 {
1856         /* Is the task we represent dead?
1857          * If so, then don't put the dentry on the lru list,
1858          * kill it immediately.
1859          */
1860         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1861 }
1862
1863 const struct dentry_operations pid_dentry_operations =
1864 {
1865         .d_revalidate   = pid_revalidate,
1866         .d_delete       = pid_delete_dentry,
1867 };
1868
1869 /* Lookups */
1870
1871 /*
1872  * Fill a directory entry.
1873  *
1874  * If possible create the dcache entry and derive our inode number and
1875  * file type from dcache entry.
1876  *
1877  * Since all of the proc inode numbers are dynamically generated, the inode
1878  * numbers do not exist until the inode is cache.  This means creating the
1879  * the dcache entry in readdir is necessary to keep the inode numbers
1880  * reported by readdir in sync with the inode numbers reported
1881  * by stat.
1882  */
1883 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1884         const char *name, int len,
1885         instantiate_t instantiate, struct task_struct *task, const void *ptr)
1886 {
1887         struct dentry *child, *dir = filp->f_path.dentry;
1888         struct inode *inode;
1889         struct qstr qname;
1890         ino_t ino = 0;
1891         unsigned type = DT_UNKNOWN;
1892
1893         qname.name = name;
1894         qname.len  = len;
1895         qname.hash = full_name_hash(name, len);
1896
1897         child = d_lookup(dir, &qname);
1898         if (!child) {
1899                 struct dentry *new;
1900                 new = d_alloc(dir, &qname);
1901                 if (new) {
1902                         child = instantiate(dir->d_inode, new, task, ptr);
1903                         if (child)
1904                                 dput(new);
1905                         else
1906                                 child = new;
1907                 }
1908         }
1909         if (!child || IS_ERR(child) || !child->d_inode)
1910                 goto end_instantiate;
1911         inode = child->d_inode;
1912         if (inode) {
1913                 ino = inode->i_ino;
1914                 type = inode->i_mode >> 12;
1915         }
1916         dput(child);
1917 end_instantiate:
1918         if (!ino)
1919                 ino = find_inode_number(dir, &qname);
1920         if (!ino)
1921                 ino = 1;
1922         return filldir(dirent, name, len, filp->f_pos, ino, type);
1923 }
1924
1925 static unsigned name_to_int(struct dentry *dentry)
1926 {
1927         const char *name = dentry->d_name.name;
1928         int len = dentry->d_name.len;
1929         unsigned n = 0;
1930
1931         if (len > 1 && *name == '0')
1932                 goto out;
1933         while (len-- > 0) {
1934                 unsigned c = *name++ - '0';
1935                 if (c > 9)
1936                         goto out;
1937                 if (n >= (~0U-9)/10)
1938                         goto out;
1939                 n *= 10;
1940                 n += c;
1941         }
1942         return n;
1943 out:
1944         return ~0U;
1945 }
1946
1947 #define PROC_FDINFO_MAX 64
1948
1949 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1950 {
1951         struct task_struct *task = get_proc_task(inode);
1952         struct files_struct *files = NULL;
1953         struct file *file;
1954         int fd = proc_fd(inode);
1955
1956         if (task) {
1957                 files = get_files_struct(task);
1958                 put_task_struct(task);
1959         }
1960         if (files) {
1961                 /*
1962                  * We are not taking a ref to the file structure, so we must
1963                  * hold ->file_lock.
1964                  */
1965                 spin_lock(&files->file_lock);
1966                 file = fcheck_files(files, fd);
1967                 if (file) {
1968                         unsigned int f_flags;
1969                         struct fdtable *fdt;
1970
1971                         fdt = files_fdtable(files);
1972                         f_flags = file->f_flags & ~O_CLOEXEC;
1973                         if (FD_ISSET(fd, fdt->close_on_exec))
1974                                 f_flags |= O_CLOEXEC;
1975
1976                         if (path) {
1977                                 *path = file->f_path;
1978                                 path_get(&file->f_path);
1979                         }
1980                         if (info)
1981                                 snprintf(info, PROC_FDINFO_MAX,
1982                                          "pos:\t%lli\n"
1983                                          "flags:\t0%o\n",
1984                                          (long long) file->f_pos,
1985                                          f_flags);
1986                         spin_unlock(&files->file_lock);
1987                         put_files_struct(files);
1988                         return 0;
1989                 }
1990                 spin_unlock(&files->file_lock);
1991                 put_files_struct(files);
1992         }
1993         return -ENOENT;
1994 }
1995
1996 static int proc_fd_link(struct dentry *dentry, struct path *path)
1997 {
1998         return proc_fd_info(dentry->d_inode, path, NULL);
1999 }
2000
2001 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
2002 {
2003         struct inode *inode;
2004         struct task_struct *task;
2005         int fd;
2006         struct files_struct *files;
2007         const struct cred *cred;
2008
2009         if (nd && nd->flags & LOOKUP_RCU)
2010                 return -ECHILD;
2011
2012         inode = dentry->d_inode;
2013         task = get_proc_task(inode);
2014         fd = proc_fd(inode);
2015
2016         if (task) {
2017                 files = get_files_struct(task);
2018                 if (files) {
2019                         rcu_read_lock();
2020                         if (fcheck_files(files, fd)) {
2021                                 rcu_read_unlock();
2022                                 put_files_struct(files);
2023                                 if (task_dumpable(task)) {
2024                                         rcu_read_lock();
2025                                         cred = __task_cred(task);
2026                                         inode->i_uid = cred->euid;
2027                                         inode->i_gid = cred->egid;
2028                                         rcu_read_unlock();
2029                                 } else {
2030                                         inode->i_uid = 0;
2031                                         inode->i_gid = 0;
2032                                 }
2033                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
2034                                 security_task_to_inode(task, inode);
2035                                 put_task_struct(task);
2036                                 return 1;
2037                         }
2038                         rcu_read_unlock();
2039                         put_files_struct(files);
2040                 }
2041                 put_task_struct(task);
2042         }
2043         d_drop(dentry);
2044         return 0;
2045 }
2046
2047 static const struct dentry_operations tid_fd_dentry_operations =
2048 {
2049         .d_revalidate   = tid_fd_revalidate,
2050         .d_delete       = pid_delete_dentry,
2051 };
2052
2053 static struct dentry *proc_fd_instantiate(struct inode *dir,
2054         struct dentry *dentry, struct task_struct *task, const void *ptr)
2055 {
2056         unsigned fd = *(const unsigned *)ptr;
2057         struct file *file;
2058         struct files_struct *files;
2059         struct inode *inode;
2060         struct proc_inode *ei;
2061         struct dentry *error = ERR_PTR(-ENOENT);
2062
2063         inode = proc_pid_make_inode(dir->i_sb, task);
2064         if (!inode)
2065                 goto out;
2066         ei = PROC_I(inode);
2067         ei->fd = fd;
2068         files = get_files_struct(task);
2069         if (!files)
2070                 goto out_iput;
2071         inode->i_mode = S_IFLNK;
2072
2073         /*
2074          * We are not taking a ref to the file structure, so we must
2075          * hold ->file_lock.
2076          */
2077         spin_lock(&files->file_lock);
2078         file = fcheck_files(files, fd);
2079         if (!file)
2080                 goto out_unlock;
2081         if (file->f_mode & FMODE_READ)
2082                 inode->i_mode |= S_IRUSR | S_IXUSR;
2083         if (file->f_mode & FMODE_WRITE)
2084                 inode->i_mode |= S_IWUSR | S_IXUSR;
2085         spin_unlock(&files->file_lock);
2086         put_files_struct(files);
2087
2088         inode->i_op = &proc_pid_link_inode_operations;
2089         inode->i_size = 64;
2090         ei->op.proc_get_link = proc_fd_link;
2091         d_set_d_op(dentry, &tid_fd_dentry_operations);
2092         d_add(dentry, inode);
2093         /* Close the race of the process dying before we return the dentry */
2094         if (tid_fd_revalidate(dentry, NULL))
2095                 error = NULL;
2096
2097  out:
2098         return error;
2099 out_unlock:
2100         spin_unlock(&files->file_lock);
2101         put_files_struct(files);
2102 out_iput:
2103         iput(inode);
2104         goto out;
2105 }
2106
2107 static struct dentry *proc_lookupfd_common(struct inode *dir,
2108                                            struct dentry *dentry,
2109                                            instantiate_t instantiate)
2110 {
2111         struct task_struct *task = get_proc_task(dir);
2112         unsigned fd = name_to_int(dentry);
2113         struct dentry *result = ERR_PTR(-ENOENT);
2114
2115         if (!task)
2116                 goto out_no_task;
2117         if (fd == ~0U)
2118                 goto out;
2119
2120         result = instantiate(dir, dentry, task, &fd);
2121 out:
2122         put_task_struct(task);
2123 out_no_task:
2124         return result;
2125 }
2126
2127 static int proc_readfd_common(struct file * filp, void * dirent,
2128                               filldir_t filldir, instantiate_t instantiate)
2129 {
2130         struct dentry *dentry = filp->f_path.dentry;
2131         struct inode *inode = dentry->d_inode;
2132         struct task_struct *p = get_proc_task(inode);
2133         unsigned int fd, ino;
2134         int retval;
2135         struct files_struct * files;
2136
2137         retval = -ENOENT;
2138         if (!p)
2139                 goto out_no_task;
2140         retval = 0;
2141
2142         fd = filp->f_pos;
2143         switch (fd) {
2144                 case 0:
2145                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2146                                 goto out;
2147                         filp->f_pos++;
2148                 case 1:
2149                         ino = parent_ino(dentry);
2150                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2151                                 goto out;
2152                         filp->f_pos++;
2153                 default:
2154                         files = get_files_struct(p);
2155                         if (!files)
2156                                 goto out;
2157                         rcu_read_lock();
2158                         for (fd = filp->f_pos-2;
2159                              fd < files_fdtable(files)->max_fds;
2160                              fd++, filp->f_pos++) {
2161                                 char name[PROC_NUMBUF];
2162                                 int len;
2163
2164                                 if (!fcheck_files(files, fd))
2165                                         continue;
2166                                 rcu_read_unlock();
2167
2168                                 len = snprintf(name, sizeof(name), "%d", fd);
2169                                 if (proc_fill_cache(filp, dirent, filldir,
2170                                                     name, len, instantiate,
2171                                                     p, &fd) < 0) {
2172                                         rcu_read_lock();
2173                                         break;
2174                                 }
2175                                 rcu_read_lock();
2176                         }
2177                         rcu_read_unlock();
2178                         put_files_struct(files);
2179         }
2180 out:
2181         put_task_struct(p);
2182 out_no_task:
2183         return retval;
2184 }
2185
2186 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2187                                     struct nameidata *nd)
2188 {
2189         return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2190 }
2191
2192 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2193 {
2194         return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2195 }
2196
2197 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2198                                       size_t len, loff_t *ppos)
2199 {
2200         char tmp[PROC_FDINFO_MAX];
2201         int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2202         if (!err)
2203                 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2204         return err;
2205 }
2206
2207 static const struct file_operations proc_fdinfo_file_operations = {
2208         .open           = nonseekable_open,
2209         .read           = proc_fdinfo_read,
2210         .llseek         = no_llseek,
2211 };
2212
2213 static const struct file_operations proc_fd_operations = {
2214         .read           = generic_read_dir,
2215         .readdir        = proc_readfd,
2216         .llseek         = default_llseek,
2217 };
2218
2219 /*
2220  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2221  * which represent vma start and end addresses.
2222  */
2223 static int dname_to_vma_addr(struct dentry *dentry,
2224                              unsigned long *start, unsigned long *end)
2225 {
2226         if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2227                 return -EINVAL;
2228
2229         return 0;
2230 }
2231
2232 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2233 {
2234         unsigned long vm_start, vm_end;
2235         bool exact_vma_exists = false;
2236         struct mm_struct *mm = NULL;
2237         struct task_struct *task;
2238         const struct cred *cred;
2239         struct inode *inode;
2240         int status = 0;
2241
2242         if (nd && nd->flags & LOOKUP_RCU)
2243                 return -ECHILD;
2244
2245         if (!capable(CAP_SYS_ADMIN)) {
2246                 status = -EACCES;
2247                 goto out_notask;
2248         }
2249
2250         inode = dentry->d_inode;
2251         task = get_proc_task(inode);
2252         if (!task)
2253                 goto out_notask;
2254
2255         if (!ptrace_may_access(task, PTRACE_MODE_READ))
2256                 goto out;
2257
2258         mm = get_task_mm(task);
2259         if (!mm)
2260                 goto out;
2261
2262         if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2263                 down_read(&mm->mmap_sem);
2264                 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2265                 up_read(&mm->mmap_sem);
2266         }
2267
2268         mmput(mm);
2269
2270         if (exact_vma_exists) {
2271                 if (task_dumpable(task)) {
2272                         rcu_read_lock();
2273                         cred = __task_cred(task);
2274                         inode->i_uid = cred->euid;
2275                         inode->i_gid = cred->egid;
2276                         rcu_read_unlock();
2277                 } else {
2278                         inode->i_uid = 0;
2279                         inode->i_gid = 0;
2280                 }
2281                 security_task_to_inode(task, inode);
2282                 status = 1;
2283         }
2284
2285 out:
2286         put_task_struct(task);
2287
2288 out_notask:
2289         if (status <= 0)
2290                 d_drop(dentry);
2291
2292         return status;
2293 }
2294
2295 static const struct dentry_operations tid_map_files_dentry_operations = {
2296         .d_revalidate   = map_files_d_revalidate,
2297         .d_delete       = pid_delete_dentry,
2298 };
2299
2300 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2301 {
2302         unsigned long vm_start, vm_end;
2303         struct vm_area_struct *vma;
2304         struct task_struct *task;
2305         struct mm_struct *mm;
2306         int rc;
2307
2308         rc = -ENOENT;
2309         task = get_proc_task(dentry->d_inode);
2310         if (!task)
2311                 goto out;
2312
2313         mm = get_task_mm(task);
2314         put_task_struct(task);
2315         if (!mm)
2316                 goto out;
2317
2318         rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2319         if (rc)
2320                 goto out_mmput;
2321
2322         down_read(&mm->mmap_sem);
2323         vma = find_exact_vma(mm, vm_start, vm_end);
2324         if (vma && vma->vm_file) {
2325                 *path = vma->vm_file->f_path;
2326                 path_get(path);
2327                 rc = 0;
2328         }
2329         up_read(&mm->mmap_sem);
2330
2331 out_mmput:
2332         mmput(mm);
2333 out:
2334         return rc;
2335 }
2336
2337 struct map_files_info {
2338         struct file     *file;
2339         unsigned long   len;
2340         unsigned char   name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2341 };
2342
2343 static struct dentry *
2344 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2345                            struct task_struct *task, const void *ptr)
2346 {
2347         const struct file *file = ptr;
2348         struct proc_inode *ei;
2349         struct inode *inode;
2350
2351         if (!file)
2352                 return ERR_PTR(-ENOENT);
2353
2354         inode = proc_pid_make_inode(dir->i_sb, task);
2355         if (!inode)
2356                 return ERR_PTR(-ENOENT);
2357
2358         ei = PROC_I(inode);
2359         ei->op.proc_get_link = proc_map_files_get_link;
2360
2361         inode->i_op = &proc_pid_link_inode_operations;
2362         inode->i_size = 64;
2363         inode->i_mode = S_IFLNK;
2364
2365         if (file->f_mode & FMODE_READ)
2366                 inode->i_mode |= S_IRUSR;
2367         if (file->f_mode & FMODE_WRITE)
2368                 inode->i_mode |= S_IWUSR;
2369
2370         d_set_d_op(dentry, &tid_map_files_dentry_operations);
2371         d_add(dentry, inode);
2372
2373         return NULL;
2374 }
2375
2376 static struct dentry *proc_map_files_lookup(struct inode *dir,
2377                 struct dentry *dentry, struct nameidata *nd)
2378 {
2379         unsigned long vm_start, vm_end;
2380         struct vm_area_struct *vma;
2381         struct task_struct *task;
2382         struct dentry *result;
2383         struct mm_struct *mm;
2384
2385         result = ERR_PTR(-EACCES);
2386         if (!capable(CAP_SYS_ADMIN))
2387                 goto out;
2388
2389         result = ERR_PTR(-ENOENT);
2390         task = get_proc_task(dir);
2391         if (!task)
2392                 goto out;
2393
2394         result = ERR_PTR(-EACCES);
2395         if (lock_trace(task))
2396                 goto out_put_task;
2397
2398         result = ERR_PTR(-ENOENT);
2399         if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2400                 goto out_unlock;
2401
2402         mm = get_task_mm(task);
2403         if (!mm)
2404                 goto out_unlock;
2405
2406         down_read(&mm->mmap_sem);
2407         vma = find_exact_vma(mm, vm_start, vm_end);
2408         if (!vma)
2409                 goto out_no_vma;
2410
2411         result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2412
2413 out_no_vma:
2414         up_read(&mm->mmap_sem);
2415         mmput(mm);
2416 out_unlock:
2417         unlock_trace(task);
2418 out_put_task:
2419         put_task_struct(task);
2420 out:
2421         return result;
2422 }
2423
2424 static const struct inode_operations proc_map_files_inode_operations = {
2425         .lookup         = proc_map_files_lookup,
2426         .permission     = proc_fd_permission,
2427         .setattr        = proc_setattr,
2428 };
2429
2430 static int
2431 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2432 {
2433         struct dentry *dentry = filp->f_path.dentry;
2434         struct inode *inode = dentry->d_inode;
2435         struct vm_area_struct *vma;
2436         struct task_struct *task;
2437         struct mm_struct *mm;
2438         ino_t ino;
2439         int ret;
2440
2441         ret = -EACCES;
2442         if (!capable(CAP_SYS_ADMIN))
2443                 goto out;
2444
2445         ret = -ENOENT;
2446         task = get_proc_task(inode);
2447         if (!task)
2448                 goto out;
2449
2450         ret = -EACCES;
2451         if (lock_trace(task))
2452                 goto out_put_task;
2453
2454         ret = 0;
2455         switch (filp->f_pos) {
2456         case 0:
2457                 ino = inode->i_ino;
2458                 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2459                         goto out_unlock;
2460                 filp->f_pos++;
2461         case 1:
2462                 ino = parent_ino(dentry);
2463                 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2464                         goto out_unlock;
2465                 filp->f_pos++;
2466         default:
2467         {
2468                 unsigned long nr_files, pos, i;
2469                 struct flex_array *fa = NULL;
2470                 struct map_files_info info;
2471                 struct map_files_info *p;
2472
2473                 mm = get_task_mm(task);
2474                 if (!mm)
2475                         goto out_unlock;
2476                 down_read(&mm->mmap_sem);
2477
2478                 nr_files = 0;
2479
2480                 /*
2481                  * We need two passes here:
2482                  *
2483                  *  1) Collect vmas of mapped files with mmap_sem taken
2484                  *  2) Release mmap_sem and instantiate entries
2485                  *
2486                  * otherwise we get lockdep complained, since filldir()
2487                  * routine might require mmap_sem taken in might_fault().
2488                  */
2489
2490                 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2491                         if (vma->vm_file && ++pos > filp->f_pos)
2492                                 nr_files++;
2493                 }
2494
2495                 if (nr_files) {
2496                         fa = flex_array_alloc(sizeof(info), nr_files,
2497                                                 GFP_KERNEL);
2498                         if (!fa || flex_array_prealloc(fa, 0, nr_files,
2499                                                         GFP_KERNEL)) {
2500                                 ret = -ENOMEM;
2501                                 if (fa)
2502                                         flex_array_free(fa);
2503                                 up_read(&mm->mmap_sem);
2504                                 mmput(mm);
2505                                 goto out_unlock;
2506                         }
2507                         for (i = 0, vma = mm->mmap, pos = 2; vma;
2508                                         vma = vma->vm_next) {
2509                                 if (!vma->vm_file)
2510                                         continue;
2511                                 if (++pos <= filp->f_pos)
2512                                         continue;
2513
2514                                 get_file(vma->vm_file);
2515                                 info.file = vma->vm_file;
2516                                 info.len = snprintf(info.name,
2517                                                 sizeof(info.name), "%lx-%lx",
2518                                                 vma->vm_start, vma->vm_end);
2519                                 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2520                                         BUG();
2521                         }
2522                 }
2523                 up_read(&mm->mmap_sem);
2524
2525                 for (i = 0; i < nr_files; i++) {
2526                         p = flex_array_get(fa, i);
2527                         ret = proc_fill_cache(filp, dirent, filldir,
2528                                               p->name, p->len,
2529                                               proc_map_files_instantiate,
2530                                               task, p->file);
2531                         if (ret)
2532                                 break;
2533                         filp->f_pos++;
2534                         fput(p->file);
2535                 }
2536                 for (; i < nr_files; i++) {
2537                         /*
2538                          * In case of error don't forget
2539                          * to put rest of file refs.
2540                          */
2541                         p = flex_array_get(fa, i);
2542                         fput(p->file);
2543                 }
2544                 if (fa)
2545                         flex_array_free(fa);
2546                 mmput(mm);
2547         }
2548         }
2549
2550 out_unlock:
2551         unlock_trace(task);
2552 out_put_task:
2553         put_task_struct(task);
2554 out:
2555         return ret;
2556 }
2557
2558 static const struct file_operations proc_map_files_operations = {
2559         .read           = generic_read_dir,
2560         .readdir        = proc_map_files_readdir,
2561         .llseek         = default_llseek,
2562 };
2563
2564 /*
2565  * /proc/pid/fd needs a special permission handler so that a process can still
2566  * access /proc/self/fd after it has executed a setuid().
2567  */
2568 static int proc_fd_permission(struct inode *inode, int mask)
2569 {
2570         int rv = generic_permission(inode, mask);
2571         if (rv == 0)
2572                 return 0;
2573         if (task_pid(current) == proc_pid(inode))
2574                 rv = 0;
2575         return rv;
2576 }
2577
2578 /*
2579  * proc directories can do almost nothing..
2580  */
2581 static const struct inode_operations proc_fd_inode_operations = {
2582         .lookup         = proc_lookupfd,
2583         .permission     = proc_fd_permission,
2584         .setattr        = proc_setattr,
2585 };
2586
2587 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2588         struct dentry *dentry, struct task_struct *task, const void *ptr)
2589 {
2590         unsigned fd = *(unsigned *)ptr;
2591         struct inode *inode;
2592         struct proc_inode *ei;
2593         struct dentry *error = ERR_PTR(-ENOENT);
2594
2595         inode = proc_pid_make_inode(dir->i_sb, task);
2596         if (!inode)
2597                 goto out;
2598         ei = PROC_I(inode);
2599         ei->fd = fd;
2600         inode->i_mode = S_IFREG | S_IRUSR;
2601         inode->i_fop = &proc_fdinfo_file_operations;
2602         d_set_d_op(dentry, &tid_fd_dentry_operations);
2603         d_add(dentry, inode);
2604         /* Close the race of the process dying before we return the dentry */
2605         if (tid_fd_revalidate(dentry, NULL))
2606                 error = NULL;
2607
2608  out:
2609         return error;
2610 }
2611
2612 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2613                                         struct dentry *dentry,
2614                                         struct nameidata *nd)
2615 {
2616         return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2617 }
2618
2619 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2620 {
2621         return proc_readfd_common(filp, dirent, filldir,
2622                                   proc_fdinfo_instantiate);
2623 }
2624
2625 static const struct file_operations proc_fdinfo_operations = {
2626         .read           = generic_read_dir,
2627         .readdir        = proc_readfdinfo,
2628         .llseek         = default_llseek,
2629 };
2630
2631 /*
2632  * proc directories can do almost nothing..
2633  */
2634 static const struct inode_operations proc_fdinfo_inode_operations = {
2635         .lookup         = proc_lookupfdinfo,
2636         .setattr        = proc_setattr,
2637 };
2638
2639
2640 static struct dentry *proc_pident_instantiate(struct inode *dir,
2641         struct dentry *dentry, struct task_struct *task, const void *ptr)
2642 {
2643         const struct pid_entry *p = ptr;
2644         struct inode *inode;
2645         struct proc_inode *ei;
2646         struct dentry *error = ERR_PTR(-ENOENT);
2647
2648         inode = proc_pid_make_inode(dir->i_sb, task);
2649         if (!inode)
2650                 goto out;
2651
2652         ei = PROC_I(inode);
2653         inode->i_mode = p->mode;
2654         if (S_ISDIR(inode->i_mode))
2655                 set_nlink(inode, 2);    /* Use getattr to fix if necessary */
2656         if (p->iop)
2657                 inode->i_op = p->iop;
2658         if (p->fop)
2659                 inode->i_fop = p->fop;
2660         ei->op = p->op;
2661         d_set_d_op(dentry, &pid_dentry_operations);
2662         d_add(dentry, inode);
2663         /* Close the race of the process dying before we return the dentry */
2664         if (pid_revalidate(dentry, NULL))
2665                 error = NULL;
2666 out:
2667         return error;
2668 }
2669
2670 static struct dentry *proc_pident_lookup(struct inode *dir, 
2671                                          struct dentry *dentry,
2672                                          const struct pid_entry *ents,
2673                                          unsigned int nents)
2674 {
2675         struct dentry *error;
2676         struct task_struct *task = get_proc_task(dir);
2677         const struct pid_entry *p, *last;
2678
2679         error = ERR_PTR(-ENOENT);
2680
2681         if (!task)
2682                 goto out_no_task;
2683
2684         /*
2685          * Yes, it does not scale. And it should not. Don't add
2686          * new entries into /proc/<tgid>/ without very good reasons.
2687          */
2688         last = &ents[nents - 1];
2689         for (p = ents; p <= last; p++) {
2690                 if (p->len != dentry->d_name.len)
2691                         continue;
2692                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2693                         break;
2694         }
2695         if (p > last)
2696                 goto out;
2697
2698         error = proc_pident_instantiate(dir, dentry, task, p);
2699 out:
2700         put_task_struct(task);
2701 out_no_task:
2702         return error;
2703 }
2704
2705 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2706         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2707 {
2708         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2709                                 proc_pident_instantiate, task, p);
2710 }
2711
2712 static int proc_pident_readdir(struct file *filp,
2713                 void *dirent, filldir_t filldir,
2714                 const struct pid_entry *ents, unsigned int nents)
2715 {
2716         int i;
2717         struct dentry *dentry = filp->f_path.dentry;
2718         struct inode *inode = dentry->d_inode;
2719         struct task_struct *task = get_proc_task(inode);
2720         const struct pid_entry *p, *last;
2721         ino_t ino;
2722         int ret;
2723
2724         ret = -ENOENT;
2725         if (!task)
2726                 goto out_no_task;
2727
2728         ret = 0;
2729         i = filp->f_pos;
2730         switch (i) {
2731         case 0:
2732                 ino = inode->i_ino;
2733                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2734                         goto out;
2735                 i++;
2736                 filp->f_pos++;
2737                 /* fall through */
2738         case 1:
2739                 ino = parent_ino(dentry);
2740                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2741                         goto out;
2742                 i++;
2743                 filp->f_pos++;
2744                 /* fall through */
2745         default:
2746                 i -= 2;
2747                 if (i >= nents) {
2748                         ret = 1;
2749                         goto out;
2750                 }
2751                 p = ents + i;
2752                 last = &ents[nents - 1];
2753                 while (p <= last) {
2754                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2755                                 goto out;
2756                         filp->f_pos++;
2757                         p++;
2758                 }
2759         }
2760
2761         ret = 1;
2762 out:
2763         put_task_struct(task);
2764 out_no_task:
2765         return ret;
2766 }
2767
2768 #ifdef CONFIG_SECURITY
2769 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2770                                   size_t count, loff_t *ppos)
2771 {
2772         struct inode * inode = file->f_path.dentry->d_inode;
2773         char *p = NULL;
2774         ssize_t length;
2775         struct task_struct *task = get_proc_task(inode);
2776
2777         if (!task)
2778                 return -ESRCH;
2779
2780         length = security_getprocattr(task,
2781                                       (char*)file->f_path.dentry->d_name.name,
2782                                       &p);
2783         put_task_struct(task);
2784         if (length > 0)
2785                 length = simple_read_from_buffer(buf, count, ppos, p, length);
2786         kfree(p);
2787         return length;
2788 }
2789
2790 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2791                                    size_t count, loff_t *ppos)
2792 {
2793         struct inode * inode = file->f_path.dentry->d_inode;
2794         char *page;
2795         ssize_t length;
2796         struct task_struct *task = get_proc_task(inode);
2797
2798         length = -ESRCH;
2799         if (!task)
2800                 goto out_no_task;
2801         if (count > PAGE_SIZE)
2802                 count = PAGE_SIZE;
2803
2804         /* No partial writes. */
2805         length = -EINVAL;
2806         if (*ppos != 0)
2807                 goto out;
2808
2809         length = -ENOMEM;
2810         page = (char*)__get_free_page(GFP_TEMPORARY);
2811         if (!page)
2812                 goto out;
2813
2814         length = -EFAULT;
2815         if (copy_from_user(page, buf, count))
2816                 goto out_free;
2817
2818         /* Guard against adverse ptrace interaction */
2819         length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2820         if (length < 0)
2821                 goto out_free;
2822
2823         length = security_setprocattr(task,
2824                                       (char*)file->f_path.dentry->d_name.name,
2825                                       (void*)page, count);
2826         mutex_unlock(&task->signal->cred_guard_mutex);
2827 out_free:
2828         free_page((unsigned long) page);
2829 out:
2830         put_task_struct(task);
2831 out_no_task:
2832         return length;
2833 }
2834
2835 static const struct file_operations proc_pid_attr_operations = {
2836         .read           = proc_pid_attr_read,
2837         .write          = proc_pid_attr_write,
2838         .llseek         = generic_file_llseek,
2839 };
2840
2841 static const struct pid_entry attr_dir_stuff[] = {
2842         REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2843         REG("prev",       S_IRUGO,         proc_pid_attr_operations),
2844         REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2845         REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2846         REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2847         REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2848 };
2849
2850 static int proc_attr_dir_readdir(struct file * filp,
2851                              void * dirent, filldir_t filldir)
2852 {
2853         return proc_pident_readdir(filp,dirent,filldir,
2854                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2855 }
2856
2857 static const struct file_operations proc_attr_dir_operations = {
2858         .read           = generic_read_dir,
2859         .readdir        = proc_attr_dir_readdir,
2860         .llseek         = default_llseek,
2861 };
2862
2863 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2864                                 struct dentry *dentry, struct nameidata *nd)
2865 {
2866         return proc_pident_lookup(dir, dentry,
2867                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2868 }
2869
2870 static const struct inode_operations proc_attr_dir_inode_operations = {
2871         .lookup         = proc_attr_dir_lookup,
2872         .getattr        = pid_getattr,
2873         .setattr        = proc_setattr,
2874 };
2875
2876 #endif
2877
2878 #ifdef CONFIG_ELF_CORE
2879 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2880                                          size_t count, loff_t *ppos)
2881 {
2882         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2883         struct mm_struct *mm;
2884         char buffer[PROC_NUMBUF];
2885         size_t len;
2886         int ret;
2887
2888         if (!task)
2889                 return -ESRCH;
2890
2891         ret = 0;
2892         mm = get_task_mm(task);
2893         if (mm) {
2894                 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2895                                ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2896                                 MMF_DUMP_FILTER_SHIFT));
2897                 mmput(mm);
2898                 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2899         }
2900
2901         put_task_struct(task);
2902
2903         return ret;
2904 }
2905
2906 static ssize_t proc_coredump_filter_write(struct file *file,
2907                                           const char __user *buf,
2908                                           size_t count,
2909                                           loff_t *ppos)
2910 {
2911         struct task_struct *task;
2912         struct mm_struct *mm;
2913         char buffer[PROC_NUMBUF], *end;
2914         unsigned int val;
2915         int ret;
2916         int i;
2917         unsigned long mask;
2918
2919         ret = -EFAULT;
2920         memset(buffer, 0, sizeof(buffer));
2921         if (count > sizeof(buffer) - 1)
2922                 count = sizeof(buffer) - 1;
2923         if (copy_from_user(buffer, buf, count))
2924                 goto out_no_task;
2925
2926         ret = -EINVAL;
2927         val = (unsigned int)simple_strtoul(buffer, &end, 0);
2928         if (*end == '\n')
2929                 end++;
2930         if (end - buffer == 0)
2931                 goto out_no_task;
2932
2933         ret = -ESRCH;
2934         task = get_proc_task(file->f_dentry->d_inode);
2935         if (!task)
2936                 goto out_no_task;
2937
2938         ret = end - buffer;
2939         mm = get_task_mm(task);
2940         if (!mm)
2941                 goto out_no_mm;
2942
2943         for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2944                 if (val & mask)
2945                         set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2946                 else
2947                         clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2948         }
2949
2950         mmput(mm);
2951  out_no_mm:
2952         put_task_struct(task);
2953  out_no_task:
2954         return ret;
2955 }
2956
2957 static const struct file_operations proc_coredump_filter_operations = {
2958         .read           = proc_coredump_filter_read,
2959         .write          = proc_coredump_filter_write,
2960         .llseek         = generic_file_llseek,
2961 };
2962 #endif
2963
2964 /*
2965  * /proc/self:
2966  */
2967 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2968                               int buflen)
2969 {
2970         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2971         pid_t tgid = task_tgid_nr_ns(current, ns);
2972         char tmp[PROC_NUMBUF];
2973         if (!tgid)
2974                 return -ENOENT;
2975         sprintf(tmp, "%d", tgid);
2976         return vfs_readlink(dentry,buffer,buflen,tmp);
2977 }
2978
2979 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2980 {
2981         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2982         pid_t tgid = task_tgid_nr_ns(current, ns);
2983         char *name = ERR_PTR(-ENOENT);
2984         if (tgid) {
2985                 name = __getname();
2986                 if (!name)
2987                         name = ERR_PTR(-ENOMEM);
2988                 else
2989                         sprintf(name, "%d", tgid);
2990         }
2991         nd_set_link(nd, name);
2992         return NULL;
2993 }
2994
2995 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2996                                 void *cookie)
2997 {
2998         char *s = nd_get_link(nd);
2999         if (!IS_ERR(s))
3000                 __putname(s);
3001 }
3002
3003 static const struct inode_operations proc_self_inode_operations = {
3004         .readlink       = proc_self_readlink,
3005         .follow_link    = proc_self_follow_link,
3006         .put_link       = proc_self_put_link,
3007 };
3008
3009 /*
3010  * proc base
3011  *
3012  * These are the directory entries in the root directory of /proc
3013  * that properly belong to the /proc filesystem, as they describe
3014  * describe something that is process related.
3015  */
3016 static const struct pid_entry proc_base_stuff[] = {
3017         NOD("self", S_IFLNK|S_IRWXUGO,
3018                 &proc_self_inode_operations, NULL, {}),
3019 };
3020
3021 static struct dentry *proc_base_instantiate(struct inode *dir,
3022         struct dentry *dentry, struct task_struct *task, const void *ptr)
3023 {
3024         const struct pid_entry *p = ptr;
3025         struct inode *inode;
3026         struct proc_inode *ei;
3027         struct dentry *error;
3028
3029         /* Allocate the inode */
3030         error = ERR_PTR(-ENOMEM);
3031         inode = new_inode(dir->i_sb);
3032         if (!inode)
3033                 goto out;
3034
3035         /* Initialize the inode */
3036         ei = PROC_I(inode);
3037         inode->i_ino = get_next_ino();
3038         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3039
3040         /*
3041          * grab the reference to the task.
3042          */
3043         ei->pid = get_task_pid(task, PIDTYPE_PID);
3044         if (!ei->pid)
3045                 goto out_iput;
3046
3047         inode->i_mode = p->mode;
3048         if (S_ISDIR(inode->i_mode))
3049                 set_nlink(inode, 2);
3050         if (S_ISLNK(inode->i_mode))
3051                 inode->i_size = 64;
3052         if (p->iop)
3053                 inode->i_op = p->iop;
3054         if (p->fop)
3055                 inode->i_fop = p->fop;
3056         ei->op = p->op;
3057         d_add(dentry, inode);
3058         error = NULL;
3059 out:
3060         return error;
3061 out_iput:
3062         iput(inode);
3063         goto out;
3064 }
3065
3066 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
3067 {
3068         struct dentry *error;
3069         struct task_struct *task = get_proc_task(dir);
3070         const struct pid_entry *p, *last;
3071
3072         error = ERR_PTR(-ENOENT);
3073
3074         if (!task)
3075                 goto out_no_task;
3076
3077         /* Lookup the directory entry */
3078         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
3079         for (p = proc_base_stuff; p <= last; p++) {
3080                 if (p->len != dentry->d_name.len)
3081                         continue;
3082                 if (!memcmp(dentry->d_name.name, p->name, p->len))
3083                         break;
3084         }
3085         if (p > last)
3086                 goto out;
3087
3088         error = proc_base_instantiate(dir, dentry, task, p);
3089
3090 out:
3091         put_task_struct(task);
3092 out_no_task:
3093         return error;
3094 }
3095
3096 static int proc_base_fill_cache(struct file *filp, void *dirent,
3097         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
3098 {
3099         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
3100                                 proc_base_instantiate, task, p);
3101 }
3102
3103 #ifdef CONFIG_TASK_IO_ACCOUNTING
3104 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
3105 {
3106         struct task_io_accounting acct = task->ioac;
3107         unsigned long flags;
3108         int result;
3109
3110         result = mutex_lock_killable(&task->signal->cred_guard_mutex);
3111         if (result)
3112                 return result;
3113
3114         if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
3115                 result = -EACCES;
3116                 goto out_unlock;
3117         }
3118
3119         if (whole && lock_task_sighand(task, &flags)) {
3120                 struct task_struct *t = task;
3121
3122                 task_io_accounting_add(&acct, &task->signal->ioac);
3123                 while_each_thread(task, t)
3124                         task_io_accounting_add(&acct, &t->ioac);
3125
3126                 unlock_task_sighand(task, &flags);
3127         }
3128         result = sprintf(buffer,
3129                         "rchar: %llu\n"
3130                         "wchar: %llu\n"
3131                         "syscr: %llu\n"
3132                         "syscw: %llu\n"
3133                         "read_bytes: %llu\n"
3134                         "write_bytes: %llu\n"
3135                         "cancelled_write_bytes: %llu\n",
3136                         (unsigned long long)acct.rchar,
3137                         (unsigned long long)acct.wchar,
3138                         (unsigned long long)acct.syscr,
3139                         (unsigned long long)acct.syscw,
3140                         (unsigned long long)acct.read_bytes,
3141                         (unsigned long long)acct.write_bytes,
3142                         (unsigned long long)acct.cancelled_write_bytes);
3143 out_unlock:
3144         mutex_unlock(&task->signal->cred_guard_mutex);
3145         return result;
3146 }
3147
3148 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3149 {
3150         return do_io_accounting(task, buffer, 0);
3151 }
3152
3153 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3154 {
3155         return do_io_accounting(task, buffer, 1);
3156 }
3157 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3158
3159 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3160                                 struct pid *pid, struct task_struct *task)
3161 {
3162         int err = lock_trace(task);
3163         if (!err) {
3164                 seq_printf(m, "%08x\n", task->personality);
3165                 unlock_trace(task);
3166         }
3167         return err;
3168 }
3169
3170 /*
3171  * Thread groups
3172  */
3173 static const struct file_operations proc_task_operations;
3174 static const struct inode_operations proc_task_inode_operations;
3175
3176 static const struct pid_entry tgid_base_stuff[] = {
3177         DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3178         DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3179         DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3180         DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3181         DIR("ns",         S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3182 #ifdef CONFIG_NET
3183         DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3184 #endif
3185         REG("environ",    S_IRUSR, proc_environ_operations),
3186         INF("auxv",       S_IRUSR, proc_pid_auxv),
3187         ONE("status",     S_IRUGO, proc_pid_status),
3188         ONE("personality", S_IRUGO, proc_pid_personality),
3189         INF("limits",     S_IRUGO, proc_pid_limits),
3190 #ifdef CONFIG_SCHED_DEBUG
3191         REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3192 #endif
3193 #ifdef CONFIG_SCHED_AUTOGROUP
3194         REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3195 #endif
3196         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3197 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3198         INF("syscall",    S_IRUGO, proc_pid_syscall),
3199 #endif
3200         INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3201         ONE("stat",       S_IRUGO, proc_tgid_stat),
3202         ONE("statm",      S_IRUGO, proc_pid_statm),
3203         REG("maps",       S_IRUGO, proc_maps_operations),
3204 #ifdef CONFIG_NUMA
3205         REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
3206 #endif
3207         REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3208         LNK("cwd",        proc_cwd_link),
3209         LNK("root",       proc_root_link),
3210         LNK("exe",        proc_exe_link),
3211         REG("mounts",     S_IRUGO, proc_mounts_operations),
3212         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3213         REG("mountstats", S_IRUSR, proc_mountstats_operations),
3214 #ifdef CONFIG_PROC_PAGE_MONITOR
3215         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3216         REG("smaps",      S_IRUGO, proc_smaps_operations),
3217         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3218 #endif
3219 #ifdef CONFIG_SECURITY
3220         DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3221 #endif
3222 #ifdef CONFIG_KALLSYMS
3223         INF("wchan",      S_IRUGO, proc_pid_wchan),
3224 #endif
3225 #ifdef CONFIG_STACKTRACE
3226         ONE("stack",      S_IRUGO, proc_pid_stack),
3227 #endif
3228 #ifdef CONFIG_SCHEDSTATS
3229         INF("schedstat",  S_IRUGO, proc_pid_schedstat),
3230 #endif
3231 #ifdef CONFIG_LATENCYTOP
3232         REG("latency",  S_IRUGO, proc_lstats_operations),
3233 #endif
3234 #ifdef CONFIG_PROC_PID_CPUSET
3235         REG("cpuset",     S_IRUGO, proc_cpuset_operations),
3236 #endif
3237 #ifdef CONFIG_CGROUPS
3238         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3239 #endif
3240         INF("oom_score",  S_IRUGO, proc_oom_score),
3241         REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3242         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3243 #ifdef CONFIG_AUDITSYSCALL
3244         REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3245         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3246 #endif
3247 #ifdef CONFIG_FAULT_INJECTION
3248         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3249 #endif
3250 #ifdef CONFIG_ELF_CORE
3251         REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3252 #endif
3253 #ifdef CONFIG_TASK_IO_ACCOUNTING
3254         INF("io",       S_IRUSR, proc_tgid_io_accounting),
3255 #endif
3256 #ifdef CONFIG_HARDWALL
3257         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3258 #endif
3259 };
3260
3261 static int proc_tgid_base_readdir(struct file * filp,
3262                              void * dirent, filldir_t filldir)
3263 {
3264         return proc_pident_readdir(filp,dirent,filldir,
3265                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3266 }
3267
3268 static const struct file_operations proc_tgid_base_operations = {
3269         .read           = generic_read_dir,
3270         .readdir        = proc_tgid_base_readdir,
3271         .llseek         = default_llseek,
3272 };
3273
3274 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3275         return proc_pident_lookup(dir, dentry,
3276                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3277 }
3278
3279 static const struct inode_operations proc_tgid_base_inode_operations = {
3280         .lookup         = proc_tgid_base_lookup,
3281         .getattr        = pid_getattr,
3282         .setattr        = proc_setattr,
3283         .permission     = proc_pid_permission,
3284 };
3285
3286 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3287 {
3288         struct dentry *dentry, *leader, *dir;
3289         char buf[PROC_NUMBUF];
3290         struct qstr name;
3291
3292         name.name = buf;
3293         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3294         dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3295         if (dentry) {
3296                 shrink_dcache_parent(dentry);
3297                 d_drop(dentry);
3298                 dput(dentry);
3299         }
3300
3301         name.name = buf;
3302         name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3303         leader = d_hash_and_lookup(mnt->mnt_root, &name);
3304         if (!leader)
3305                 goto out;
3306
3307         name.name = "task";
3308         name.len = strlen(name.name);
3309         dir = d_hash_and_lookup(leader, &name);
3310         if (!dir)
3311                 goto out_put_leader;
3312
3313         name.name = buf;
3314         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3315         dentry = d_hash_and_lookup(dir, &name);
3316         if (dentry) {
3317                 shrink_dcache_parent(dentry);
3318                 d_drop(dentry);
3319                 dput(dentry);
3320         }
3321
3322         dput(dir);
3323 out_put_leader:
3324         dput(leader);
3325 out:
3326         return;
3327 }
3328
3329 /**
3330  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
3331  * @task: task that should be flushed.
3332  *
3333  * When flushing dentries from proc, one needs to flush them from global
3334  * proc (proc_mnt) and from all the namespaces' procs this task was seen
3335  * in. This call is supposed to do all of this job.
3336  *
3337  * Looks in the dcache for
3338  * /proc/@pid
3339  * /proc/@tgid/task/@pid
3340  * if either directory is present flushes it and all of it'ts children
3341  * from the dcache.
3342  *
3343  * It is safe and reasonable to cache /proc entries for a task until
3344  * that task exits.  After that they just clog up the dcache with
3345  * useless entries, possibly causing useful dcache entries to be
3346  * flushed instead.  This routine is proved to flush those useless
3347  * dcache entries at process exit time.
3348  *
3349  * NOTE: This routine is just an optimization so it does not guarantee
3350  *       that no dcache entries will exist at process exit time it
3351  *       just makes it very unlikely that any will persist.
3352  */
3353
3354 void proc_flush_task(struct task_struct *task)
3355 {
3356         int i;
3357         struct pid *pid, *tgid;
3358         struct upid *upid;
3359
3360         pid = task_pid(task);
3361         tgid = task_tgid(task);
3362
3363         for (i = 0; i <= pid->level; i++) {
3364                 upid = &pid->numbers[i];
3365                 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3366                                         tgid->numbers[i].nr);
3367         }
3368
3369         upid = &pid->numbers[pid->level];
3370         if (upid->nr == 1)
3371                 pid_ns_release_proc(upid->ns);
3372 }
3373
3374 static struct dentry *proc_pid_instantiate(struct inode *dir,
3375                                            struct dentry * dentry,
3376                                            struct task_struct *task, const void *ptr)
3377 {
3378         struct dentry *error = ERR_PTR(-ENOENT);
3379         struct inode *inode;
3380
3381         inode = proc_pid_make_inode(dir->i_sb, task);
3382         if (!inode)
3383                 goto out;
3384
3385         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3386         inode->i_op = &proc_tgid_base_inode_operations;
3387         inode->i_fop = &proc_tgid_base_operations;
3388         inode->i_flags|=S_IMMUTABLE;
3389
3390         set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3391                                                   ARRAY_SIZE(tgid_base_stuff)));
3392
3393         d_set_d_op(dentry, &pid_dentry_operations);
3394
3395         d_add(dentry, inode);
3396         /* Close the race of the process dying before we return the dentry */
3397         if (pid_revalidate(dentry, NULL))
3398                 error = NULL;
3399 out:
3400         return error;
3401 }
3402
3403 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3404 {
3405         struct dentry *result;
3406         struct task_struct *task;
3407         unsigned tgid;
3408         struct pid_namespace *ns;
3409
3410         result = proc_base_lookup(dir, dentry);
3411         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3412                 goto out;
3413
3414         tgid = name_to_int(dentry);
3415         if (tgid == ~0U)
3416                 goto out;
3417
3418         ns = dentry->d_sb->s_fs_info;
3419         rcu_read_lock();
3420         task = find_task_by_pid_ns(tgid, ns);
3421         if (task)
3422                 get_task_struct(task);
3423         rcu_read_unlock();
3424         if (!task)
3425                 goto out;
3426
3427         result = proc_pid_instantiate(dir, dentry, task, NULL);
3428         put_task_struct(task);
3429 out:
3430         return result;
3431 }
3432
3433 /*
3434  * Find the first task with tgid >= tgid
3435  *
3436  */
3437 struct tgid_iter {
3438         unsigned int tgid;
3439         struct task_struct *task;
3440 };
3441 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3442 {
3443         struct pid *pid;
3444
3445         if (iter.task)
3446                 put_task_struct(iter.task);
3447         rcu_read_lock();
3448 retry:
3449         iter.task = NULL;
3450         pid = find_ge_pid(iter.tgid, ns);
3451         if (pid) {
3452                 iter.tgid = pid_nr_ns(pid, ns);
3453                 iter.task = pid_task(pid, PIDTYPE_PID);
3454                 /* What we to know is if the pid we have find is the
3455                  * pid of a thread_group_leader.  Testing for task
3456                  * being a thread_group_leader is the obvious thing
3457                  * todo but there is a window when it fails, due to
3458                  * the pid transfer logic in de_thread.
3459                  *
3460                  * So we perform the straight forward test of seeing
3461                  * if the pid we have found is the pid of a thread
3462                  * group leader, and don't worry if the task we have
3463                  * found doesn't happen to be a thread group leader.
3464                  * As we don't care in the case of readdir.
3465                  */
3466                 if (!iter.task || !has_group_leader_pid(iter.task)) {
3467                         iter.tgid += 1;
3468                         goto retry;
3469                 }
3470                 get_task_struct(iter.task);
3471         }
3472         rcu_read_unlock();
3473         return iter;
3474 }
3475
3476 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3477
3478 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3479         struct tgid_iter iter)
3480 {
3481         char name[PROC_NUMBUF];
3482         int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3483         return proc_fill_cache(filp, dirent, filldir, name, len,
3484                                 proc_pid_instantiate, iter.task, NULL);
3485 }
3486
3487 static int fake_filldir(void *buf, const char *name, int namelen,
3488                         loff_t offset, u64 ino, unsigned d_type)
3489 {
3490         return 0;
3491 }
3492
3493 /* for the /proc/ directory itself, after non-process stuff has been done */
3494 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3495 {
3496         unsigned int nr;
3497         struct task_struct *reaper;
3498         struct tgid_iter iter;
3499         struct pid_namespace *ns;
3500         filldir_t __filldir;
3501
3502         if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3503                 goto out_no_task;
3504         nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3505
3506         reaper = get_proc_task(filp->f_path.dentry->d_inode);
3507         if (!reaper)
3508                 goto out_no_task;
3509
3510         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3511                 const struct pid_entry *p = &proc_base_stuff[nr];
3512                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3513                         goto out;
3514         }
3515
3516         ns = filp->f_dentry->d_sb->s_fs_info;
3517         iter.task = NULL;
3518         iter.tgid = filp->f_pos - TGID_OFFSET;
3519         for (iter = next_tgid(ns, iter);
3520              iter.task;
3521              iter.tgid += 1, iter = next_tgid(ns, iter)) {
3522                 if (has_pid_permissions(ns, iter.task, 2))
3523                         __filldir = filldir;
3524                 else
3525                         __filldir = fake_filldir;
3526
3527                 filp->f_pos = iter.tgid + TGID_OFFSET;
3528                 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3529                         put_task_struct(iter.task);
3530                         goto out;
3531                 }
3532         }
3533         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3534 out:
3535         put_task_struct(reaper);
3536 out_no_task:
3537         return 0;
3538 }
3539
3540 /*
3541  * Tasks
3542  */
3543 static const struct pid_entry tid_base_stuff[] = {
3544         DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3545         DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3546         DIR("ns",        S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3547         REG("environ",   S_IRUSR, proc_environ_operations),
3548         INF("auxv",      S_IRUSR, proc_pid_auxv),
3549         ONE("status",    S_IRUGO, proc_pid_status),
3550         ONE("personality", S_IRUGO, proc_pid_personality),
3551         INF("limits",    S_IRUGO, proc_pid_limits),
3552 #ifdef CONFIG_SCHED_DEBUG
3553         REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3554 #endif
3555         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3556 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3557         INF("syscall",   S_IRUGO, proc_pid_syscall),
3558 #endif
3559         INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3560         ONE("stat",      S_IRUGO, proc_tid_stat),
3561         ONE("statm",     S_IRUGO, proc_pid_statm),
3562         REG("maps",      S_IRUGO, proc_maps_operations),
3563 #ifdef CONFIG_NUMA
3564         REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3565 #endif
3566         REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3567         LNK("cwd",       proc_cwd_link),
3568         LNK("root",      proc_root_link),
3569         LNK("exe",       proc_exe_link),
3570         REG("mounts",    S_IRUGO, proc_mounts_operations),
3571         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3572 #ifdef CONFIG_PROC_PAGE_MONITOR
3573         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3574         REG("smaps",     S_IRUGO, proc_smaps_operations),
3575         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3576 #endif
3577 #ifdef CONFIG_SECURITY
3578         DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3579 #endif
3580 #ifdef CONFIG_KALLSYMS
3581         INF("wchan",     S_IRUGO, proc_pid_wchan),
3582 #endif
3583 #ifdef CONFIG_STACKTRACE
3584         ONE("stack",      S_IRUGO, proc_pid_stack),
3585 #endif
3586 #ifdef CONFIG_SCHEDSTATS
3587         INF("schedstat", S_IRUGO, proc_pid_schedstat),
3588 #endif
3589 #ifdef CONFIG_LATENCYTOP
3590         REG("latency",  S_IRUGO, proc_lstats_operations),
3591 #endif
3592 #ifdef CONFIG_PROC_PID_CPUSET
3593         REG("cpuset",    S_IRUGO, proc_cpuset_operations),
3594 #endif
3595 #ifdef CONFIG_CGROUPS
3596         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3597 #endif
3598         INF("oom_score", S_IRUGO, proc_oom_score),
3599         REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3600         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3601 #ifdef CONFIG_AUDITSYSCALL
3602         REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3603         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3604 #endif
3605 #ifdef CONFIG_FAULT_INJECTION
3606         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3607 #endif
3608 #ifdef CONFIG_TASK_IO_ACCOUNTING
3609         INF("io",       S_IRUSR, proc_tid_io_accounting),
3610 #endif
3611 #ifdef CONFIG_HARDWALL
3612         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3613 #endif
3614 };
3615
3616 static int proc_tid_base_readdir(struct file * filp,
3617                              void * dirent, filldir_t filldir)
3618 {
3619         return proc_pident_readdir(filp,dirent,filldir,
3620                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3621 }
3622
3623 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3624         return proc_pident_lookup(dir, dentry,
3625                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3626 }
3627
3628 static const struct file_operations proc_tid_base_operations = {
3629         .read           = generic_read_dir,
3630         .readdir        = proc_tid_base_readdir,
3631         .llseek         = default_llseek,
3632 };
3633
3634 static const struct inode_operations proc_tid_base_inode_operations = {
3635         .lookup         = proc_tid_base_lookup,
3636         .getattr        = pid_getattr,
3637         .setattr        = proc_setattr,
3638 };
3639
3640 static struct dentry *proc_task_instantiate(struct inode *dir,
3641         struct dentry *dentry, struct task_struct *task, const void *ptr)
3642 {
3643         struct dentry *error = ERR_PTR(-ENOENT);
3644         struct inode *inode;
3645         inode = proc_pid_make_inode(dir->i_sb, task);
3646
3647         if (!inode)
3648                 goto out;
3649         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3650         inode->i_op = &proc_tid_base_inode_operations;
3651         inode->i_fop = &proc_tid_base_operations;
3652         inode->i_flags|=S_IMMUTABLE;
3653
3654         set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3655                                                   ARRAY_SIZE(tid_base_stuff)));
3656
3657         d_set_d_op(dentry, &pid_dentry_operations);
3658
3659         d_add(dentry, inode);
3660         /* Close the race of the process dying before we return the dentry */
3661         if (pid_revalidate(dentry, NULL))
3662                 error = NULL;
3663 out:
3664         return error;
3665 }
3666
3667 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3668 {
3669         struct dentry *result = ERR_PTR(-ENOENT);
3670         struct task_struct *task;
3671         struct task_struct *leader = get_proc_task(dir);
3672         unsigned tid;
3673         struct pid_namespace *ns;
3674
3675         if (!leader)
3676                 goto out_no_task;
3677
3678         tid = name_to_int(dentry);
3679         if (tid == ~0U)
3680                 goto out;
3681
3682         ns = dentry->d_sb->s_fs_info;
3683         rcu_read_lock();
3684         task = find_task_by_pid_ns(tid, ns);
3685         if (task)
3686                 get_task_struct(task);
3687         rcu_read_unlock();
3688         if (!task)
3689                 goto out;
3690         if (!same_thread_group(leader, task))
3691                 goto out_drop_task;
3692
3693         result = proc_task_instantiate(dir, dentry, task, NULL);
3694 out_drop_task:
3695         put_task_struct(task);
3696 out:
3697         put_task_struct(leader);
3698 out_no_task:
3699         return result;
3700 }
3701
3702 /*
3703  * Find the first tid of a thread group to return to user space.
3704  *
3705  * Usually this is just the thread group leader, but if the users
3706  * buffer was too small or there was a seek into the middle of the
3707  * directory we have more work todo.
3708  *
3709  * In the case of a short read we start with find_task_by_pid.
3710  *
3711  * In the case of a seek we start with the leader and walk nr
3712  * threads past it.
3713  */
3714 static struct task_struct *first_tid(struct task_struct *leader,
3715                 int tid, int nr, struct pid_namespace *ns)
3716 {
3717         struct task_struct *pos;
3718
3719         rcu_read_lock();
3720         /* Attempt to start with the pid of a thread */
3721         if (tid && (nr > 0)) {
3722                 pos = find_task_by_pid_ns(tid, ns);
3723                 if (pos && (pos->group_leader == leader))
3724                         goto found;
3725         }
3726
3727         /* If nr exceeds the number of threads there is nothing todo */
3728         pos = NULL;
3729         if (nr && nr >= get_nr_threads(leader))
3730                 goto out;
3731
3732         /* If we haven't found our starting place yet start
3733          * with the leader and walk nr threads forward.
3734          */
3735         for (pos = leader; nr > 0; --nr) {
3736                 pos = next_thread(pos);
3737                 if (pos == leader) {
3738                         pos = NULL;
3739                         goto out;
3740                 }
3741         }
3742 found:
3743         get_task_struct(pos);
3744 out:
3745         rcu_read_unlock();
3746         return pos;
3747 }
3748
3749 /*
3750  * Find the next thread in the thread list.
3751  * Return NULL if there is an error or no next thread.
3752  *
3753  * The reference to the input task_struct is released.
3754  */
3755 static struct task_struct *next_tid(struct task_struct *start)
3756 {
3757         struct task_struct *pos = NULL;
3758         rcu_read_lock();
3759         if (pid_alive(start)) {
3760                 pos = next_thread(start);
3761                 if (thread_group_leader(pos))
3762                         pos = NULL;
3763                 else
3764                         get_task_struct(pos);
3765         }
3766         rcu_read_unlock();
3767         put_task_struct(start);
3768         return pos;
3769 }
3770
3771 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3772         struct task_struct *task, int tid)
3773 {
3774         char name[PROC_NUMBUF];
3775         int len = snprintf(name, sizeof(name), "%d", tid);
3776         return proc_fill_cache(filp, dirent, filldir, name, len,
3777                                 proc_task_instantiate, task, NULL);
3778 }
3779
3780 /* for the /proc/TGID/task/ directories */
3781 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3782 {
3783         struct dentry *dentry = filp->f_path.dentry;
3784         struct inode *inode = dentry->d_inode;
3785         struct task_struct *leader = NULL;
3786         struct task_struct *task;
3787         int retval = -ENOENT;
3788         ino_t ino;
3789         int tid;
3790         struct pid_namespace *ns;
3791
3792         task = get_proc_task(inode);
3793         if (!task)
3794                 goto out_no_task;
3795         rcu_read_lock();
3796         if (pid_alive(task)) {
3797                 leader = task->group_leader;
3798                 get_task_struct(leader);
3799         }
3800         rcu_read_unlock();
3801         put_task_struct(task);
3802         if (!leader)
3803                 goto out_no_task;
3804         retval = 0;
3805
3806         switch ((unsigned long)filp->f_pos) {
3807         case 0:
3808                 ino = inode->i_ino;
3809                 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3810                         goto out;
3811                 filp->f_pos++;
3812                 /* fall through */
3813         case 1:
3814                 ino = parent_ino(dentry);
3815                 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3816                         goto out;
3817                 filp->f_pos++;
3818                 /* fall through */
3819         }
3820
3821         /* f_version caches the tgid value that the last readdir call couldn't
3822          * return. lseek aka telldir automagically resets f_version to 0.
3823          */
3824         ns = filp->f_dentry->d_sb->s_fs_info;
3825         tid = (int)filp->f_version;
3826         filp->f_version = 0;
3827         for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3828              task;
3829              task = next_tid(task), filp->f_pos++) {
3830                 tid = task_pid_nr_ns(task, ns);
3831                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3832                         /* returning this tgid failed, save it as the first
3833                          * pid for the next readir call */
3834                         filp->f_version = (u64)tid;
3835                         put_task_struct(task);
3836                         break;
3837                 }
3838         }
3839 out:
3840         put_task_struct(leader);
3841 out_no_task:
3842         return retval;
3843 }
3844
3845 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3846 {
3847         struct inode *inode = dentry->d_inode;
3848         struct task_struct *p = get_proc_task(inode);
3849         generic_fillattr(inode, stat);
3850
3851         if (p) {
3852                 stat->nlink += get_nr_threads(p);
3853                 put_task_struct(p);
3854         }
3855
3856         return 0;
3857 }
3858
3859 static const struct inode_operations proc_task_inode_operations = {
3860         .lookup         = proc_task_lookup,
3861         .getattr        = proc_task_getattr,
3862         .setattr        = proc_setattr,
3863         .permission     = proc_pid_permission,
3864 };
3865
3866 static const struct file_operations proc_task_operations = {
3867         .read           = generic_read_dir,
3868         .readdir        = proc_task_readdir,
3869         .llseek         = default_llseek,
3870 };