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