[PATCH] proc: remove the useless SMP-safe comments from /proc
[safe/jmp/linux-2.6] / fs / proc / base.c
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49
50 #include <asm/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
75 #include "internal.h"
76
77 /* NOTE:
78  *      Implementing inode permission operations in /proc is almost
79  *      certainly an error.  Permission checks need to happen during
80  *      each system call not at open time.  The reason is that most of
81  *      what we wish to check for permissions in /proc varies at runtime.
82  *
83  *      The classic example of a problem is opening file descriptors
84  *      in /proc for a task before it execs a suid executable.
85  */
86
87
88 /* Worst case buffer size needed for holding an integer. */
89 #define PROC_NUMBUF 10
90
91 struct pid_entry {
92         int len;
93         char *name;
94         mode_t mode;
95         struct inode_operations *iop;
96         struct file_operations *fop;
97         union proc_op op;
98 };
99
100 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
101         .len  = sizeof(NAME) - 1,                       \
102         .name = (NAME),                                 \
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
125 static struct fs_struct *get_fs_struct(struct task_struct *task)
126 {
127         struct fs_struct *fs;
128         task_lock(task);
129         fs = task->fs;
130         if(fs)
131                 atomic_inc(&fs->count);
132         task_unlock(task);
133         return fs;
134 }
135
136 static int get_nr_threads(struct task_struct *tsk)
137 {
138         /* Must be called with the rcu_read_lock held */
139         unsigned long flags;
140         int count = 0;
141
142         if (lock_task_sighand(tsk, &flags)) {
143                 count = atomic_read(&tsk->signal->count);
144                 unlock_task_sighand(tsk, &flags);
145         }
146         return count;
147 }
148
149 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
150 {
151         struct task_struct *task = get_proc_task(inode);
152         struct fs_struct *fs = NULL;
153         int result = -ENOENT;
154
155         if (task) {
156                 fs = get_fs_struct(task);
157                 put_task_struct(task);
158         }
159         if (fs) {
160                 read_lock(&fs->lock);
161                 *mnt = mntget(fs->pwdmnt);
162                 *dentry = dget(fs->pwd);
163                 read_unlock(&fs->lock);
164                 result = 0;
165                 put_fs_struct(fs);
166         }
167         return result;
168 }
169
170 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
171 {
172         struct task_struct *task = get_proc_task(inode);
173         struct fs_struct *fs = NULL;
174         int result = -ENOENT;
175
176         if (task) {
177                 fs = get_fs_struct(task);
178                 put_task_struct(task);
179         }
180         if (fs) {
181                 read_lock(&fs->lock);
182                 *mnt = mntget(fs->rootmnt);
183                 *dentry = dget(fs->root);
184                 read_unlock(&fs->lock);
185                 result = 0;
186                 put_fs_struct(fs);
187         }
188         return result;
189 }
190
191 #define MAY_PTRACE(task) \
192         (task == current || \
193         (task->parent == current && \
194         (task->ptrace & PT_PTRACED) && \
195          (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
196          security_ptrace(current,task) == 0))
197
198 static int proc_pid_environ(struct task_struct *task, char * buffer)
199 {
200         int res = 0;
201         struct mm_struct *mm = get_task_mm(task);
202         if (mm) {
203                 unsigned int len = mm->env_end - mm->env_start;
204                 if (len > PAGE_SIZE)
205                         len = PAGE_SIZE;
206                 res = access_process_vm(task, mm->env_start, buffer, len, 0);
207                 if (!ptrace_may_attach(task))
208                         res = -ESRCH;
209                 mmput(mm);
210         }
211         return res;
212 }
213
214 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
215 {
216         int res = 0;
217         unsigned int len;
218         struct mm_struct *mm = get_task_mm(task);
219         if (!mm)
220                 goto out;
221         if (!mm->arg_end)
222                 goto out_mm;    /* Shh! No looking before we're done */
223
224         len = mm->arg_end - mm->arg_start;
225  
226         if (len > PAGE_SIZE)
227                 len = PAGE_SIZE;
228  
229         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
230
231         // If the nul at the end of args has been overwritten, then
232         // assume application is using setproctitle(3).
233         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
234                 len = strnlen(buffer, res);
235                 if (len < res) {
236                     res = len;
237                 } else {
238                         len = mm->env_end - mm->env_start;
239                         if (len > PAGE_SIZE - res)
240                                 len = PAGE_SIZE - res;
241                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
242                         res = strnlen(buffer, res);
243                 }
244         }
245 out_mm:
246         mmput(mm);
247 out:
248         return res;
249 }
250
251 static int proc_pid_auxv(struct task_struct *task, char *buffer)
252 {
253         int res = 0;
254         struct mm_struct *mm = get_task_mm(task);
255         if (mm) {
256                 unsigned int nwords = 0;
257                 do
258                         nwords += 2;
259                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
260                 res = nwords * sizeof(mm->saved_auxv[0]);
261                 if (res > PAGE_SIZE)
262                         res = PAGE_SIZE;
263                 memcpy(buffer, mm->saved_auxv, res);
264                 mmput(mm);
265         }
266         return res;
267 }
268
269
270 #ifdef CONFIG_KALLSYMS
271 /*
272  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
273  * Returns the resolved symbol.  If that fails, simply return the address.
274  */
275 static int proc_pid_wchan(struct task_struct *task, char *buffer)
276 {
277         char *modname;
278         const char *sym_name;
279         unsigned long wchan, size, offset;
280         char namebuf[KSYM_NAME_LEN+1];
281
282         wchan = get_wchan(task);
283
284         sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
285         if (sym_name)
286                 return sprintf(buffer, "%s", sym_name);
287         return sprintf(buffer, "%lu", wchan);
288 }
289 #endif /* CONFIG_KALLSYMS */
290
291 #ifdef CONFIG_SCHEDSTATS
292 /*
293  * Provides /proc/PID/schedstat
294  */
295 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
296 {
297         return sprintf(buffer, "%lu %lu %lu\n",
298                         task->sched_info.cpu_time,
299                         task->sched_info.run_delay,
300                         task->sched_info.pcnt);
301 }
302 #endif
303
304 /* The badness from the OOM killer */
305 unsigned long badness(struct task_struct *p, unsigned long uptime);
306 static int proc_oom_score(struct task_struct *task, char *buffer)
307 {
308         unsigned long points;
309         struct timespec uptime;
310
311         do_posix_clock_monotonic_gettime(&uptime);
312         points = badness(task, uptime.tv_sec);
313         return sprintf(buffer, "%lu\n", points);
314 }
315
316 /************************************************************************/
317 /*                       Here the fs part begins                        */
318 /************************************************************************/
319
320 /* permission checks */
321 static int proc_fd_access_allowed(struct inode *inode)
322 {
323         struct task_struct *task;
324         int allowed = 0;
325         /* Allow access to a task's file descriptors if it is us or we
326          * may use ptrace attach to the process and find out that
327          * information.
328          */
329         task = get_proc_task(inode);
330         if (task) {
331                 allowed = ptrace_may_attach(task);
332                 put_task_struct(task);
333         }
334         return allowed;
335 }
336
337 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
338 {
339         int error;
340         struct inode *inode = dentry->d_inode;
341
342         if (attr->ia_valid & ATTR_MODE)
343                 return -EPERM;
344
345         error = inode_change_ok(inode, attr);
346         if (!error) {
347                 error = security_inode_setattr(dentry, attr);
348                 if (!error)
349                         error = inode_setattr(inode, attr);
350         }
351         return error;
352 }
353
354 static struct inode_operations proc_def_inode_operations = {
355         .setattr        = proc_setattr,
356 };
357
358 extern struct seq_operations mounts_op;
359 struct proc_mounts {
360         struct seq_file m;
361         int event;
362 };
363
364 static int mounts_open(struct inode *inode, struct file *file)
365 {
366         struct task_struct *task = get_proc_task(inode);
367         struct namespace *namespace = NULL;
368         struct proc_mounts *p;
369         int ret = -EINVAL;
370
371         if (task) {
372                 task_lock(task);
373                 namespace = task->nsproxy->namespace;
374                 if (namespace)
375                         get_namespace(namespace);
376                 task_unlock(task);
377                 put_task_struct(task);
378         }
379
380         if (namespace) {
381                 ret = -ENOMEM;
382                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
383                 if (p) {
384                         file->private_data = &p->m;
385                         ret = seq_open(file, &mounts_op);
386                         if (!ret) {
387                                 p->m.private = namespace;
388                                 p->event = namespace->event;
389                                 return 0;
390                         }
391                         kfree(p);
392                 }
393                 put_namespace(namespace);
394         }
395         return ret;
396 }
397
398 static int mounts_release(struct inode *inode, struct file *file)
399 {
400         struct seq_file *m = file->private_data;
401         struct namespace *namespace = m->private;
402         put_namespace(namespace);
403         return seq_release(inode, file);
404 }
405
406 static unsigned mounts_poll(struct file *file, poll_table *wait)
407 {
408         struct proc_mounts *p = file->private_data;
409         struct namespace *ns = p->m.private;
410         unsigned res = 0;
411
412         poll_wait(file, &ns->poll, wait);
413
414         spin_lock(&vfsmount_lock);
415         if (p->event != ns->event) {
416                 p->event = ns->event;
417                 res = POLLERR;
418         }
419         spin_unlock(&vfsmount_lock);
420
421         return res;
422 }
423
424 static struct file_operations proc_mounts_operations = {
425         .open           = mounts_open,
426         .read           = seq_read,
427         .llseek         = seq_lseek,
428         .release        = mounts_release,
429         .poll           = mounts_poll,
430 };
431
432 extern struct seq_operations mountstats_op;
433 static int mountstats_open(struct inode *inode, struct file *file)
434 {
435         int ret = seq_open(file, &mountstats_op);
436
437         if (!ret) {
438                 struct seq_file *m = file->private_data;
439                 struct namespace *namespace = NULL;
440                 struct task_struct *task = get_proc_task(inode);
441
442                 if (task) {
443                         task_lock(task);
444                         namespace = task->nsproxy->namespace;
445                         if (namespace)
446                                 get_namespace(namespace);
447                         task_unlock(task);
448                         put_task_struct(task);
449                 }
450
451                 if (namespace)
452                         m->private = namespace;
453                 else {
454                         seq_release(inode, file);
455                         ret = -EINVAL;
456                 }
457         }
458         return ret;
459 }
460
461 static struct file_operations proc_mountstats_operations = {
462         .open           = mountstats_open,
463         .read           = seq_read,
464         .llseek         = seq_lseek,
465         .release        = mounts_release,
466 };
467
468 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
469
470 static ssize_t proc_info_read(struct file * file, char __user * buf,
471                           size_t count, loff_t *ppos)
472 {
473         struct inode * inode = file->f_dentry->d_inode;
474         unsigned long page;
475         ssize_t length;
476         struct task_struct *task = get_proc_task(inode);
477
478         length = -ESRCH;
479         if (!task)
480                 goto out_no_task;
481
482         if (count > PROC_BLOCK_SIZE)
483                 count = PROC_BLOCK_SIZE;
484
485         length = -ENOMEM;
486         if (!(page = __get_free_page(GFP_KERNEL)))
487                 goto out;
488
489         length = PROC_I(inode)->op.proc_read(task, (char*)page);
490
491         if (length >= 0)
492                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
493         free_page(page);
494 out:
495         put_task_struct(task);
496 out_no_task:
497         return length;
498 }
499
500 static struct file_operations proc_info_file_operations = {
501         .read           = proc_info_read,
502 };
503
504 static int mem_open(struct inode* inode, struct file* file)
505 {
506         file->private_data = (void*)((long)current->self_exec_id);
507         return 0;
508 }
509
510 static ssize_t mem_read(struct file * file, char __user * buf,
511                         size_t count, loff_t *ppos)
512 {
513         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
514         char *page;
515         unsigned long src = *ppos;
516         int ret = -ESRCH;
517         struct mm_struct *mm;
518
519         if (!task)
520                 goto out_no_task;
521
522         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
523                 goto out;
524
525         ret = -ENOMEM;
526         page = (char *)__get_free_page(GFP_USER);
527         if (!page)
528                 goto out;
529
530         ret = 0;
531  
532         mm = get_task_mm(task);
533         if (!mm)
534                 goto out_free;
535
536         ret = -EIO;
537  
538         if (file->private_data != (void*)((long)current->self_exec_id))
539                 goto out_put;
540
541         ret = 0;
542  
543         while (count > 0) {
544                 int this_len, retval;
545
546                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
547                 retval = access_process_vm(task, src, page, this_len, 0);
548                 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
549                         if (!ret)
550                                 ret = -EIO;
551                         break;
552                 }
553
554                 if (copy_to_user(buf, page, retval)) {
555                         ret = -EFAULT;
556                         break;
557                 }
558  
559                 ret += retval;
560                 src += retval;
561                 buf += retval;
562                 count -= retval;
563         }
564         *ppos = src;
565
566 out_put:
567         mmput(mm);
568 out_free:
569         free_page((unsigned long) page);
570 out:
571         put_task_struct(task);
572 out_no_task:
573         return ret;
574 }
575
576 #define mem_write NULL
577
578 #ifndef mem_write
579 /* This is a security hazard */
580 static ssize_t mem_write(struct file * file, const char * buf,
581                          size_t count, loff_t *ppos)
582 {
583         int copied;
584         char *page;
585         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
586         unsigned long dst = *ppos;
587
588         copied = -ESRCH;
589         if (!task)
590                 goto out_no_task;
591
592         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
593                 goto out;
594
595         copied = -ENOMEM;
596         page = (char *)__get_free_page(GFP_USER);
597         if (!page)
598                 goto out;
599
600         copied = 0;
601         while (count > 0) {
602                 int this_len, retval;
603
604                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
605                 if (copy_from_user(page, buf, this_len)) {
606                         copied = -EFAULT;
607                         break;
608                 }
609                 retval = access_process_vm(task, dst, page, this_len, 1);
610                 if (!retval) {
611                         if (!copied)
612                                 copied = -EIO;
613                         break;
614                 }
615                 copied += retval;
616                 buf += retval;
617                 dst += retval;
618                 count -= retval;                        
619         }
620         *ppos = dst;
621         free_page((unsigned long) page);
622 out:
623         put_task_struct(task);
624 out_no_task:
625         return copied;
626 }
627 #endif
628
629 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
630 {
631         switch (orig) {
632         case 0:
633                 file->f_pos = offset;
634                 break;
635         case 1:
636                 file->f_pos += offset;
637                 break;
638         default:
639                 return -EINVAL;
640         }
641         force_successful_syscall_return();
642         return file->f_pos;
643 }
644
645 static struct file_operations proc_mem_operations = {
646         .llseek         = mem_lseek,
647         .read           = mem_read,
648         .write          = mem_write,
649         .open           = mem_open,
650 };
651
652 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
653                                 size_t count, loff_t *ppos)
654 {
655         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
656         char buffer[PROC_NUMBUF];
657         size_t len;
658         int oom_adjust;
659         loff_t __ppos = *ppos;
660
661         if (!task)
662                 return -ESRCH;
663         oom_adjust = task->oomkilladj;
664         put_task_struct(task);
665
666         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
667         if (__ppos >= len)
668                 return 0;
669         if (count > len-__ppos)
670                 count = len-__ppos;
671         if (copy_to_user(buf, buffer + __ppos, count))
672                 return -EFAULT;
673         *ppos = __ppos + count;
674         return count;
675 }
676
677 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
678                                 size_t count, loff_t *ppos)
679 {
680         struct task_struct *task;
681         char buffer[PROC_NUMBUF], *end;
682         int oom_adjust;
683
684         if (!capable(CAP_SYS_RESOURCE))
685                 return -EPERM;
686         memset(buffer, 0, sizeof(buffer));
687         if (count > sizeof(buffer) - 1)
688                 count = sizeof(buffer) - 1;
689         if (copy_from_user(buffer, buf, count))
690                 return -EFAULT;
691         oom_adjust = simple_strtol(buffer, &end, 0);
692         if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
693                 return -EINVAL;
694         if (*end == '\n')
695                 end++;
696         task = get_proc_task(file->f_dentry->d_inode);
697         if (!task)
698                 return -ESRCH;
699         task->oomkilladj = oom_adjust;
700         put_task_struct(task);
701         if (end - buffer == 0)
702                 return -EIO;
703         return end - buffer;
704 }
705
706 static struct file_operations proc_oom_adjust_operations = {
707         .read           = oom_adjust_read,
708         .write          = oom_adjust_write,
709 };
710
711 #ifdef CONFIG_AUDITSYSCALL
712 #define TMPBUFLEN 21
713 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
714                                   size_t count, loff_t *ppos)
715 {
716         struct inode * inode = file->f_dentry->d_inode;
717         struct task_struct *task = get_proc_task(inode);
718         ssize_t length;
719         char tmpbuf[TMPBUFLEN];
720
721         if (!task)
722                 return -ESRCH;
723         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
724                                 audit_get_loginuid(task->audit_context));
725         put_task_struct(task);
726         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
727 }
728
729 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
730                                    size_t count, loff_t *ppos)
731 {
732         struct inode * inode = file->f_dentry->d_inode;
733         char *page, *tmp;
734         ssize_t length;
735         uid_t loginuid;
736
737         if (!capable(CAP_AUDIT_CONTROL))
738                 return -EPERM;
739
740         if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
741                 return -EPERM;
742
743         if (count >= PAGE_SIZE)
744                 count = PAGE_SIZE - 1;
745
746         if (*ppos != 0) {
747                 /* No partial writes. */
748                 return -EINVAL;
749         }
750         page = (char*)__get_free_page(GFP_USER);
751         if (!page)
752                 return -ENOMEM;
753         length = -EFAULT;
754         if (copy_from_user(page, buf, count))
755                 goto out_free_page;
756
757         page[count] = '\0';
758         loginuid = simple_strtoul(page, &tmp, 10);
759         if (tmp == page) {
760                 length = -EINVAL;
761                 goto out_free_page;
762
763         }
764         length = audit_set_loginuid(current, loginuid);
765         if (likely(length == 0))
766                 length = count;
767
768 out_free_page:
769         free_page((unsigned long) page);
770         return length;
771 }
772
773 static struct file_operations proc_loginuid_operations = {
774         .read           = proc_loginuid_read,
775         .write          = proc_loginuid_write,
776 };
777 #endif
778
779 #ifdef CONFIG_SECCOMP
780 static ssize_t seccomp_read(struct file *file, char __user *buf,
781                             size_t count, loff_t *ppos)
782 {
783         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
784         char __buf[20];
785         loff_t __ppos = *ppos;
786         size_t len;
787
788         if (!tsk)
789                 return -ESRCH;
790         /* no need to print the trailing zero, so use only len */
791         len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
792         put_task_struct(tsk);
793         if (__ppos >= len)
794                 return 0;
795         if (count > len - __ppos)
796                 count = len - __ppos;
797         if (copy_to_user(buf, __buf + __ppos, count))
798                 return -EFAULT;
799         *ppos = __ppos + count;
800         return count;
801 }
802
803 static ssize_t seccomp_write(struct file *file, const char __user *buf,
804                              size_t count, loff_t *ppos)
805 {
806         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
807         char __buf[20], *end;
808         unsigned int seccomp_mode;
809         ssize_t result;
810
811         result = -ESRCH;
812         if (!tsk)
813                 goto out_no_task;
814
815         /* can set it only once to be even more secure */
816         result = -EPERM;
817         if (unlikely(tsk->seccomp.mode))
818                 goto out;
819
820         result = -EFAULT;
821         memset(__buf, 0, sizeof(__buf));
822         count = min(count, sizeof(__buf) - 1);
823         if (copy_from_user(__buf, buf, count))
824                 goto out;
825
826         seccomp_mode = simple_strtoul(__buf, &end, 0);
827         if (*end == '\n')
828                 end++;
829         result = -EINVAL;
830         if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
831                 tsk->seccomp.mode = seccomp_mode;
832                 set_tsk_thread_flag(tsk, TIF_SECCOMP);
833         } else
834                 goto out;
835         result = -EIO;
836         if (unlikely(!(end - __buf)))
837                 goto out;
838         result = end - __buf;
839 out:
840         put_task_struct(tsk);
841 out_no_task:
842         return result;
843 }
844
845 static struct file_operations proc_seccomp_operations = {
846         .read           = seccomp_read,
847         .write          = seccomp_write,
848 };
849 #endif /* CONFIG_SECCOMP */
850
851 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
852 {
853         struct inode *inode = dentry->d_inode;
854         int error = -EACCES;
855
856         /* We don't need a base pointer in the /proc filesystem */
857         path_release(nd);
858
859         /* Are we allowed to snoop on the tasks file descriptors? */
860         if (!proc_fd_access_allowed(inode))
861                 goto out;
862
863         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
864         nd->last_type = LAST_BIND;
865 out:
866         return ERR_PTR(error);
867 }
868
869 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
870                             char __user *buffer, int buflen)
871 {
872         struct inode * inode;
873         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
874         int len;
875
876         if (!tmp)
877                 return -ENOMEM;
878                 
879         inode = dentry->d_inode;
880         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
881         len = PTR_ERR(path);
882         if (IS_ERR(path))
883                 goto out;
884         len = tmp + PAGE_SIZE - 1 - path;
885
886         if (len > buflen)
887                 len = buflen;
888         if (copy_to_user(buffer, path, len))
889                 len = -EFAULT;
890  out:
891         free_page((unsigned long)tmp);
892         return len;
893 }
894
895 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
896 {
897         int error = -EACCES;
898         struct inode *inode = dentry->d_inode;
899         struct dentry *de;
900         struct vfsmount *mnt = NULL;
901
902         /* Are we allowed to snoop on the tasks file descriptors? */
903         if (!proc_fd_access_allowed(inode))
904                 goto out;
905
906         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
907         if (error)
908                 goto out;
909
910         error = do_proc_readlink(de, mnt, buffer, buflen);
911         dput(de);
912         mntput(mnt);
913 out:
914         return error;
915 }
916
917 static struct inode_operations proc_pid_link_inode_operations = {
918         .readlink       = proc_pid_readlink,
919         .follow_link    = proc_pid_follow_link,
920         .setattr        = proc_setattr,
921 };
922
923
924 /* building an inode */
925
926 static int task_dumpable(struct task_struct *task)
927 {
928         int dumpable = 0;
929         struct mm_struct *mm;
930
931         task_lock(task);
932         mm = task->mm;
933         if (mm)
934                 dumpable = mm->dumpable;
935         task_unlock(task);
936         if(dumpable == 1)
937                 return 1;
938         return 0;
939 }
940
941
942 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
943 {
944         struct inode * inode;
945         struct proc_inode *ei;
946
947         /* We need a new inode */
948
949         inode = new_inode(sb);
950         if (!inode)
951                 goto out;
952
953         /* Common stuff */
954         ei = PROC_I(inode);
955         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
956         inode->i_op = &proc_def_inode_operations;
957
958         /*
959          * grab the reference to task.
960          */
961         ei->pid = get_pid(task_pid(task));
962         if (!ei->pid)
963                 goto out_unlock;
964
965         inode->i_uid = 0;
966         inode->i_gid = 0;
967         if (task_dumpable(task)) {
968                 inode->i_uid = task->euid;
969                 inode->i_gid = task->egid;
970         }
971         security_task_to_inode(task, inode);
972
973 out:
974         return inode;
975
976 out_unlock:
977         iput(inode);
978         return NULL;
979 }
980
981 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
982 {
983         struct inode *inode = dentry->d_inode;
984         struct task_struct *task;
985         generic_fillattr(inode, stat);
986
987         rcu_read_lock();
988         stat->uid = 0;
989         stat->gid = 0;
990         task = pid_task(proc_pid(inode), PIDTYPE_PID);
991         if (task) {
992                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
993                     task_dumpable(task)) {
994                         stat->uid = task->euid;
995                         stat->gid = task->egid;
996                 }
997         }
998         rcu_read_unlock();
999         return 0;
1000 }
1001
1002 /* dentry stuff */
1003
1004 /*
1005  *      Exceptional case: normally we are not allowed to unhash a busy
1006  * directory. In this case, however, we can do it - no aliasing problems
1007  * due to the way we treat inodes.
1008  *
1009  * Rewrite the inode's ownerships here because the owning task may have
1010  * performed a setuid(), etc.
1011  *
1012  * Before the /proc/pid/status file was created the only way to read
1013  * the effective uid of a /process was to stat /proc/pid.  Reading
1014  * /proc/pid/status is slow enough that procps and other packages
1015  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1016  * made this apply to all per process world readable and executable
1017  * directories.
1018  */
1019 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1020 {
1021         struct inode *inode = dentry->d_inode;
1022         struct task_struct *task = get_proc_task(inode);
1023         if (task) {
1024                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1025                     task_dumpable(task)) {
1026                         inode->i_uid = task->euid;
1027                         inode->i_gid = task->egid;
1028                 } else {
1029                         inode->i_uid = 0;
1030                         inode->i_gid = 0;
1031                 }
1032                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1033                 security_task_to_inode(task, inode);
1034                 put_task_struct(task);
1035                 return 1;
1036         }
1037         d_drop(dentry);
1038         return 0;
1039 }
1040
1041 static int pid_delete_dentry(struct dentry * dentry)
1042 {
1043         /* Is the task we represent dead?
1044          * If so, then don't put the dentry on the lru list,
1045          * kill it immediately.
1046          */
1047         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1048 }
1049
1050 static struct dentry_operations pid_dentry_operations =
1051 {
1052         .d_revalidate   = pid_revalidate,
1053         .d_delete       = pid_delete_dentry,
1054 };
1055
1056 /* Lookups */
1057
1058 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1059
1060 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1061         char *name, int len,
1062         instantiate_t instantiate, struct task_struct *task, void *ptr)
1063 {
1064         struct dentry *child, *dir = filp->f_dentry;
1065         struct inode *inode;
1066         struct qstr qname;
1067         ino_t ino = 0;
1068         unsigned type = DT_UNKNOWN;
1069
1070         qname.name = name;
1071         qname.len  = len;
1072         qname.hash = full_name_hash(name, len);
1073
1074         child = d_lookup(dir, &qname);
1075         if (!child) {
1076                 struct dentry *new;
1077                 new = d_alloc(dir, &qname);
1078                 if (new) {
1079                         child = instantiate(dir->d_inode, new, task, ptr);
1080                         if (child)
1081                                 dput(new);
1082                         else
1083                                 child = new;
1084                 }
1085         }
1086         if (!child || IS_ERR(child) || !child->d_inode)
1087                 goto end_instantiate;
1088         inode = child->d_inode;
1089         if (inode) {
1090                 ino = inode->i_ino;
1091                 type = inode->i_mode >> 12;
1092         }
1093         dput(child);
1094 end_instantiate:
1095         if (!ino)
1096                 ino = find_inode_number(dir, &qname);
1097         if (!ino)
1098                 ino = 1;
1099         return filldir(dirent, name, len, filp->f_pos, ino, type);
1100 }
1101
1102 static unsigned name_to_int(struct dentry *dentry)
1103 {
1104         const char *name = dentry->d_name.name;
1105         int len = dentry->d_name.len;
1106         unsigned n = 0;
1107
1108         if (len > 1 && *name == '0')
1109                 goto out;
1110         while (len-- > 0) {
1111                 unsigned c = *name++ - '0';
1112                 if (c > 9)
1113                         goto out;
1114                 if (n >= (~0U-9)/10)
1115                         goto out;
1116                 n *= 10;
1117                 n += c;
1118         }
1119         return n;
1120 out:
1121         return ~0U;
1122 }
1123
1124 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1125 {
1126         struct task_struct *task = get_proc_task(inode);
1127         struct files_struct *files = NULL;
1128         struct file *file;
1129         int fd = proc_fd(inode);
1130
1131         if (task) {
1132                 files = get_files_struct(task);
1133                 put_task_struct(task);
1134         }
1135         if (files) {
1136                 /*
1137                  * We are not taking a ref to the file structure, so we must
1138                  * hold ->file_lock.
1139                  */
1140                 spin_lock(&files->file_lock);
1141                 file = fcheck_files(files, fd);
1142                 if (file) {
1143                         *mnt = mntget(file->f_vfsmnt);
1144                         *dentry = dget(file->f_dentry);
1145                         spin_unlock(&files->file_lock);
1146                         put_files_struct(files);
1147                         return 0;
1148                 }
1149                 spin_unlock(&files->file_lock);
1150                 put_files_struct(files);
1151         }
1152         return -ENOENT;
1153 }
1154
1155 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1156 {
1157         struct inode *inode = dentry->d_inode;
1158         struct task_struct *task = get_proc_task(inode);
1159         int fd = proc_fd(inode);
1160         struct files_struct *files;
1161
1162         if (task) {
1163                 files = get_files_struct(task);
1164                 if (files) {
1165                         rcu_read_lock();
1166                         if (fcheck_files(files, fd)) {
1167                                 rcu_read_unlock();
1168                                 put_files_struct(files);
1169                                 if (task_dumpable(task)) {
1170                                         inode->i_uid = task->euid;
1171                                         inode->i_gid = task->egid;
1172                                 } else {
1173                                         inode->i_uid = 0;
1174                                         inode->i_gid = 0;
1175                                 }
1176                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1177                                 security_task_to_inode(task, inode);
1178                                 put_task_struct(task);
1179                                 return 1;
1180                         }
1181                         rcu_read_unlock();
1182                         put_files_struct(files);
1183                 }
1184                 put_task_struct(task);
1185         }
1186         d_drop(dentry);
1187         return 0;
1188 }
1189
1190 static struct dentry_operations tid_fd_dentry_operations =
1191 {
1192         .d_revalidate   = tid_fd_revalidate,
1193         .d_delete       = pid_delete_dentry,
1194 };
1195
1196 static struct dentry *proc_fd_instantiate(struct inode *dir,
1197         struct dentry *dentry, struct task_struct *task, void *ptr)
1198 {
1199         unsigned fd = *(unsigned *)ptr;
1200         struct file *file;
1201         struct files_struct *files;
1202         struct inode *inode;
1203         struct proc_inode *ei;
1204         struct dentry *error = ERR_PTR(-ENOENT);
1205
1206         inode = proc_pid_make_inode(dir->i_sb, task);
1207         if (!inode)
1208                 goto out;
1209         ei = PROC_I(inode);
1210         ei->fd = fd;
1211         files = get_files_struct(task);
1212         if (!files)
1213                 goto out_iput;
1214         inode->i_mode = S_IFLNK;
1215
1216         /*
1217          * We are not taking a ref to the file structure, so we must
1218          * hold ->file_lock.
1219          */
1220         spin_lock(&files->file_lock);
1221         file = fcheck_files(files, fd);
1222         if (!file)
1223                 goto out_unlock;
1224         if (file->f_mode & 1)
1225                 inode->i_mode |= S_IRUSR | S_IXUSR;
1226         if (file->f_mode & 2)
1227                 inode->i_mode |= S_IWUSR | S_IXUSR;
1228         spin_unlock(&files->file_lock);
1229         put_files_struct(files);
1230
1231         inode->i_op = &proc_pid_link_inode_operations;
1232         inode->i_size = 64;
1233         ei->op.proc_get_link = proc_fd_link;
1234         dentry->d_op = &tid_fd_dentry_operations;
1235         d_add(dentry, inode);
1236         /* Close the race of the process dying before we return the dentry */
1237         if (tid_fd_revalidate(dentry, NULL))
1238                 error = NULL;
1239
1240  out:
1241         return error;
1242 out_unlock:
1243         spin_unlock(&files->file_lock);
1244         put_files_struct(files);
1245 out_iput:
1246         iput(inode);
1247         goto out;
1248 }
1249
1250 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1251 {
1252         struct task_struct *task = get_proc_task(dir);
1253         unsigned fd = name_to_int(dentry);
1254         struct dentry *result = ERR_PTR(-ENOENT);
1255
1256         if (!task)
1257                 goto out_no_task;
1258         if (fd == ~0U)
1259                 goto out;
1260
1261         result = proc_fd_instantiate(dir, dentry, task, &fd);
1262 out:
1263         put_task_struct(task);
1264 out_no_task:
1265         return result;
1266 }
1267
1268 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1269         struct task_struct *task, int fd)
1270 {
1271         char name[PROC_NUMBUF];
1272         int len = snprintf(name, sizeof(name), "%d", fd);
1273         return proc_fill_cache(filp, dirent, filldir, name, len,
1274                                 proc_fd_instantiate, task, &fd);
1275 }
1276
1277 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1278 {
1279         struct dentry *dentry = filp->f_dentry;
1280         struct inode *inode = dentry->d_inode;
1281         struct task_struct *p = get_proc_task(inode);
1282         unsigned int fd, tid, ino;
1283         int retval;
1284         struct files_struct * files;
1285         struct fdtable *fdt;
1286
1287         retval = -ENOENT;
1288         if (!p)
1289                 goto out_no_task;
1290         retval = 0;
1291         tid = p->pid;
1292
1293         fd = filp->f_pos;
1294         switch (fd) {
1295                 case 0:
1296                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1297                                 goto out;
1298                         filp->f_pos++;
1299                 case 1:
1300                         ino = parent_ino(dentry);
1301                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1302                                 goto out;
1303                         filp->f_pos++;
1304                 default:
1305                         files = get_files_struct(p);
1306                         if (!files)
1307                                 goto out;
1308                         rcu_read_lock();
1309                         fdt = files_fdtable(files);
1310                         for (fd = filp->f_pos-2;
1311                              fd < fdt->max_fds;
1312                              fd++, filp->f_pos++) {
1313
1314                                 if (!fcheck_files(files, fd))
1315                                         continue;
1316                                 rcu_read_unlock();
1317
1318                                 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1319                                         rcu_read_lock();
1320                                         break;
1321                                 }
1322                                 rcu_read_lock();
1323                         }
1324                         rcu_read_unlock();
1325                         put_files_struct(files);
1326         }
1327 out:
1328         put_task_struct(p);
1329 out_no_task:
1330         return retval;
1331 }
1332
1333 static struct file_operations proc_fd_operations = {
1334         .read           = generic_read_dir,
1335         .readdir        = proc_readfd,
1336 };
1337
1338 /*
1339  * proc directories can do almost nothing..
1340  */
1341 static struct inode_operations proc_fd_inode_operations = {
1342         .lookup         = proc_lookupfd,
1343         .setattr        = proc_setattr,
1344 };
1345
1346 static struct dentry *proc_pident_instantiate(struct inode *dir,
1347         struct dentry *dentry, struct task_struct *task, void *ptr)
1348 {
1349         struct pid_entry *p = ptr;
1350         struct inode *inode;
1351         struct proc_inode *ei;
1352         struct dentry *error = ERR_PTR(-EINVAL);
1353
1354         inode = proc_pid_make_inode(dir->i_sb, task);
1355         if (!inode)
1356                 goto out;
1357
1358         ei = PROC_I(inode);
1359         inode->i_mode = p->mode;
1360         if (S_ISDIR(inode->i_mode))
1361                 inode->i_nlink = 2;     /* Use getattr to fix if necessary */
1362         if (p->iop)
1363                 inode->i_op = p->iop;
1364         if (p->fop)
1365                 inode->i_fop = p->fop;
1366         ei->op = p->op;
1367         dentry->d_op = &pid_dentry_operations;
1368         d_add(dentry, inode);
1369         /* Close the race of the process dying before we return the dentry */
1370         if (pid_revalidate(dentry, NULL))
1371                 error = NULL;
1372 out:
1373         return error;
1374 }
1375
1376 static struct dentry *proc_pident_lookup(struct inode *dir, 
1377                                          struct dentry *dentry,
1378                                          struct pid_entry *ents,
1379                                          unsigned int nents)
1380 {
1381         struct inode *inode;
1382         struct dentry *error;
1383         struct task_struct *task = get_proc_task(dir);
1384         struct pid_entry *p, *last;
1385
1386         error = ERR_PTR(-ENOENT);
1387         inode = NULL;
1388
1389         if (!task)
1390                 goto out_no_task;
1391
1392         /*
1393          * Yes, it does not scale. And it should not. Don't add
1394          * new entries into /proc/<tgid>/ without very good reasons.
1395          */
1396         last = &ents[nents - 1];
1397         for (p = ents; p <= last; p++) {
1398                 if (p->len != dentry->d_name.len)
1399                         continue;
1400                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1401                         break;
1402         }
1403         if (p > last)
1404                 goto out;
1405
1406         error = proc_pident_instantiate(dir, dentry, task, p);
1407 out:
1408         put_task_struct(task);
1409 out_no_task:
1410         return error;
1411 }
1412
1413 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1414         struct task_struct *task, struct pid_entry *p)
1415 {
1416         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1417                                 proc_pident_instantiate, task, p);
1418 }
1419
1420 static int proc_pident_readdir(struct file *filp,
1421                 void *dirent, filldir_t filldir,
1422                 struct pid_entry *ents, unsigned int nents)
1423 {
1424         int i;
1425         int pid;
1426         struct dentry *dentry = filp->f_dentry;
1427         struct inode *inode = dentry->d_inode;
1428         struct task_struct *task = get_proc_task(inode);
1429         struct pid_entry *p, *last;
1430         ino_t ino;
1431         int ret;
1432
1433         ret = -ENOENT;
1434         if (!task)
1435                 goto out_no_task;
1436
1437         ret = 0;
1438         pid = task->pid;
1439         i = filp->f_pos;
1440         switch (i) {
1441         case 0:
1442                 ino = inode->i_ino;
1443                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1444                         goto out;
1445                 i++;
1446                 filp->f_pos++;
1447                 /* fall through */
1448         case 1:
1449                 ino = parent_ino(dentry);
1450                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1451                         goto out;
1452                 i++;
1453                 filp->f_pos++;
1454                 /* fall through */
1455         default:
1456                 i -= 2;
1457                 if (i >= nents) {
1458                         ret = 1;
1459                         goto out;
1460                 }
1461                 p = ents + i;
1462                 last = &ents[nents - 1];
1463                 while (p <= last) {
1464                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1465                                 goto out;
1466                         filp->f_pos++;
1467                         p++;
1468                 }
1469         }
1470
1471         ret = 1;
1472 out:
1473         put_task_struct(task);
1474 out_no_task:
1475         return ret;
1476 }
1477
1478 #ifdef CONFIG_SECURITY
1479 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1480                                   size_t count, loff_t *ppos)
1481 {
1482         struct inode * inode = file->f_dentry->d_inode;
1483         unsigned long page;
1484         ssize_t length;
1485         struct task_struct *task = get_proc_task(inode);
1486
1487         length = -ESRCH;
1488         if (!task)
1489                 goto out_no_task;
1490
1491         if (count > PAGE_SIZE)
1492                 count = PAGE_SIZE;
1493         length = -ENOMEM;
1494         if (!(page = __get_free_page(GFP_KERNEL)))
1495                 goto out;
1496
1497         length = security_getprocattr(task,
1498                                       (char*)file->f_dentry->d_name.name,
1499                                       (void*)page, count);
1500         if (length >= 0)
1501                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1502         free_page(page);
1503 out:
1504         put_task_struct(task);
1505 out_no_task:
1506         return length;
1507 }
1508
1509 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1510                                    size_t count, loff_t *ppos)
1511 {
1512         struct inode * inode = file->f_dentry->d_inode;
1513         char *page;
1514         ssize_t length;
1515         struct task_struct *task = get_proc_task(inode);
1516
1517         length = -ESRCH;
1518         if (!task)
1519                 goto out_no_task;
1520         if (count > PAGE_SIZE)
1521                 count = PAGE_SIZE;
1522
1523         /* No partial writes. */
1524         length = -EINVAL;
1525         if (*ppos != 0)
1526                 goto out;
1527
1528         length = -ENOMEM;
1529         page = (char*)__get_free_page(GFP_USER);
1530         if (!page)
1531                 goto out;
1532
1533         length = -EFAULT;
1534         if (copy_from_user(page, buf, count))
1535                 goto out_free;
1536
1537         length = security_setprocattr(task,
1538                                       (char*)file->f_dentry->d_name.name,
1539                                       (void*)page, count);
1540 out_free:
1541         free_page((unsigned long) page);
1542 out:
1543         put_task_struct(task);
1544 out_no_task:
1545         return length;
1546 }
1547
1548 static struct file_operations proc_pid_attr_operations = {
1549         .read           = proc_pid_attr_read,
1550         .write          = proc_pid_attr_write,
1551 };
1552
1553 static struct pid_entry attr_dir_stuff[] = {
1554         REG("current",    S_IRUGO|S_IWUGO, pid_attr),
1555         REG("prev",       S_IRUGO,         pid_attr),
1556         REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
1557         REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
1558         REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
1559         REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1560 };
1561
1562 static int proc_attr_dir_readdir(struct file * filp,
1563                              void * dirent, filldir_t filldir)
1564 {
1565         return proc_pident_readdir(filp,dirent,filldir,
1566                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1567 }
1568
1569 static struct file_operations proc_attr_dir_operations = {
1570         .read           = generic_read_dir,
1571         .readdir        = proc_attr_dir_readdir,
1572 };
1573
1574 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1575                                 struct dentry *dentry, struct nameidata *nd)
1576 {
1577         return proc_pident_lookup(dir, dentry,
1578                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1579 }
1580
1581 static struct inode_operations proc_attr_dir_inode_operations = {
1582         .lookup         = proc_attr_dir_lookup,
1583         .getattr        = pid_getattr,
1584         .setattr        = proc_setattr,
1585 };
1586
1587 #endif
1588
1589 /*
1590  * /proc/self:
1591  */
1592 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1593                               int buflen)
1594 {
1595         char tmp[PROC_NUMBUF];
1596         sprintf(tmp, "%d", current->tgid);
1597         return vfs_readlink(dentry,buffer,buflen,tmp);
1598 }
1599
1600 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1601 {
1602         char tmp[PROC_NUMBUF];
1603         sprintf(tmp, "%d", current->tgid);
1604         return ERR_PTR(vfs_follow_link(nd,tmp));
1605 }
1606
1607 static struct inode_operations proc_self_inode_operations = {
1608         .readlink       = proc_self_readlink,
1609         .follow_link    = proc_self_follow_link,
1610 };
1611
1612 /*
1613  * proc base
1614  *
1615  * These are the directory entries in the root directory of /proc
1616  * that properly belong to the /proc filesystem, as they describe
1617  * describe something that is process related.
1618  */
1619 static struct pid_entry proc_base_stuff[] = {
1620         NOD("self", S_IFLNK|S_IRWXUGO,
1621                 &proc_self_inode_operations, NULL, {}),
1622 };
1623
1624 /*
1625  *      Exceptional case: normally we are not allowed to unhash a busy
1626  * directory. In this case, however, we can do it - no aliasing problems
1627  * due to the way we treat inodes.
1628  */
1629 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1630 {
1631         struct inode *inode = dentry->d_inode;
1632         struct task_struct *task = get_proc_task(inode);
1633         if (task) {
1634                 put_task_struct(task);
1635                 return 1;
1636         }
1637         d_drop(dentry);
1638         return 0;
1639 }
1640
1641 static struct dentry_operations proc_base_dentry_operations =
1642 {
1643         .d_revalidate   = proc_base_revalidate,
1644         .d_delete       = pid_delete_dentry,
1645 };
1646
1647 static struct dentry *proc_base_instantiate(struct inode *dir,
1648         struct dentry *dentry, struct task_struct *task, void *ptr)
1649 {
1650         struct pid_entry *p = ptr;
1651         struct inode *inode;
1652         struct proc_inode *ei;
1653         struct dentry *error = ERR_PTR(-EINVAL);
1654
1655         /* Allocate the inode */
1656         error = ERR_PTR(-ENOMEM);
1657         inode = new_inode(dir->i_sb);
1658         if (!inode)
1659                 goto out;
1660
1661         /* Initialize the inode */
1662         ei = PROC_I(inode);
1663         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1664
1665         /*
1666          * grab the reference to the task.
1667          */
1668         ei->pid = get_pid(task_pid(task));
1669         if (!ei->pid)
1670                 goto out_iput;
1671
1672         inode->i_uid = 0;
1673         inode->i_gid = 0;
1674         inode->i_mode = p->mode;
1675         if (S_ISDIR(inode->i_mode))
1676                 inode->i_nlink = 2;
1677         if (S_ISLNK(inode->i_mode))
1678                 inode->i_size = 64;
1679         if (p->iop)
1680                 inode->i_op = p->iop;
1681         if (p->fop)
1682                 inode->i_fop = p->fop;
1683         ei->op = p->op;
1684         dentry->d_op = &proc_base_dentry_operations;
1685         d_add(dentry, inode);
1686         error = NULL;
1687 out:
1688         return error;
1689 out_iput:
1690         iput(inode);
1691         goto out;
1692 }
1693
1694 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1695 {
1696         struct dentry *error;
1697         struct task_struct *task = get_proc_task(dir);
1698         struct pid_entry *p, *last;
1699
1700         error = ERR_PTR(-ENOENT);
1701
1702         if (!task)
1703                 goto out_no_task;
1704
1705         /* Lookup the directory entry */
1706         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1707         for (p = proc_base_stuff; p <= last; p++) {
1708                 if (p->len != dentry->d_name.len)
1709                         continue;
1710                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1711                         break;
1712         }
1713         if (p > last)
1714                 goto out;
1715
1716         error = proc_base_instantiate(dir, dentry, task, p);
1717
1718 out:
1719         put_task_struct(task);
1720 out_no_task:
1721         return error;
1722 }
1723
1724 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1725         struct task_struct *task, struct pid_entry *p)
1726 {
1727         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1728                                 proc_base_instantiate, task, p);
1729 }
1730
1731 /*
1732  * Thread groups
1733  */
1734 static struct file_operations proc_task_operations;
1735 static struct inode_operations proc_task_inode_operations;
1736
1737 static struct pid_entry tgid_base_stuff[] = {
1738         DIR("task",       S_IRUGO|S_IXUGO, task),
1739         DIR("fd",         S_IRUSR|S_IXUSR, fd),
1740         INF("environ",    S_IRUSR, pid_environ),
1741         INF("auxv",       S_IRUSR, pid_auxv),
1742         INF("status",     S_IRUGO, pid_status),
1743         INF("cmdline",    S_IRUGO, pid_cmdline),
1744         INF("stat",       S_IRUGO, tgid_stat),
1745         INF("statm",      S_IRUGO, pid_statm),
1746         REG("maps",       S_IRUGO, maps),
1747 #ifdef CONFIG_NUMA
1748         REG("numa_maps",  S_IRUGO, numa_maps),
1749 #endif
1750         REG("mem",        S_IRUSR|S_IWUSR, mem),
1751 #ifdef CONFIG_SECCOMP
1752         REG("seccomp",    S_IRUSR|S_IWUSR, seccomp),
1753 #endif
1754         LNK("cwd",        cwd),
1755         LNK("root",       root),
1756         LNK("exe",        exe),
1757         REG("mounts",     S_IRUGO, mounts),
1758         REG("mountstats", S_IRUSR, mountstats),
1759 #ifdef CONFIG_MMU
1760         REG("smaps",      S_IRUGO, smaps),
1761 #endif
1762 #ifdef CONFIG_SECURITY
1763         DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
1764 #endif
1765 #ifdef CONFIG_KALLSYMS
1766         INF("wchan",      S_IRUGO, pid_wchan),
1767 #endif
1768 #ifdef CONFIG_SCHEDSTATS
1769         INF("schedstat",  S_IRUGO, pid_schedstat),
1770 #endif
1771 #ifdef CONFIG_CPUSETS
1772         REG("cpuset",     S_IRUGO, cpuset),
1773 #endif
1774         INF("oom_score",  S_IRUGO, oom_score),
1775         REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
1776 #ifdef CONFIG_AUDITSYSCALL
1777         REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
1778 #endif
1779 };
1780
1781 static int proc_tgid_base_readdir(struct file * filp,
1782                              void * dirent, filldir_t filldir)
1783 {
1784         return proc_pident_readdir(filp,dirent,filldir,
1785                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1786 }
1787
1788 static struct file_operations proc_tgid_base_operations = {
1789         .read           = generic_read_dir,
1790         .readdir        = proc_tgid_base_readdir,
1791 };
1792
1793 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1794         return proc_pident_lookup(dir, dentry,
1795                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1796 }
1797
1798 static struct inode_operations proc_tgid_base_inode_operations = {
1799         .lookup         = proc_tgid_base_lookup,
1800         .getattr        = pid_getattr,
1801         .setattr        = proc_setattr,
1802 };
1803
1804 /**
1805  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
1806  *
1807  * @task: task that should be flushed.
1808  *
1809  * Looks in the dcache for
1810  * /proc/@pid
1811  * /proc/@tgid/task/@pid
1812  * if either directory is present flushes it and all of it'ts children
1813  * from the dcache.
1814  *
1815  * It is safe and reasonable to cache /proc entries for a task until
1816  * that task exits.  After that they just clog up the dcache with
1817  * useless entries, possibly causing useful dcache entries to be
1818  * flushed instead.  This routine is proved to flush those useless
1819  * dcache entries at process exit time.
1820  *
1821  * NOTE: This routine is just an optimization so it does not guarantee
1822  *       that no dcache entries will exist at process exit time it
1823  *       just makes it very unlikely that any will persist.
1824  */
1825 void proc_flush_task(struct task_struct *task)
1826 {
1827         struct dentry *dentry, *leader, *dir;
1828         char buf[PROC_NUMBUF];
1829         struct qstr name;
1830
1831         name.name = buf;
1832         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1833         dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1834         if (dentry) {
1835                 shrink_dcache_parent(dentry);
1836                 d_drop(dentry);
1837                 dput(dentry);
1838         }
1839
1840         if (thread_group_leader(task))
1841                 goto out;
1842
1843         name.name = buf;
1844         name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1845         leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1846         if (!leader)
1847                 goto out;
1848
1849         name.name = "task";
1850         name.len = strlen(name.name);
1851         dir = d_hash_and_lookup(leader, &name);
1852         if (!dir)
1853                 goto out_put_leader;
1854
1855         name.name = buf;
1856         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1857         dentry = d_hash_and_lookup(dir, &name);
1858         if (dentry) {
1859                 shrink_dcache_parent(dentry);
1860                 d_drop(dentry);
1861                 dput(dentry);
1862         }
1863
1864         dput(dir);
1865 out_put_leader:
1866         dput(leader);
1867 out:
1868         return;
1869 }
1870
1871 struct dentry *proc_pid_instantiate(struct inode *dir,
1872         struct dentry * dentry, struct task_struct *task, void *ptr)
1873 {
1874         struct dentry *error = ERR_PTR(-ENOENT);
1875         struct inode *inode;
1876
1877         inode = proc_pid_make_inode(dir->i_sb, task);
1878         if (!inode)
1879                 goto out;
1880
1881         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1882         inode->i_op = &proc_tgid_base_inode_operations;
1883         inode->i_fop = &proc_tgid_base_operations;
1884         inode->i_flags|=S_IMMUTABLE;
1885         inode->i_nlink = 4;
1886 #ifdef CONFIG_SECURITY
1887         inode->i_nlink += 1;
1888 #endif
1889
1890         dentry->d_op = &pid_dentry_operations;
1891
1892         d_add(dentry, inode);
1893         /* Close the race of the process dying before we return the dentry */
1894         if (pid_revalidate(dentry, NULL))
1895                 error = NULL;
1896 out:
1897         return error;
1898 }
1899
1900 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1901 {
1902         struct dentry *result = ERR_PTR(-ENOENT);
1903         struct task_struct *task;
1904         unsigned tgid;
1905
1906         result = proc_base_lookup(dir, dentry);
1907         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1908                 goto out;
1909
1910         tgid = name_to_int(dentry);
1911         if (tgid == ~0U)
1912                 goto out;
1913
1914         rcu_read_lock();
1915         task = find_task_by_pid(tgid);
1916         if (task)
1917                 get_task_struct(task);
1918         rcu_read_unlock();
1919         if (!task)
1920                 goto out;
1921
1922         result = proc_pid_instantiate(dir, dentry, task, NULL);
1923         put_task_struct(task);
1924 out:
1925         return result;
1926 }
1927
1928 /*
1929  * Find the first task with tgid >= tgid
1930  *
1931  */
1932 static struct task_struct *next_tgid(unsigned int tgid)
1933 {
1934         struct task_struct *task;
1935         struct pid *pid;
1936
1937         rcu_read_lock();
1938 retry:
1939         task = NULL;
1940         pid = find_ge_pid(tgid);
1941         if (pid) {
1942                 tgid = pid->nr + 1;
1943                 task = pid_task(pid, PIDTYPE_PID);
1944                 /* What we to know is if the pid we have find is the
1945                  * pid of a thread_group_leader.  Testing for task
1946                  * being a thread_group_leader is the obvious thing
1947                  * todo but there is a window when it fails, due to
1948                  * the pid transfer logic in de_thread.
1949                  *
1950                  * So we perform the straight forward test of seeing
1951                  * if the pid we have found is the pid of a thread
1952                  * group leader, and don't worry if the task we have
1953                  * found doesn't happen to be a thread group leader.
1954                  * As we don't care in the case of readdir.
1955                  */
1956                 if (!task || !has_group_leader_pid(task))
1957                         goto retry;
1958                 get_task_struct(task);
1959         }
1960         rcu_read_unlock();
1961         return task;
1962 }
1963
1964 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
1965
1966 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1967         struct task_struct *task, int tgid)
1968 {
1969         char name[PROC_NUMBUF];
1970         int len = snprintf(name, sizeof(name), "%d", tgid);
1971         return proc_fill_cache(filp, dirent, filldir, name, len,
1972                                 proc_pid_instantiate, task, NULL);
1973 }
1974
1975 /* for the /proc/ directory itself, after non-process stuff has been done */
1976 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1977 {
1978         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1979         struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
1980         struct task_struct *task;
1981         int tgid;
1982
1983         if (!reaper)
1984                 goto out_no_task;
1985
1986         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
1987                 struct pid_entry *p = &proc_base_stuff[nr];
1988                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
1989                         goto out;
1990         }
1991
1992         tgid = filp->f_pos - TGID_OFFSET;
1993         for (task = next_tgid(tgid);
1994              task;
1995              put_task_struct(task), task = next_tgid(tgid + 1)) {
1996                 tgid = task->pid;
1997                 filp->f_pos = tgid + TGID_OFFSET;
1998                 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
1999                         put_task_struct(task);
2000                         goto out;
2001                 }
2002         }
2003         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2004 out:
2005         put_task_struct(reaper);
2006 out_no_task:
2007         return 0;
2008 }
2009
2010 /*
2011  * Tasks
2012  */
2013 static struct pid_entry tid_base_stuff[] = {
2014         DIR("fd",        S_IRUSR|S_IXUSR, fd),
2015         INF("environ",   S_IRUSR, pid_environ),
2016         INF("auxv",      S_IRUSR, pid_auxv),
2017         INF("status",    S_IRUGO, pid_status),
2018         INF("cmdline",   S_IRUGO, pid_cmdline),
2019         INF("stat",      S_IRUGO, tid_stat),
2020         INF("statm",     S_IRUGO, pid_statm),
2021         REG("maps",      S_IRUGO, maps),
2022 #ifdef CONFIG_NUMA
2023         REG("numa_maps", S_IRUGO, numa_maps),
2024 #endif
2025         REG("mem",       S_IRUSR|S_IWUSR, mem),
2026 #ifdef CONFIG_SECCOMP
2027         REG("seccomp",   S_IRUSR|S_IWUSR, seccomp),
2028 #endif
2029         LNK("cwd",       cwd),
2030         LNK("root",      root),
2031         LNK("exe",       exe),
2032         REG("mounts",    S_IRUGO, mounts),
2033 #ifdef CONFIG_MMU
2034         REG("smaps",     S_IRUGO, smaps),
2035 #endif
2036 #ifdef CONFIG_SECURITY
2037         DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
2038 #endif
2039 #ifdef CONFIG_KALLSYMS
2040         INF("wchan",     S_IRUGO, pid_wchan),
2041 #endif
2042 #ifdef CONFIG_SCHEDSTATS
2043         INF("schedstat", S_IRUGO, pid_schedstat),
2044 #endif
2045 #ifdef CONFIG_CPUSETS
2046         REG("cpuset",    S_IRUGO, cpuset),
2047 #endif
2048         INF("oom_score", S_IRUGO, oom_score),
2049         REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
2050 #ifdef CONFIG_AUDITSYSCALL
2051         REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
2052 #endif
2053 };
2054
2055 static int proc_tid_base_readdir(struct file * filp,
2056                              void * dirent, filldir_t filldir)
2057 {
2058         return proc_pident_readdir(filp,dirent,filldir,
2059                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2060 }
2061
2062 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2063         return proc_pident_lookup(dir, dentry,
2064                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2065 }
2066
2067 static struct file_operations proc_tid_base_operations = {
2068         .read           = generic_read_dir,
2069         .readdir        = proc_tid_base_readdir,
2070 };
2071
2072 static struct inode_operations proc_tid_base_inode_operations = {
2073         .lookup         = proc_tid_base_lookup,
2074         .getattr        = pid_getattr,
2075         .setattr        = proc_setattr,
2076 };
2077
2078 static struct dentry *proc_task_instantiate(struct inode *dir,
2079         struct dentry *dentry, struct task_struct *task, void *ptr)
2080 {
2081         struct dentry *error = ERR_PTR(-ENOENT);
2082         struct inode *inode;
2083         inode = proc_pid_make_inode(dir->i_sb, task);
2084
2085         if (!inode)
2086                 goto out;
2087         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2088         inode->i_op = &proc_tid_base_inode_operations;
2089         inode->i_fop = &proc_tid_base_operations;
2090         inode->i_flags|=S_IMMUTABLE;
2091         inode->i_nlink = 3;
2092 #ifdef CONFIG_SECURITY
2093         inode->i_nlink += 1;
2094 #endif
2095
2096         dentry->d_op = &pid_dentry_operations;
2097
2098         d_add(dentry, inode);
2099         /* Close the race of the process dying before we return the dentry */
2100         if (pid_revalidate(dentry, NULL))
2101                 error = NULL;
2102 out:
2103         return error;
2104 }
2105
2106 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2107 {
2108         struct dentry *result = ERR_PTR(-ENOENT);
2109         struct task_struct *task;
2110         struct task_struct *leader = get_proc_task(dir);
2111         unsigned tid;
2112
2113         if (!leader)
2114                 goto out_no_task;
2115
2116         tid = name_to_int(dentry);
2117         if (tid == ~0U)
2118                 goto out;
2119
2120         rcu_read_lock();
2121         task = find_task_by_pid(tid);
2122         if (task)
2123                 get_task_struct(task);
2124         rcu_read_unlock();
2125         if (!task)
2126                 goto out;
2127         if (leader->tgid != task->tgid)
2128                 goto out_drop_task;
2129
2130         result = proc_task_instantiate(dir, dentry, task, NULL);
2131 out_drop_task:
2132         put_task_struct(task);
2133 out:
2134         put_task_struct(leader);
2135 out_no_task:
2136         return result;
2137 }
2138
2139 /*
2140  * Find the first tid of a thread group to return to user space.
2141  *
2142  * Usually this is just the thread group leader, but if the users
2143  * buffer was too small or there was a seek into the middle of the
2144  * directory we have more work todo.
2145  *
2146  * In the case of a short read we start with find_task_by_pid.
2147  *
2148  * In the case of a seek we start with the leader and walk nr
2149  * threads past it.
2150  */
2151 static struct task_struct *first_tid(struct task_struct *leader,
2152                                         int tid, int nr)
2153 {
2154         struct task_struct *pos;
2155
2156         rcu_read_lock();
2157         /* Attempt to start with the pid of a thread */
2158         if (tid && (nr > 0)) {
2159                 pos = find_task_by_pid(tid);
2160                 if (pos && (pos->group_leader == leader))
2161                         goto found;
2162         }
2163
2164         /* If nr exceeds the number of threads there is nothing todo */
2165         pos = NULL;
2166         if (nr && nr >= get_nr_threads(leader))
2167                 goto out;
2168
2169         /* If we haven't found our starting place yet start
2170          * with the leader and walk nr threads forward.
2171          */
2172         for (pos = leader; nr > 0; --nr) {
2173                 pos = next_thread(pos);
2174                 if (pos == leader) {
2175                         pos = NULL;
2176                         goto out;
2177                 }
2178         }
2179 found:
2180         get_task_struct(pos);
2181 out:
2182         rcu_read_unlock();
2183         return pos;
2184 }
2185
2186 /*
2187  * Find the next thread in the thread list.
2188  * Return NULL if there is an error or no next thread.
2189  *
2190  * The reference to the input task_struct is released.
2191  */
2192 static struct task_struct *next_tid(struct task_struct *start)
2193 {
2194         struct task_struct *pos = NULL;
2195         rcu_read_lock();
2196         if (pid_alive(start)) {
2197                 pos = next_thread(start);
2198                 if (thread_group_leader(pos))
2199                         pos = NULL;
2200                 else
2201                         get_task_struct(pos);
2202         }
2203         rcu_read_unlock();
2204         put_task_struct(start);
2205         return pos;
2206 }
2207
2208 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2209         struct task_struct *task, int tid)
2210 {
2211         char name[PROC_NUMBUF];
2212         int len = snprintf(name, sizeof(name), "%d", tid);
2213         return proc_fill_cache(filp, dirent, filldir, name, len,
2214                                 proc_task_instantiate, task, NULL);
2215 }
2216
2217 /* for the /proc/TGID/task/ directories */
2218 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2219 {
2220         struct dentry *dentry = filp->f_dentry;
2221         struct inode *inode = dentry->d_inode;
2222         struct task_struct *leader = get_proc_task(inode);
2223         struct task_struct *task;
2224         int retval = -ENOENT;
2225         ino_t ino;
2226         int tid;
2227         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2228
2229         if (!leader)
2230                 goto out_no_task;
2231         retval = 0;
2232
2233         switch (pos) {
2234         case 0:
2235                 ino = inode->i_ino;
2236                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2237                         goto out;
2238                 pos++;
2239                 /* fall through */
2240         case 1:
2241                 ino = parent_ino(dentry);
2242                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2243                         goto out;
2244                 pos++;
2245                 /* fall through */
2246         }
2247
2248         /* f_version caches the tgid value that the last readdir call couldn't
2249          * return. lseek aka telldir automagically resets f_version to 0.
2250          */
2251         tid = filp->f_version;
2252         filp->f_version = 0;
2253         for (task = first_tid(leader, tid, pos - 2);
2254              task;
2255              task = next_tid(task), pos++) {
2256                 tid = task->pid;
2257                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2258                         /* returning this tgid failed, save it as the first
2259                          * pid for the next readir call */
2260                         filp->f_version = tid;
2261                         put_task_struct(task);
2262                         break;
2263                 }
2264         }
2265 out:
2266         filp->f_pos = pos;
2267         put_task_struct(leader);
2268 out_no_task:
2269         return retval;
2270 }
2271
2272 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2273 {
2274         struct inode *inode = dentry->d_inode;
2275         struct task_struct *p = get_proc_task(inode);
2276         generic_fillattr(inode, stat);
2277
2278         if (p) {
2279                 rcu_read_lock();
2280                 stat->nlink += get_nr_threads(p);
2281                 rcu_read_unlock();
2282                 put_task_struct(p);
2283         }
2284
2285         return 0;
2286 }
2287
2288 static struct inode_operations proc_task_inode_operations = {
2289         .lookup         = proc_task_lookup,
2290         .getattr        = proc_task_getattr,
2291         .setattr        = proc_setattr,
2292 };
2293
2294 static struct file_operations proc_task_operations = {
2295         .read           = generic_read_dir,
2296         .readdir        = proc_task_readdir,
2297 };