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