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