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