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