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