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