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