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