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