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