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