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