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