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