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