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