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