procfs: introduce the /proc/<pid>/map_files/ directory
[pandora-kernel.git] / fs / proc / base.c
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49
50 #include <asm/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
65 #include <linux/mm.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
89 #endif
90 #include "internal.h"
91
92 /* NOTE:
93  *      Implementing inode permission operations in /proc is almost
94  *      certainly an error.  Permission checks need to happen during
95  *      each system call not at open time.  The reason is that most of
96  *      what we wish to check for permissions in /proc varies at runtime.
97  *
98  *      The classic example of a problem is opening file descriptors
99  *      in /proc for a task before it execs a suid executable.
100  */
101
102 struct pid_entry {
103         char *name;
104         int len;
105         mode_t mode;
106         const struct inode_operations *iop;
107         const struct file_operations *fop;
108         union proc_op op;
109 };
110
111 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
112         .name = (NAME),                                 \
113         .len  = sizeof(NAME) - 1,                       \
114         .mode = MODE,                                   \
115         .iop  = IOP,                                    \
116         .fop  = FOP,                                    \
117         .op   = OP,                                     \
118 }
119
120 #define DIR(NAME, MODE, iops, fops)     \
121         NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
122 #define LNK(NAME, get_link)                                     \
123         NOD(NAME, (S_IFLNK|S_IRWXUGO),                          \
124                 &proc_pid_link_inode_operations, NULL,          \
125                 { .proc_get_link = get_link } )
126 #define REG(NAME, MODE, fops)                           \
127         NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
128 #define INF(NAME, MODE, read)                           \
129         NOD(NAME, (S_IFREG|(MODE)),                     \
130                 NULL, &proc_info_file_operations,       \
131                 { .proc_read = read } )
132 #define ONE(NAME, MODE, show)                           \
133         NOD(NAME, (S_IFREG|(MODE)),                     \
134                 NULL, &proc_single_file_operations,     \
135                 { .proc_show = show } )
136
137 static int proc_fd_permission(struct inode *inode, int mask);
138
139 /*
140  * Count the number of hardlinks for the pid_entry table, excluding the .
141  * and .. links.
142  */
143 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
144         unsigned int n)
145 {
146         unsigned int i;
147         unsigned int count;
148
149         count = 0;
150         for (i = 0; i < n; ++i) {
151                 if (S_ISDIR(entries[i].mode))
152                         ++count;
153         }
154
155         return count;
156 }
157
158 static int get_task_root(struct task_struct *task, struct path *root)
159 {
160         int result = -ENOENT;
161
162         task_lock(task);
163         if (task->fs) {
164                 get_fs_root(task->fs, root);
165                 result = 0;
166         }
167         task_unlock(task);
168         return result;
169 }
170
171 static int proc_cwd_link(struct dentry *dentry, struct path *path)
172 {
173         struct task_struct *task = get_proc_task(dentry->d_inode);
174         int result = -ENOENT;
175
176         if (task) {
177                 task_lock(task);
178                 if (task->fs) {
179                         get_fs_pwd(task->fs, path);
180                         result = 0;
181                 }
182                 task_unlock(task);
183                 put_task_struct(task);
184         }
185         return result;
186 }
187
188 static int proc_root_link(struct dentry *dentry, struct path *path)
189 {
190         struct task_struct *task = get_proc_task(dentry->d_inode);
191         int result = -ENOENT;
192
193         if (task) {
194                 result = get_task_root(task, path);
195                 put_task_struct(task);
196         }
197         return result;
198 }
199
200 static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
201 {
202         struct mm_struct *mm;
203         int err;
204
205         err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
206         if (err)
207                 return ERR_PTR(err);
208
209         mm = get_task_mm(task);
210         if (mm && mm != current->mm &&
211                         !ptrace_may_access(task, mode)) {
212                 mmput(mm);
213                 mm = ERR_PTR(-EACCES);
214         }
215         mutex_unlock(&task->signal->cred_guard_mutex);
216
217         return mm;
218 }
219
220 struct mm_struct *mm_for_maps(struct task_struct *task)
221 {
222         return mm_access(task, PTRACE_MODE_READ_FSCREDS);
223 }
224
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226 {
227         int res = 0;
228         unsigned int len;
229         struct mm_struct *mm = get_task_mm(task);
230         if (!mm)
231                 goto out;
232         if (!mm->arg_end)
233                 goto out_mm;    /* Shh! No looking before we're done */
234
235         len = mm->arg_end - mm->arg_start;
236  
237         if (len > PAGE_SIZE)
238                 len = PAGE_SIZE;
239  
240         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242         // If the nul at the end of args has been overwritten, then
243         // assume application is using setproctitle(3).
244         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245                 len = strnlen(buffer, res);
246                 if (len < res) {
247                     res = len;
248                 } else {
249                         len = mm->env_end - mm->env_start;
250                         if (len > PAGE_SIZE - res)
251                                 len = PAGE_SIZE - res;
252                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253                         res = strnlen(buffer, res);
254                 }
255         }
256 out_mm:
257         mmput(mm);
258 out:
259         return res;
260 }
261
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
263 {
264         struct mm_struct *mm = mm_for_maps(task);
265         int res = PTR_ERR(mm);
266         if (mm && !IS_ERR(mm)) {
267                 unsigned int nwords = 0;
268                 do {
269                         nwords += 2;
270                 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271                 res = nwords * sizeof(mm->saved_auxv[0]);
272                 if (res > PAGE_SIZE)
273                         res = PAGE_SIZE;
274                 memcpy(buffer, mm->saved_auxv, res);
275                 mmput(mm);
276         }
277         return res;
278 }
279
280
281 #ifdef CONFIG_KALLSYMS
282 /*
283  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284  * Returns the resolved symbol.  If that fails, simply return the address.
285  */
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
287 {
288         unsigned long wchan;
289         char symname[KSYM_NAME_LEN];
290
291         wchan = get_wchan(task);
292
293         if (lookup_symbol_name(wchan, symname) < 0)
294                 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
295                         return 0;
296                 else
297                         return sprintf(buffer, "%lu", wchan);
298         else
299                 return sprintf(buffer, "%s", symname);
300 }
301 #endif /* CONFIG_KALLSYMS */
302
303 static int lock_trace(struct task_struct *task)
304 {
305         int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
306         if (err)
307                 return err;
308         if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
309                 mutex_unlock(&task->signal->cred_guard_mutex);
310                 return -EPERM;
311         }
312         return 0;
313 }
314
315 static void unlock_trace(struct task_struct *task)
316 {
317         mutex_unlock(&task->signal->cred_guard_mutex);
318 }
319
320 #ifdef CONFIG_STACKTRACE
321
322 #define MAX_STACK_TRACE_DEPTH   64
323
324 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
325                           struct pid *pid, struct task_struct *task)
326 {
327         struct stack_trace trace;
328         unsigned long *entries;
329         int err;
330         int i;
331
332         entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
333         if (!entries)
334                 return -ENOMEM;
335
336         trace.nr_entries        = 0;
337         trace.max_entries       = MAX_STACK_TRACE_DEPTH;
338         trace.entries           = entries;
339         trace.skip              = 0;
340
341         err = lock_trace(task);
342         if (!err) {
343                 save_stack_trace_tsk(task, &trace);
344
345                 for (i = 0; i < trace.nr_entries; i++) {
346                         seq_printf(m, "[<%pK>] %pS\n",
347                                    (void *)entries[i], (void *)entries[i]);
348                 }
349                 unlock_trace(task);
350         }
351         kfree(entries);
352
353         return err;
354 }
355 #endif
356
357 #ifdef CONFIG_SCHEDSTATS
358 /*
359  * Provides /proc/PID/schedstat
360  */
361 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
362 {
363         return sprintf(buffer, "%llu %llu %lu\n",
364                         (unsigned long long)task->se.sum_exec_runtime,
365                         (unsigned long long)task->sched_info.run_delay,
366                         task->sched_info.pcount);
367 }
368 #endif
369
370 #ifdef CONFIG_LATENCYTOP
371 static int lstats_show_proc(struct seq_file *m, void *v)
372 {
373         int i;
374         struct inode *inode = m->private;
375         struct task_struct *task = get_proc_task(inode);
376
377         if (!task)
378                 return -ESRCH;
379         seq_puts(m, "Latency Top version : v0.1\n");
380         for (i = 0; i < 32; i++) {
381                 struct latency_record *lr = &task->latency_record[i];
382                 if (lr->backtrace[0]) {
383                         int q;
384                         seq_printf(m, "%i %li %li",
385                                    lr->count, lr->time, lr->max);
386                         for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
387                                 unsigned long bt = lr->backtrace[q];
388                                 if (!bt)
389                                         break;
390                                 if (bt == ULONG_MAX)
391                                         break;
392                                 seq_printf(m, " %ps", (void *)bt);
393                         }
394                         seq_putc(m, '\n');
395                 }
396
397         }
398         put_task_struct(task);
399         return 0;
400 }
401
402 static int lstats_open(struct inode *inode, struct file *file)
403 {
404         return single_open(file, lstats_show_proc, inode);
405 }
406
407 static ssize_t lstats_write(struct file *file, const char __user *buf,
408                             size_t count, loff_t *offs)
409 {
410         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
411
412         if (!task)
413                 return -ESRCH;
414         clear_all_latency_tracing(task);
415         put_task_struct(task);
416
417         return count;
418 }
419
420 static const struct file_operations proc_lstats_operations = {
421         .open           = lstats_open,
422         .read           = seq_read,
423         .write          = lstats_write,
424         .llseek         = seq_lseek,
425         .release        = single_release,
426 };
427
428 #endif
429
430 static int proc_oom_score(struct task_struct *task, char *buffer)
431 {
432         unsigned long points = 0;
433
434         read_lock(&tasklist_lock);
435         if (pid_alive(task))
436                 points = oom_badness(task, NULL, NULL,
437                                         totalram_pages + total_swap_pages);
438         read_unlock(&tasklist_lock);
439         return sprintf(buffer, "%lu\n", points);
440 }
441
442 struct limit_names {
443         char *name;
444         char *unit;
445 };
446
447 static const struct limit_names lnames[RLIM_NLIMITS] = {
448         [RLIMIT_CPU] = {"Max cpu time", "seconds"},
449         [RLIMIT_FSIZE] = {"Max file size", "bytes"},
450         [RLIMIT_DATA] = {"Max data size", "bytes"},
451         [RLIMIT_STACK] = {"Max stack size", "bytes"},
452         [RLIMIT_CORE] = {"Max core file size", "bytes"},
453         [RLIMIT_RSS] = {"Max resident set", "bytes"},
454         [RLIMIT_NPROC] = {"Max processes", "processes"},
455         [RLIMIT_NOFILE] = {"Max open files", "files"},
456         [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
457         [RLIMIT_AS] = {"Max address space", "bytes"},
458         [RLIMIT_LOCKS] = {"Max file locks", "locks"},
459         [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
460         [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
461         [RLIMIT_NICE] = {"Max nice priority", NULL},
462         [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
463         [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
464 };
465
466 /* Display limits for a process */
467 static int proc_pid_limits(struct task_struct *task, char *buffer)
468 {
469         unsigned int i;
470         int count = 0;
471         unsigned long flags;
472         char *bufptr = buffer;
473
474         struct rlimit rlim[RLIM_NLIMITS];
475
476         if (!lock_task_sighand(task, &flags))
477                 return 0;
478         memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
479         unlock_task_sighand(task, &flags);
480
481         /*
482          * print the file header
483          */
484         count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
485                         "Limit", "Soft Limit", "Hard Limit", "Units");
486
487         for (i = 0; i < RLIM_NLIMITS; i++) {
488                 if (rlim[i].rlim_cur == RLIM_INFINITY)
489                         count += sprintf(&bufptr[count], "%-25s %-20s ",
490                                          lnames[i].name, "unlimited");
491                 else
492                         count += sprintf(&bufptr[count], "%-25s %-20lu ",
493                                          lnames[i].name, rlim[i].rlim_cur);
494
495                 if (rlim[i].rlim_max == RLIM_INFINITY)
496                         count += sprintf(&bufptr[count], "%-20s ", "unlimited");
497                 else
498                         count += sprintf(&bufptr[count], "%-20lu ",
499                                          rlim[i].rlim_max);
500
501                 if (lnames[i].unit)
502                         count += sprintf(&bufptr[count], "%-10s\n",
503                                          lnames[i].unit);
504                 else
505                         count += sprintf(&bufptr[count], "\n");
506         }
507
508         return count;
509 }
510
511 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
512 static int proc_pid_syscall(struct task_struct *task, char *buffer)
513 {
514         long nr;
515         unsigned long args[6], sp, pc;
516         int res = lock_trace(task);
517         if (res)
518                 return res;
519
520         if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
521                 res = sprintf(buffer, "running\n");
522         else if (nr < 0)
523                 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
524         else
525                 res = sprintf(buffer,
526                        "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
527                        nr,
528                        args[0], args[1], args[2], args[3], args[4], args[5],
529                        sp, pc);
530         unlock_trace(task);
531         return res;
532 }
533 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
534
535 /************************************************************************/
536 /*                       Here the fs part begins                        */
537 /************************************************************************/
538
539 /* permission checks */
540 static int proc_fd_access_allowed(struct inode *inode)
541 {
542         struct task_struct *task;
543         int allowed = 0;
544         /* Allow access to a task's file descriptors if it is us or we
545          * may use ptrace attach to the process and find out that
546          * information.
547          */
548         task = get_proc_task(inode);
549         if (task) {
550                 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
551                 put_task_struct(task);
552         }
553         return allowed;
554 }
555
556 int proc_setattr(struct dentry *dentry, struct iattr *attr)
557 {
558         int error;
559         struct inode *inode = dentry->d_inode;
560
561         if (attr->ia_valid & ATTR_MODE)
562                 return -EPERM;
563
564         error = setattr_prepare(dentry, attr);
565         if (error)
566                 return error;
567
568         if ((attr->ia_valid & ATTR_SIZE) &&
569             attr->ia_size != i_size_read(inode)) {
570                 error = vmtruncate(inode, attr->ia_size);
571                 if (error)
572                         return error;
573         }
574
575         setattr_copy(inode, attr);
576         mark_inode_dirty(inode);
577         return 0;
578 }
579
580 static const struct inode_operations proc_def_inode_operations = {
581         .setattr        = proc_setattr,
582 };
583
584 static int mounts_open_common(struct inode *inode, struct file *file,
585                               const struct seq_operations *op)
586 {
587         struct task_struct *task = get_proc_task(inode);
588         struct nsproxy *nsp;
589         struct mnt_namespace *ns = NULL;
590         struct path root;
591         struct proc_mounts *p;
592         int ret = -EINVAL;
593
594         if (task) {
595                 rcu_read_lock();
596                 nsp = task_nsproxy(task);
597                 if (nsp) {
598                         ns = nsp->mnt_ns;
599                         if (ns)
600                                 get_mnt_ns(ns);
601                 }
602                 rcu_read_unlock();
603                 if (ns && get_task_root(task, &root) == 0)
604                         ret = 0;
605                 put_task_struct(task);
606         }
607
608         if (!ns)
609                 goto err;
610         if (ret)
611                 goto err_put_ns;
612
613         ret = -ENOMEM;
614         p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
615         if (!p)
616                 goto err_put_path;
617
618         file->private_data = &p->m;
619         ret = seq_open(file, op);
620         if (ret)
621                 goto err_free;
622
623         p->m.private = p;
624         p->ns = ns;
625         p->root = root;
626         p->m.poll_event = ns->event;
627
628         return 0;
629
630  err_free:
631         kfree(p);
632  err_put_path:
633         path_put(&root);
634  err_put_ns:
635         put_mnt_ns(ns);
636  err:
637         return ret;
638 }
639
640 static int mounts_release(struct inode *inode, struct file *file)
641 {
642         struct proc_mounts *p = file->private_data;
643         path_put(&p->root);
644         put_mnt_ns(p->ns);
645         return seq_release(inode, file);
646 }
647
648 static unsigned mounts_poll(struct file *file, poll_table *wait)
649 {
650         struct proc_mounts *p = file->private_data;
651         unsigned res = POLLIN | POLLRDNORM;
652
653         poll_wait(file, &p->ns->poll, wait);
654         if (mnt_had_events(p))
655                 res |= POLLERR | POLLPRI;
656
657         return res;
658 }
659
660 static int mounts_open(struct inode *inode, struct file *file)
661 {
662         return mounts_open_common(inode, file, &mounts_op);
663 }
664
665 static const struct file_operations proc_mounts_operations = {
666         .open           = mounts_open,
667         .read           = seq_read,
668         .llseek         = seq_lseek,
669         .release        = mounts_release,
670         .poll           = mounts_poll,
671 };
672
673 static int mountinfo_open(struct inode *inode, struct file *file)
674 {
675         return mounts_open_common(inode, file, &mountinfo_op);
676 }
677
678 static const struct file_operations proc_mountinfo_operations = {
679         .open           = mountinfo_open,
680         .read           = seq_read,
681         .llseek         = seq_lseek,
682         .release        = mounts_release,
683         .poll           = mounts_poll,
684 };
685
686 static int mountstats_open(struct inode *inode, struct file *file)
687 {
688         return mounts_open_common(inode, file, &mountstats_op);
689 }
690
691 static const struct file_operations proc_mountstats_operations = {
692         .open           = mountstats_open,
693         .read           = seq_read,
694         .llseek         = seq_lseek,
695         .release        = mounts_release,
696 };
697
698 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
699
700 static ssize_t proc_info_read(struct file * file, char __user * buf,
701                           size_t count, loff_t *ppos)
702 {
703         struct inode * inode = file->f_path.dentry->d_inode;
704         unsigned long page;
705         ssize_t length;
706         struct task_struct *task = get_proc_task(inode);
707
708         length = -ESRCH;
709         if (!task)
710                 goto out_no_task;
711
712         if (count > PROC_BLOCK_SIZE)
713                 count = PROC_BLOCK_SIZE;
714
715         length = -ENOMEM;
716         if (!(page = __get_free_page(GFP_TEMPORARY)))
717                 goto out;
718
719         length = PROC_I(inode)->op.proc_read(task, (char*)page);
720
721         if (length >= 0)
722                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
723         free_page(page);
724 out:
725         put_task_struct(task);
726 out_no_task:
727         return length;
728 }
729
730 static const struct file_operations proc_info_file_operations = {
731         .read           = proc_info_read,
732         .llseek         = generic_file_llseek,
733 };
734
735 static int proc_single_show(struct seq_file *m, void *v)
736 {
737         struct inode *inode = m->private;
738         struct pid_namespace *ns;
739         struct pid *pid;
740         struct task_struct *task;
741         int ret;
742
743         ns = inode->i_sb->s_fs_info;
744         pid = proc_pid(inode);
745         task = get_pid_task(pid, PIDTYPE_PID);
746         if (!task)
747                 return -ESRCH;
748
749         ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
750
751         put_task_struct(task);
752         return ret;
753 }
754
755 static int proc_single_open(struct inode *inode, struct file *filp)
756 {
757         return single_open(filp, proc_single_show, inode);
758 }
759
760 static const struct file_operations proc_single_file_operations = {
761         .open           = proc_single_open,
762         .read           = seq_read,
763         .llseek         = seq_lseek,
764         .release        = single_release,
765 };
766
767 static int mem_open(struct inode* inode, struct file* file)
768 {
769         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
770         struct mm_struct *mm;
771
772         if (!task)
773                 return -ESRCH;
774
775         mm = mm_access(task, PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS);
776         put_task_struct(task);
777
778         if (IS_ERR(mm))
779                 return PTR_ERR(mm);
780
781         if (mm) {
782                 /* ensure this mm_struct can't be freed */
783                 atomic_inc(&mm->mm_count);
784                 /* but do not pin its memory */
785                 mmput(mm);
786         }
787
788         /* OK to pass negative loff_t, we can catch out-of-range */
789         file->f_mode |= FMODE_UNSIGNED_OFFSET;
790         file->private_data = mm;
791
792         return 0;
793 }
794
795 static ssize_t mem_rw(struct file *file, char __user *buf,
796                         size_t count, loff_t *ppos, int write)
797 {
798         struct mm_struct *mm = file->private_data;
799         unsigned long addr = *ppos;
800         ssize_t copied;
801         char *page;
802
803         if (!mm)
804                 return 0;
805
806         page = (char *)__get_free_page(GFP_TEMPORARY);
807         if (!page)
808                 return -ENOMEM;
809
810         copied = 0;
811         if (!atomic_inc_not_zero(&mm->mm_users))
812                 goto free;
813
814         while (count > 0) {
815                 int this_len = min_t(int, count, PAGE_SIZE);
816
817                 if (write && copy_from_user(page, buf, this_len)) {
818                         copied = -EFAULT;
819                         break;
820                 }
821
822                 this_len = access_remote_vm(mm, addr, page, this_len, write);
823                 if (!this_len) {
824                         if (!copied)
825                                 copied = -EIO;
826                         break;
827                 }
828
829                 if (!write && copy_to_user(buf, page, this_len)) {
830                         copied = -EFAULT;
831                         break;
832                 }
833
834                 buf += this_len;
835                 addr += this_len;
836                 copied += this_len;
837                 count -= this_len;
838         }
839         *ppos = addr;
840
841         mmput(mm);
842 free:
843         free_page((unsigned long) page);
844         return copied;
845 }
846
847 static ssize_t mem_read(struct file *file, char __user *buf,
848                         size_t count, loff_t *ppos)
849 {
850         return mem_rw(file, buf, count, ppos, 0);
851 }
852
853 static ssize_t mem_write(struct file *file, const char __user *buf,
854                          size_t count, loff_t *ppos)
855 {
856         return mem_rw(file, (char __user*)buf, count, ppos, 1);
857 }
858
859 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
860 {
861         switch (orig) {
862         case 0:
863                 file->f_pos = offset;
864                 break;
865         case 1:
866                 file->f_pos += offset;
867                 break;
868         default:
869                 return -EINVAL;
870         }
871         force_successful_syscall_return();
872         return file->f_pos;
873 }
874
875 static int mem_release(struct inode *inode, struct file *file)
876 {
877         struct mm_struct *mm = file->private_data;
878         if (mm)
879                 mmdrop(mm);
880         return 0;
881 }
882
883 static const struct file_operations proc_mem_operations = {
884         .llseek         = mem_lseek,
885         .read           = mem_read,
886         .write          = mem_write,
887         .open           = mem_open,
888         .release        = mem_release,
889 };
890
891 static ssize_t environ_read(struct file *file, char __user *buf,
892                         size_t count, loff_t *ppos)
893 {
894         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
895         char *page;
896         unsigned long src = *ppos;
897         int ret = -ESRCH;
898         struct mm_struct *mm;
899
900         if (!task)
901                 goto out_no_task;
902
903         ret = -ENOMEM;
904         page = (char *)__get_free_page(GFP_TEMPORARY);
905         if (!page)
906                 goto out;
907
908
909         mm = mm_for_maps(task);
910         ret = PTR_ERR(mm);
911         /* Ensure the process spawned far enough to have an environment. */
912         if (!mm || IS_ERR(mm) || !mm->env_end)
913                 goto out_free;
914
915         ret = 0;
916         while (count > 0) {
917                 int this_len, retval, max_len;
918
919                 this_len = mm->env_end - (mm->env_start + src);
920
921                 if (this_len <= 0)
922                         break;
923
924                 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
925                 this_len = (this_len > max_len) ? max_len : this_len;
926
927                 retval = access_process_vm(task, (mm->env_start + src),
928                         page, this_len, 0);
929
930                 if (retval <= 0) {
931                         ret = retval;
932                         break;
933                 }
934
935                 if (copy_to_user(buf, page, retval)) {
936                         ret = -EFAULT;
937                         break;
938                 }
939
940                 ret += retval;
941                 src += retval;
942                 buf += retval;
943                 count -= retval;
944         }
945         *ppos = src;
946
947         mmput(mm);
948 out_free:
949         free_page((unsigned long) page);
950 out:
951         put_task_struct(task);
952 out_no_task:
953         return ret;
954 }
955
956 static const struct file_operations proc_environ_operations = {
957         .read           = environ_read,
958         .llseek         = generic_file_llseek,
959 };
960
961 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
962                                 size_t count, loff_t *ppos)
963 {
964         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
965         char buffer[PROC_NUMBUF];
966         size_t len;
967         int oom_adjust = OOM_DISABLE;
968         unsigned long flags;
969
970         if (!task)
971                 return -ESRCH;
972
973         if (lock_task_sighand(task, &flags)) {
974                 oom_adjust = task->signal->oom_adj;
975                 unlock_task_sighand(task, &flags);
976         }
977
978         put_task_struct(task);
979
980         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
981
982         return simple_read_from_buffer(buf, count, ppos, buffer, len);
983 }
984
985 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
986                                 size_t count, loff_t *ppos)
987 {
988         struct task_struct *task;
989         char buffer[PROC_NUMBUF];
990         int oom_adjust;
991         unsigned long flags;
992         int err;
993
994         memset(buffer, 0, sizeof(buffer));
995         if (count > sizeof(buffer) - 1)
996                 count = sizeof(buffer) - 1;
997         if (copy_from_user(buffer, buf, count)) {
998                 err = -EFAULT;
999                 goto out;
1000         }
1001
1002         err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1003         if (err)
1004                 goto out;
1005         if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1006              oom_adjust != OOM_DISABLE) {
1007                 err = -EINVAL;
1008                 goto out;
1009         }
1010
1011         task = get_proc_task(file->f_path.dentry->d_inode);
1012         if (!task) {
1013                 err = -ESRCH;
1014                 goto out;
1015         }
1016
1017         task_lock(task);
1018         if (!task->mm) {
1019                 err = -EINVAL;
1020                 goto err_task_lock;
1021         }
1022
1023         if (!lock_task_sighand(task, &flags)) {
1024                 err = -ESRCH;
1025                 goto err_task_lock;
1026         }
1027
1028         if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1029                 err = -EACCES;
1030                 goto err_sighand;
1031         }
1032
1033         /*
1034          * Warn that /proc/pid/oom_adj is deprecated, see
1035          * Documentation/feature-removal-schedule.txt.
1036          */
1037         printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1038                   current->comm, task_pid_nr(current), task_pid_nr(task),
1039                   task_pid_nr(task));
1040         task->signal->oom_adj = oom_adjust;
1041         /*
1042          * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1043          * value is always attainable.
1044          */
1045         if (task->signal->oom_adj == OOM_ADJUST_MAX)
1046                 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1047         else
1048                 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1049                                                                 -OOM_DISABLE;
1050 err_sighand:
1051         unlock_task_sighand(task, &flags);
1052 err_task_lock:
1053         task_unlock(task);
1054         put_task_struct(task);
1055 out:
1056         return err < 0 ? err : count;
1057 }
1058
1059 static const struct file_operations proc_oom_adjust_operations = {
1060         .read           = oom_adjust_read,
1061         .write          = oom_adjust_write,
1062         .llseek         = generic_file_llseek,
1063 };
1064
1065 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1066                                         size_t count, loff_t *ppos)
1067 {
1068         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1069         char buffer[PROC_NUMBUF];
1070         int oom_score_adj = OOM_SCORE_ADJ_MIN;
1071         unsigned long flags;
1072         size_t len;
1073
1074         if (!task)
1075                 return -ESRCH;
1076         if (lock_task_sighand(task, &flags)) {
1077                 oom_score_adj = task->signal->oom_score_adj;
1078                 unlock_task_sighand(task, &flags);
1079         }
1080         put_task_struct(task);
1081         len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1082         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1083 }
1084
1085 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1086                                         size_t count, loff_t *ppos)
1087 {
1088         struct task_struct *task;
1089         char buffer[PROC_NUMBUF];
1090         unsigned long flags;
1091         int oom_score_adj;
1092         int err;
1093
1094         memset(buffer, 0, sizeof(buffer));
1095         if (count > sizeof(buffer) - 1)
1096                 count = sizeof(buffer) - 1;
1097         if (copy_from_user(buffer, buf, count)) {
1098                 err = -EFAULT;
1099                 goto out;
1100         }
1101
1102         err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1103         if (err)
1104                 goto out;
1105         if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1106                         oom_score_adj > OOM_SCORE_ADJ_MAX) {
1107                 err = -EINVAL;
1108                 goto out;
1109         }
1110
1111         task = get_proc_task(file->f_path.dentry->d_inode);
1112         if (!task) {
1113                 err = -ESRCH;
1114                 goto out;
1115         }
1116
1117         task_lock(task);
1118         if (!task->mm) {
1119                 err = -EINVAL;
1120                 goto err_task_lock;
1121         }
1122
1123         if (!lock_task_sighand(task, &flags)) {
1124                 err = -ESRCH;
1125                 goto err_task_lock;
1126         }
1127
1128         if (oom_score_adj < task->signal->oom_score_adj_min &&
1129                         !capable(CAP_SYS_RESOURCE)) {
1130                 err = -EACCES;
1131                 goto err_sighand;
1132         }
1133
1134         task->signal->oom_score_adj = oom_score_adj;
1135         if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1136                 task->signal->oom_score_adj_min = oom_score_adj;
1137         /*
1138          * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1139          * always attainable.
1140          */
1141         if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1142                 task->signal->oom_adj = OOM_DISABLE;
1143         else
1144                 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1145                                                         OOM_SCORE_ADJ_MAX;
1146 err_sighand:
1147         unlock_task_sighand(task, &flags);
1148 err_task_lock:
1149         task_unlock(task);
1150         put_task_struct(task);
1151 out:
1152         return err < 0 ? err : count;
1153 }
1154
1155 static const struct file_operations proc_oom_score_adj_operations = {
1156         .read           = oom_score_adj_read,
1157         .write          = oom_score_adj_write,
1158         .llseek         = default_llseek,
1159 };
1160
1161 #ifdef CONFIG_AUDITSYSCALL
1162 #define TMPBUFLEN 21
1163 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1164                                   size_t count, loff_t *ppos)
1165 {
1166         struct inode * inode = file->f_path.dentry->d_inode;
1167         struct task_struct *task = get_proc_task(inode);
1168         ssize_t length;
1169         char tmpbuf[TMPBUFLEN];
1170
1171         if (!task)
1172                 return -ESRCH;
1173         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1174                                 audit_get_loginuid(task));
1175         put_task_struct(task);
1176         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1177 }
1178
1179 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1180                                    size_t count, loff_t *ppos)
1181 {
1182         struct inode * inode = file->f_path.dentry->d_inode;
1183         char *page, *tmp;
1184         ssize_t length;
1185         uid_t loginuid;
1186
1187         if (!capable(CAP_AUDIT_CONTROL))
1188                 return -EPERM;
1189
1190         rcu_read_lock();
1191         if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1192                 rcu_read_unlock();
1193                 return -EPERM;
1194         }
1195         rcu_read_unlock();
1196
1197         if (count >= PAGE_SIZE)
1198                 count = PAGE_SIZE - 1;
1199
1200         if (*ppos != 0) {
1201                 /* No partial writes. */
1202                 return -EINVAL;
1203         }
1204         page = (char*)__get_free_page(GFP_TEMPORARY);
1205         if (!page)
1206                 return -ENOMEM;
1207         length = -EFAULT;
1208         if (copy_from_user(page, buf, count))
1209                 goto out_free_page;
1210
1211         page[count] = '\0';
1212         loginuid = simple_strtoul(page, &tmp, 10);
1213         if (tmp == page) {
1214                 length = -EINVAL;
1215                 goto out_free_page;
1216
1217         }
1218         length = audit_set_loginuid(current, loginuid);
1219         if (likely(length == 0))
1220                 length = count;
1221
1222 out_free_page:
1223         free_page((unsigned long) page);
1224         return length;
1225 }
1226
1227 static const struct file_operations proc_loginuid_operations = {
1228         .read           = proc_loginuid_read,
1229         .write          = proc_loginuid_write,
1230         .llseek         = generic_file_llseek,
1231 };
1232
1233 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1234                                   size_t count, loff_t *ppos)
1235 {
1236         struct inode * inode = file->f_path.dentry->d_inode;
1237         struct task_struct *task = get_proc_task(inode);
1238         ssize_t length;
1239         char tmpbuf[TMPBUFLEN];
1240
1241         if (!task)
1242                 return -ESRCH;
1243         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1244                                 audit_get_sessionid(task));
1245         put_task_struct(task);
1246         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1247 }
1248
1249 static const struct file_operations proc_sessionid_operations = {
1250         .read           = proc_sessionid_read,
1251         .llseek         = generic_file_llseek,
1252 };
1253 #endif
1254
1255 #ifdef CONFIG_FAULT_INJECTION
1256 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1257                                       size_t count, loff_t *ppos)
1258 {
1259         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1260         char buffer[PROC_NUMBUF];
1261         size_t len;
1262         int make_it_fail;
1263
1264         if (!task)
1265                 return -ESRCH;
1266         make_it_fail = task->make_it_fail;
1267         put_task_struct(task);
1268
1269         len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1270
1271         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1272 }
1273
1274 static ssize_t proc_fault_inject_write(struct file * file,
1275                         const char __user * buf, size_t count, loff_t *ppos)
1276 {
1277         struct task_struct *task;
1278         char buffer[PROC_NUMBUF], *end;
1279         int make_it_fail;
1280
1281         if (!capable(CAP_SYS_RESOURCE))
1282                 return -EPERM;
1283         memset(buffer, 0, sizeof(buffer));
1284         if (count > sizeof(buffer) - 1)
1285                 count = sizeof(buffer) - 1;
1286         if (copy_from_user(buffer, buf, count))
1287                 return -EFAULT;
1288         make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1289         if (*end)
1290                 return -EINVAL;
1291         task = get_proc_task(file->f_dentry->d_inode);
1292         if (!task)
1293                 return -ESRCH;
1294         task->make_it_fail = make_it_fail;
1295         put_task_struct(task);
1296
1297         return count;
1298 }
1299
1300 static const struct file_operations proc_fault_inject_operations = {
1301         .read           = proc_fault_inject_read,
1302         .write          = proc_fault_inject_write,
1303         .llseek         = generic_file_llseek,
1304 };
1305 #endif
1306
1307
1308 #ifdef CONFIG_SCHED_DEBUG
1309 /*
1310  * Print out various scheduling related per-task fields:
1311  */
1312 static int sched_show(struct seq_file *m, void *v)
1313 {
1314         struct inode *inode = m->private;
1315         struct task_struct *p;
1316
1317         p = get_proc_task(inode);
1318         if (!p)
1319                 return -ESRCH;
1320         proc_sched_show_task(p, m);
1321
1322         put_task_struct(p);
1323
1324         return 0;
1325 }
1326
1327 static ssize_t
1328 sched_write(struct file *file, const char __user *buf,
1329             size_t count, loff_t *offset)
1330 {
1331         struct inode *inode = file->f_path.dentry->d_inode;
1332         struct task_struct *p;
1333
1334         p = get_proc_task(inode);
1335         if (!p)
1336                 return -ESRCH;
1337         proc_sched_set_task(p);
1338
1339         put_task_struct(p);
1340
1341         return count;
1342 }
1343
1344 static int sched_open(struct inode *inode, struct file *filp)
1345 {
1346         return single_open(filp, sched_show, inode);
1347 }
1348
1349 static const struct file_operations proc_pid_sched_operations = {
1350         .open           = sched_open,
1351         .read           = seq_read,
1352         .write          = sched_write,
1353         .llseek         = seq_lseek,
1354         .release        = single_release,
1355 };
1356
1357 #endif
1358
1359 #ifdef CONFIG_SCHED_AUTOGROUP
1360 /*
1361  * Print out autogroup related information:
1362  */
1363 static int sched_autogroup_show(struct seq_file *m, void *v)
1364 {
1365         struct inode *inode = m->private;
1366         struct task_struct *p;
1367
1368         p = get_proc_task(inode);
1369         if (!p)
1370                 return -ESRCH;
1371         proc_sched_autogroup_show_task(p, m);
1372
1373         put_task_struct(p);
1374
1375         return 0;
1376 }
1377
1378 static ssize_t
1379 sched_autogroup_write(struct file *file, const char __user *buf,
1380             size_t count, loff_t *offset)
1381 {
1382         struct inode *inode = file->f_path.dentry->d_inode;
1383         struct task_struct *p;
1384         char buffer[PROC_NUMBUF];
1385         int nice;
1386         int err;
1387
1388         memset(buffer, 0, sizeof(buffer));
1389         if (count > sizeof(buffer) - 1)
1390                 count = sizeof(buffer) - 1;
1391         if (copy_from_user(buffer, buf, count))
1392                 return -EFAULT;
1393
1394         err = kstrtoint(strstrip(buffer), 0, &nice);
1395         if (err < 0)
1396                 return err;
1397
1398         p = get_proc_task(inode);
1399         if (!p)
1400                 return -ESRCH;
1401
1402         err = nice;
1403         err = proc_sched_autogroup_set_nice(p, &err);
1404         if (err)
1405                 count = err;
1406
1407         put_task_struct(p);
1408
1409         return count;
1410 }
1411
1412 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1413 {
1414         int ret;
1415
1416         ret = single_open(filp, sched_autogroup_show, NULL);
1417         if (!ret) {
1418                 struct seq_file *m = filp->private_data;
1419
1420                 m->private = inode;
1421         }
1422         return ret;
1423 }
1424
1425 static const struct file_operations proc_pid_sched_autogroup_operations = {
1426         .open           = sched_autogroup_open,
1427         .read           = seq_read,
1428         .write          = sched_autogroup_write,
1429         .llseek         = seq_lseek,
1430         .release        = single_release,
1431 };
1432
1433 #endif /* CONFIG_SCHED_AUTOGROUP */
1434
1435 static ssize_t comm_write(struct file *file, const char __user *buf,
1436                                 size_t count, loff_t *offset)
1437 {
1438         struct inode *inode = file->f_path.dentry->d_inode;
1439         struct task_struct *p;
1440         char buffer[TASK_COMM_LEN];
1441
1442         memset(buffer, 0, sizeof(buffer));
1443         if (count > sizeof(buffer) - 1)
1444                 count = sizeof(buffer) - 1;
1445         if (copy_from_user(buffer, buf, count))
1446                 return -EFAULT;
1447
1448         p = get_proc_task(inode);
1449         if (!p)
1450                 return -ESRCH;
1451
1452         if (same_thread_group(current, p))
1453                 set_task_comm(p, buffer);
1454         else
1455                 count = -EINVAL;
1456
1457         put_task_struct(p);
1458
1459         return count;
1460 }
1461
1462 static int comm_show(struct seq_file *m, void *v)
1463 {
1464         struct inode *inode = m->private;
1465         struct task_struct *p;
1466
1467         p = get_proc_task(inode);
1468         if (!p)
1469                 return -ESRCH;
1470
1471         task_lock(p);
1472         seq_printf(m, "%s\n", p->comm);
1473         task_unlock(p);
1474
1475         put_task_struct(p);
1476
1477         return 0;
1478 }
1479
1480 static int comm_open(struct inode *inode, struct file *filp)
1481 {
1482         return single_open(filp, comm_show, inode);
1483 }
1484
1485 static const struct file_operations proc_pid_set_comm_operations = {
1486         .open           = comm_open,
1487         .read           = seq_read,
1488         .write          = comm_write,
1489         .llseek         = seq_lseek,
1490         .release        = single_release,
1491 };
1492
1493 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1494 {
1495         struct task_struct *task;
1496         struct mm_struct *mm;
1497         struct file *exe_file;
1498
1499         task = get_proc_task(dentry->d_inode);
1500         if (!task)
1501                 return -ENOENT;
1502         mm = get_task_mm(task);
1503         put_task_struct(task);
1504         if (!mm)
1505                 return -ENOENT;
1506         exe_file = get_mm_exe_file(mm);
1507         mmput(mm);
1508         if (exe_file) {
1509                 *exe_path = exe_file->f_path;
1510                 path_get(&exe_file->f_path);
1511                 fput(exe_file);
1512                 return 0;
1513         } else
1514                 return -ENOENT;
1515 }
1516
1517 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1518 {
1519         struct inode *inode = dentry->d_inode;
1520         int error = -EACCES;
1521
1522         /* We don't need a base pointer in the /proc filesystem */
1523         path_put(&nd->path);
1524
1525         /* Are we allowed to snoop on the tasks file descriptors? */
1526         if (!proc_fd_access_allowed(inode))
1527                 goto out;
1528
1529         error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1530 out:
1531         return ERR_PTR(error);
1532 }
1533
1534 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1535 {
1536         char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1537         char *pathname;
1538         int len;
1539
1540         if (!tmp)
1541                 return -ENOMEM;
1542
1543         pathname = d_path(path, tmp, PAGE_SIZE);
1544         len = PTR_ERR(pathname);
1545         if (IS_ERR(pathname))
1546                 goto out;
1547         len = tmp + PAGE_SIZE - 1 - pathname;
1548
1549         if (len > buflen)
1550                 len = buflen;
1551         if (copy_to_user(buffer, pathname, len))
1552                 len = -EFAULT;
1553  out:
1554         free_page((unsigned long)tmp);
1555         return len;
1556 }
1557
1558 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1559 {
1560         int error = -EACCES;
1561         struct inode *inode = dentry->d_inode;
1562         struct path path;
1563
1564         /* Are we allowed to snoop on the tasks file descriptors? */
1565         if (!proc_fd_access_allowed(inode))
1566                 goto out;
1567
1568         error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1569         if (error)
1570                 goto out;
1571
1572         error = do_proc_readlink(&path, buffer, buflen);
1573         path_put(&path);
1574 out:
1575         return error;
1576 }
1577
1578 static const struct inode_operations proc_pid_link_inode_operations = {
1579         .readlink       = proc_pid_readlink,
1580         .follow_link    = proc_pid_follow_link,
1581         .setattr        = proc_setattr,
1582 };
1583
1584
1585 /* building an inode */
1586
1587 static int task_dumpable(struct task_struct *task)
1588 {
1589         int dumpable = 0;
1590         struct mm_struct *mm;
1591
1592         task_lock(task);
1593         mm = task->mm;
1594         if (mm)
1595                 dumpable = get_dumpable(mm);
1596         task_unlock(task);
1597         if(dumpable == 1)
1598                 return 1;
1599         return 0;
1600 }
1601
1602 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1603 {
1604         struct inode * inode;
1605         struct proc_inode *ei;
1606         const struct cred *cred;
1607
1608         /* We need a new inode */
1609
1610         inode = new_inode(sb);
1611         if (!inode)
1612                 goto out;
1613
1614         /* Common stuff */
1615         ei = PROC_I(inode);
1616         inode->i_ino = get_next_ino();
1617         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1618         inode->i_op = &proc_def_inode_operations;
1619
1620         /*
1621          * grab the reference to task.
1622          */
1623         ei->pid = get_task_pid(task, PIDTYPE_PID);
1624         if (!ei->pid)
1625                 goto out_unlock;
1626
1627         if (task_dumpable(task)) {
1628                 rcu_read_lock();
1629                 cred = __task_cred(task);
1630                 inode->i_uid = cred->euid;
1631                 inode->i_gid = cred->egid;
1632                 rcu_read_unlock();
1633         }
1634         security_task_to_inode(task, inode);
1635
1636 out:
1637         return inode;
1638
1639 out_unlock:
1640         iput(inode);
1641         return NULL;
1642 }
1643
1644 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1645 {
1646         struct inode *inode = dentry->d_inode;
1647         struct task_struct *task;
1648         const struct cred *cred;
1649
1650         generic_fillattr(inode, stat);
1651
1652         rcu_read_lock();
1653         stat->uid = 0;
1654         stat->gid = 0;
1655         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1656         if (task) {
1657                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1658                     task_dumpable(task)) {
1659                         cred = __task_cred(task);
1660                         stat->uid = cred->euid;
1661                         stat->gid = cred->egid;
1662                 }
1663         }
1664         rcu_read_unlock();
1665         return 0;
1666 }
1667
1668 /* dentry stuff */
1669
1670 /*
1671  *      Exceptional case: normally we are not allowed to unhash a busy
1672  * directory. In this case, however, we can do it - no aliasing problems
1673  * due to the way we treat inodes.
1674  *
1675  * Rewrite the inode's ownerships here because the owning task may have
1676  * performed a setuid(), etc.
1677  *
1678  * Before the /proc/pid/status file was created the only way to read
1679  * the effective uid of a /process was to stat /proc/pid.  Reading
1680  * /proc/pid/status is slow enough that procps and other packages
1681  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1682  * made this apply to all per process world readable and executable
1683  * directories.
1684  */
1685 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1686 {
1687         struct inode *inode;
1688         struct task_struct *task;
1689         const struct cred *cred;
1690
1691         if (nd && nd->flags & LOOKUP_RCU)
1692                 return -ECHILD;
1693
1694         inode = dentry->d_inode;
1695         task = get_proc_task(inode);
1696
1697         if (task) {
1698                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1699                     task_dumpable(task)) {
1700                         rcu_read_lock();
1701                         cred = __task_cred(task);
1702                         inode->i_uid = cred->euid;
1703                         inode->i_gid = cred->egid;
1704                         rcu_read_unlock();
1705                 } else {
1706                         inode->i_uid = 0;
1707                         inode->i_gid = 0;
1708                 }
1709                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1710                 security_task_to_inode(task, inode);
1711                 put_task_struct(task);
1712                 return 1;
1713         }
1714         d_drop(dentry);
1715         return 0;
1716 }
1717
1718 static int pid_delete_dentry(const struct dentry * dentry)
1719 {
1720         /* Is the task we represent dead?
1721          * If so, then don't put the dentry on the lru list,
1722          * kill it immediately.
1723          */
1724         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1725 }
1726
1727 const struct dentry_operations pid_dentry_operations =
1728 {
1729         .d_revalidate   = pid_revalidate,
1730         .d_delete       = pid_delete_dentry,
1731 };
1732
1733 /* Lookups */
1734
1735 /*
1736  * Fill a directory entry.
1737  *
1738  * If possible create the dcache entry and derive our inode number and
1739  * file type from dcache entry.
1740  *
1741  * Since all of the proc inode numbers are dynamically generated, the inode
1742  * numbers do not exist until the inode is cache.  This means creating the
1743  * the dcache entry in readdir is necessary to keep the inode numbers
1744  * reported by readdir in sync with the inode numbers reported
1745  * by stat.
1746  */
1747 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1748         const char *name, int len,
1749         instantiate_t instantiate, struct task_struct *task, const void *ptr)
1750 {
1751         struct dentry *child, *dir = filp->f_path.dentry;
1752         struct inode *inode;
1753         struct qstr qname;
1754         ino_t ino = 0;
1755         unsigned type = DT_UNKNOWN;
1756
1757         qname.name = name;
1758         qname.len  = len;
1759         qname.hash = full_name_hash(name, len);
1760
1761         child = d_lookup(dir, &qname);
1762         if (!child) {
1763                 struct dentry *new;
1764                 new = d_alloc(dir, &qname);
1765                 if (new) {
1766                         child = instantiate(dir->d_inode, new, task, ptr);
1767                         if (child)
1768                                 dput(new);
1769                         else
1770                                 child = new;
1771                 }
1772         }
1773         if (!child || IS_ERR(child) || !child->d_inode)
1774                 goto end_instantiate;
1775         inode = child->d_inode;
1776         if (inode) {
1777                 ino = inode->i_ino;
1778                 type = inode->i_mode >> 12;
1779         }
1780         dput(child);
1781 end_instantiate:
1782         if (!ino)
1783                 ino = find_inode_number(dir, &qname);
1784         if (!ino)
1785                 ino = 1;
1786         return filldir(dirent, name, len, filp->f_pos, ino, type);
1787 }
1788
1789 static unsigned name_to_int(struct dentry *dentry)
1790 {
1791         const char *name = dentry->d_name.name;
1792         int len = dentry->d_name.len;
1793         unsigned n = 0;
1794
1795         if (len > 1 && *name == '0')
1796                 goto out;
1797         while (len-- > 0) {
1798                 unsigned c = *name++ - '0';
1799                 if (c > 9)
1800                         goto out;
1801                 if (n >= (~0U-9)/10)
1802                         goto out;
1803                 n *= 10;
1804                 n += c;
1805         }
1806         return n;
1807 out:
1808         return ~0U;
1809 }
1810
1811 #define PROC_FDINFO_MAX 64
1812
1813 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1814 {
1815         struct task_struct *task = get_proc_task(inode);
1816         struct files_struct *files = NULL;
1817         struct file *file;
1818         int fd = proc_fd(inode);
1819
1820         if (task) {
1821                 files = get_files_struct(task);
1822                 put_task_struct(task);
1823         }
1824         if (files) {
1825                 /*
1826                  * We are not taking a ref to the file structure, so we must
1827                  * hold ->file_lock.
1828                  */
1829                 spin_lock(&files->file_lock);
1830                 file = fcheck_files(files, fd);
1831                 if (file) {
1832                         unsigned int f_flags;
1833                         struct fdtable *fdt;
1834
1835                         fdt = files_fdtable(files);
1836                         f_flags = file->f_flags & ~O_CLOEXEC;
1837                         if (close_on_exec(fd, fdt))
1838                                 f_flags |= O_CLOEXEC;
1839
1840                         if (path) {
1841                                 *path = file->f_path;
1842                                 path_get(&file->f_path);
1843                         }
1844                         if (info)
1845                                 snprintf(info, PROC_FDINFO_MAX,
1846                                          "pos:\t%lli\n"
1847                                          "flags:\t0%o\n",
1848                                          (long long) file->f_pos,
1849                                          f_flags);
1850                         spin_unlock(&files->file_lock);
1851                         put_files_struct(files);
1852                         return 0;
1853                 }
1854                 spin_unlock(&files->file_lock);
1855                 put_files_struct(files);
1856         }
1857         return -ENOENT;
1858 }
1859
1860 static int proc_fd_link(struct dentry *dentry, struct path *path)
1861 {
1862         return proc_fd_info(dentry->d_inode, path, NULL);
1863 }
1864
1865 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1866 {
1867         struct inode *inode;
1868         struct task_struct *task;
1869         int fd;
1870         struct files_struct *files;
1871         const struct cred *cred;
1872
1873         if (nd && nd->flags & LOOKUP_RCU)
1874                 return -ECHILD;
1875
1876         inode = dentry->d_inode;
1877         task = get_proc_task(inode);
1878         fd = proc_fd(inode);
1879
1880         if (task) {
1881                 files = get_files_struct(task);
1882                 if (files) {
1883                         rcu_read_lock();
1884                         if (fcheck_files(files, fd)) {
1885                                 rcu_read_unlock();
1886                                 put_files_struct(files);
1887                                 if (task_dumpable(task)) {
1888                                         rcu_read_lock();
1889                                         cred = __task_cred(task);
1890                                         inode->i_uid = cred->euid;
1891                                         inode->i_gid = cred->egid;
1892                                         rcu_read_unlock();
1893                                 } else {
1894                                         inode->i_uid = 0;
1895                                         inode->i_gid = 0;
1896                                 }
1897                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1898                                 security_task_to_inode(task, inode);
1899                                 put_task_struct(task);
1900                                 return 1;
1901                         }
1902                         rcu_read_unlock();
1903                         put_files_struct(files);
1904                 }
1905                 put_task_struct(task);
1906         }
1907         d_drop(dentry);
1908         return 0;
1909 }
1910
1911 static const struct dentry_operations tid_fd_dentry_operations =
1912 {
1913         .d_revalidate   = tid_fd_revalidate,
1914         .d_delete       = pid_delete_dentry,
1915 };
1916
1917 static struct dentry *proc_fd_instantiate(struct inode *dir,
1918         struct dentry *dentry, struct task_struct *task, const void *ptr)
1919 {
1920         unsigned fd = *(const unsigned *)ptr;
1921         struct file *file;
1922         struct files_struct *files;
1923         struct inode *inode;
1924         struct proc_inode *ei;
1925         struct dentry *error = ERR_PTR(-ENOENT);
1926
1927         inode = proc_pid_make_inode(dir->i_sb, task);
1928         if (!inode)
1929                 goto out;
1930         ei = PROC_I(inode);
1931         ei->fd = fd;
1932         files = get_files_struct(task);
1933         if (!files)
1934                 goto out_iput;
1935         inode->i_mode = S_IFLNK;
1936
1937         /*
1938          * We are not taking a ref to the file structure, so we must
1939          * hold ->file_lock.
1940          */
1941         spin_lock(&files->file_lock);
1942         file = fcheck_files(files, fd);
1943         if (!file)
1944                 goto out_unlock;
1945         if (file->f_mode & FMODE_READ)
1946                 inode->i_mode |= S_IRUSR | S_IXUSR;
1947         if (file->f_mode & FMODE_WRITE)
1948                 inode->i_mode |= S_IWUSR | S_IXUSR;
1949         spin_unlock(&files->file_lock);
1950         put_files_struct(files);
1951
1952         inode->i_op = &proc_pid_link_inode_operations;
1953         inode->i_size = 64;
1954         ei->op.proc_get_link = proc_fd_link;
1955         d_set_d_op(dentry, &tid_fd_dentry_operations);
1956         d_add(dentry, inode);
1957         /* Close the race of the process dying before we return the dentry */
1958         if (tid_fd_revalidate(dentry, NULL))
1959                 error = NULL;
1960
1961  out:
1962         return error;
1963 out_unlock:
1964         spin_unlock(&files->file_lock);
1965         put_files_struct(files);
1966 out_iput:
1967         iput(inode);
1968         goto out;
1969 }
1970
1971 static struct dentry *proc_lookupfd_common(struct inode *dir,
1972                                            struct dentry *dentry,
1973                                            instantiate_t instantiate)
1974 {
1975         struct task_struct *task = get_proc_task(dir);
1976         unsigned fd = name_to_int(dentry);
1977         struct dentry *result = ERR_PTR(-ENOENT);
1978
1979         if (!task)
1980                 goto out_no_task;
1981         if (fd == ~0U)
1982                 goto out;
1983
1984         result = instantiate(dir, dentry, task, &fd);
1985 out:
1986         put_task_struct(task);
1987 out_no_task:
1988         return result;
1989 }
1990
1991 static int proc_readfd_common(struct file * filp, void * dirent,
1992                               filldir_t filldir, instantiate_t instantiate)
1993 {
1994         struct dentry *dentry = filp->f_path.dentry;
1995         struct inode *inode = dentry->d_inode;
1996         struct task_struct *p = get_proc_task(inode);
1997         unsigned int fd, ino;
1998         int retval;
1999         struct files_struct * files;
2000
2001         retval = -ENOENT;
2002         if (!p)
2003                 goto out_no_task;
2004         retval = 0;
2005
2006         fd = filp->f_pos;
2007         switch (fd) {
2008                 case 0:
2009                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2010                                 goto out;
2011                         filp->f_pos++;
2012                 case 1:
2013                         ino = parent_ino(dentry);
2014                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2015                                 goto out;
2016                         filp->f_pos++;
2017                 default:
2018                         files = get_files_struct(p);
2019                         if (!files)
2020                                 goto out;
2021                         rcu_read_lock();
2022                         for (fd = filp->f_pos-2;
2023                              fd < files_fdtable(files)->max_fds;
2024                              fd++, filp->f_pos++) {
2025                                 char name[PROC_NUMBUF];
2026                                 int len;
2027
2028                                 if (!fcheck_files(files, fd))
2029                                         continue;
2030                                 rcu_read_unlock();
2031
2032                                 len = snprintf(name, sizeof(name), "%d", fd);
2033                                 if (proc_fill_cache(filp, dirent, filldir,
2034                                                     name, len, instantiate,
2035                                                     p, &fd) < 0) {
2036                                         rcu_read_lock();
2037                                         break;
2038                                 }
2039                                 rcu_read_lock();
2040                         }
2041                         rcu_read_unlock();
2042                         put_files_struct(files);
2043         }
2044 out:
2045         put_task_struct(p);
2046 out_no_task:
2047         return retval;
2048 }
2049
2050 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2051                                     struct nameidata *nd)
2052 {
2053         return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2054 }
2055
2056 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2057 {
2058         return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2059 }
2060
2061 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2062                                       size_t len, loff_t *ppos)
2063 {
2064         char tmp[PROC_FDINFO_MAX];
2065         int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2066         if (!err)
2067                 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2068         return err;
2069 }
2070
2071 static const struct file_operations proc_fdinfo_file_operations = {
2072         .open           = nonseekable_open,
2073         .read           = proc_fdinfo_read,
2074         .llseek         = no_llseek,
2075 };
2076
2077 static const struct file_operations proc_fd_operations = {
2078         .read           = generic_read_dir,
2079         .readdir        = proc_readfd,
2080         .llseek         = default_llseek,
2081 };
2082
2083 #ifdef CONFIG_CHECKPOINT_RESTORE
2084
2085 /*
2086  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2087  * which represent vma start and end addresses.
2088  */
2089 static int dname_to_vma_addr(struct dentry *dentry,
2090                              unsigned long *start, unsigned long *end)
2091 {
2092         if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2093                 return -EINVAL;
2094
2095         return 0;
2096 }
2097
2098 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2099 {
2100         unsigned long vm_start, vm_end;
2101         bool exact_vma_exists = false;
2102         struct mm_struct *mm = NULL;
2103         struct task_struct *task;
2104         const struct cred *cred;
2105         struct inode *inode;
2106         int status = 0;
2107
2108         if (nd && nd->flags & LOOKUP_RCU)
2109                 return -ECHILD;
2110
2111         if (!capable(CAP_SYS_ADMIN)) {
2112                 status = -EACCES;
2113                 goto out_notask;
2114         }
2115
2116         inode = dentry->d_inode;
2117         task = get_proc_task(inode);
2118         if (!task)
2119                 goto out_notask;
2120
2121         if (!ptrace_may_access(task, PTRACE_MODE_READ))
2122                 goto out;
2123
2124         mm = get_task_mm(task);
2125         if (!mm)
2126                 goto out;
2127
2128         if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2129                 down_read(&mm->mmap_sem);
2130                 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2131                 up_read(&mm->mmap_sem);
2132         }
2133
2134         mmput(mm);
2135
2136         if (exact_vma_exists) {
2137                 if (task_dumpable(task)) {
2138                         rcu_read_lock();
2139                         cred = __task_cred(task);
2140                         inode->i_uid = cred->euid;
2141                         inode->i_gid = cred->egid;
2142                         rcu_read_unlock();
2143                 } else {
2144                         inode->i_uid = 0;
2145                         inode->i_gid = 0;
2146                 }
2147                 security_task_to_inode(task, inode);
2148                 status = 1;
2149         }
2150
2151 out:
2152         put_task_struct(task);
2153
2154 out_notask:
2155         if (status <= 0)
2156                 d_drop(dentry);
2157
2158         return status;
2159 }
2160
2161 static const struct dentry_operations tid_map_files_dentry_operations = {
2162         .d_revalidate   = map_files_d_revalidate,
2163         .d_delete       = pid_delete_dentry,
2164 };
2165
2166 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2167 {
2168         unsigned long vm_start, vm_end;
2169         struct vm_area_struct *vma;
2170         struct task_struct *task;
2171         struct mm_struct *mm;
2172         int rc;
2173
2174         rc = -ENOENT;
2175         task = get_proc_task(dentry->d_inode);
2176         if (!task)
2177                 goto out;
2178
2179         mm = get_task_mm(task);
2180         put_task_struct(task);
2181         if (!mm)
2182                 goto out;
2183
2184         rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2185         if (rc)
2186                 goto out_mmput;
2187
2188         down_read(&mm->mmap_sem);
2189         vma = find_exact_vma(mm, vm_start, vm_end);
2190         if (vma && vma->vm_file) {
2191                 *path = vma->vm_file->f_path;
2192                 path_get(path);
2193                 rc = 0;
2194         }
2195         up_read(&mm->mmap_sem);
2196
2197 out_mmput:
2198         mmput(mm);
2199 out:
2200         return rc;
2201 }
2202
2203 struct map_files_info {
2204         struct file     *file;
2205         unsigned long   len;
2206         unsigned char   name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2207 };
2208
2209 static struct dentry *
2210 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2211                            struct task_struct *task, const void *ptr)
2212 {
2213         const struct file *file = ptr;
2214         struct proc_inode *ei;
2215         struct inode *inode;
2216
2217         if (!file)
2218                 return ERR_PTR(-ENOENT);
2219
2220         inode = proc_pid_make_inode(dir->i_sb, task);
2221         if (!inode)
2222                 return ERR_PTR(-ENOENT);
2223
2224         ei = PROC_I(inode);
2225         ei->op.proc_get_link = proc_map_files_get_link;
2226
2227         inode->i_op = &proc_pid_link_inode_operations;
2228         inode->i_size = 64;
2229         inode->i_mode = S_IFLNK;
2230
2231         if (file->f_mode & FMODE_READ)
2232                 inode->i_mode |= S_IRUSR;
2233         if (file->f_mode & FMODE_WRITE)
2234                 inode->i_mode |= S_IWUSR;
2235
2236         d_set_d_op(dentry, &tid_map_files_dentry_operations);
2237         d_add(dentry, inode);
2238
2239         return NULL;
2240 }
2241
2242 static struct dentry *proc_map_files_lookup(struct inode *dir,
2243                 struct dentry *dentry, struct nameidata *nd)
2244 {
2245         unsigned long vm_start, vm_end;
2246         struct vm_area_struct *vma;
2247         struct task_struct *task;
2248         struct dentry *result;
2249         struct mm_struct *mm;
2250
2251         result = ERR_PTR(-EACCES);
2252         if (!capable(CAP_SYS_ADMIN))
2253                 goto out;
2254
2255         result = ERR_PTR(-ENOENT);
2256         task = get_proc_task(dir);
2257         if (!task)
2258                 goto out;
2259
2260         result = ERR_PTR(-EACCES);
2261         if (lock_trace(task))
2262                 goto out_put_task;
2263
2264         result = ERR_PTR(-ENOENT);
2265         if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2266                 goto out_unlock;
2267
2268         mm = get_task_mm(task);
2269         if (!mm)
2270                 goto out_unlock;
2271
2272         down_read(&mm->mmap_sem);
2273         vma = find_exact_vma(mm, vm_start, vm_end);
2274         if (!vma)
2275                 goto out_no_vma;
2276
2277         result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2278
2279 out_no_vma:
2280         up_read(&mm->mmap_sem);
2281         mmput(mm);
2282 out_unlock:
2283         unlock_trace(task);
2284 out_put_task:
2285         put_task_struct(task);
2286 out:
2287         return result;
2288 }
2289
2290 static const struct inode_operations proc_map_files_inode_operations = {
2291         .lookup         = proc_map_files_lookup,
2292         .permission     = proc_fd_permission,
2293         .setattr        = proc_setattr,
2294 };
2295
2296 static int
2297 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2298 {
2299         struct dentry *dentry = filp->f_path.dentry;
2300         struct inode *inode = dentry->d_inode;
2301         struct vm_area_struct *vma;
2302         struct task_struct *task;
2303         struct mm_struct *mm;
2304         ino_t ino;
2305         int ret;
2306
2307         ret = -EACCES;
2308         if (!capable(CAP_SYS_ADMIN))
2309                 goto out;
2310
2311         ret = -ENOENT;
2312         task = get_proc_task(inode);
2313         if (!task)
2314                 goto out;
2315
2316         ret = -EACCES;
2317         if (lock_trace(task))
2318                 goto out_put_task;
2319
2320         ret = 0;
2321         switch (filp->f_pos) {
2322         case 0:
2323                 ino = inode->i_ino;
2324                 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2325                         goto out_unlock;
2326                 filp->f_pos++;
2327         case 1:
2328                 ino = parent_ino(dentry);
2329                 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2330                         goto out_unlock;
2331                 filp->f_pos++;
2332         default:
2333         {
2334                 unsigned long nr_files, pos, i;
2335                 struct flex_array *fa = NULL;
2336                 struct map_files_info info;
2337                 struct map_files_info *p;
2338
2339                 mm = get_task_mm(task);
2340                 if (!mm)
2341                         goto out_unlock;
2342                 down_read(&mm->mmap_sem);
2343
2344                 nr_files = 0;
2345
2346                 /*
2347                  * We need two passes here:
2348                  *
2349                  *  1) Collect vmas of mapped files with mmap_sem taken
2350                  *  2) Release mmap_sem and instantiate entries
2351                  *
2352                  * otherwise we get lockdep complained, since filldir()
2353                  * routine might require mmap_sem taken in might_fault().
2354                  */
2355
2356                 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2357                         if (vma->vm_file && ++pos > filp->f_pos)
2358                                 nr_files++;
2359                 }
2360
2361                 if (nr_files) {
2362                         fa = flex_array_alloc(sizeof(info), nr_files,
2363                                                 GFP_KERNEL);
2364                         if (!fa || flex_array_prealloc(fa, 0, nr_files,
2365                                                         GFP_KERNEL)) {
2366                                 ret = -ENOMEM;
2367                                 if (fa)
2368                                         flex_array_free(fa);
2369                                 up_read(&mm->mmap_sem);
2370                                 mmput(mm);
2371                                 goto out_unlock;
2372                         }
2373                         for (i = 0, vma = mm->mmap, pos = 2; vma;
2374                                         vma = vma->vm_next) {
2375                                 if (!vma->vm_file)
2376                                         continue;
2377                                 if (++pos <= filp->f_pos)
2378                                         continue;
2379
2380                                 get_file(vma->vm_file);
2381                                 info.file = vma->vm_file;
2382                                 info.len = snprintf(info.name,
2383                                                 sizeof(info.name), "%lx-%lx",
2384                                                 vma->vm_start, vma->vm_end);
2385                                 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2386                                         BUG();
2387                         }
2388                 }
2389                 up_read(&mm->mmap_sem);
2390
2391                 for (i = 0; i < nr_files; i++) {
2392                         p = flex_array_get(fa, i);
2393                         ret = proc_fill_cache(filp, dirent, filldir,
2394                                               p->name, p->len,
2395                                               proc_map_files_instantiate,
2396                                               task, p->file);
2397                         if (ret)
2398                                 break;
2399                         filp->f_pos++;
2400                         fput(p->file);
2401                 }
2402                 for (; i < nr_files; i++) {
2403                         /*
2404                          * In case of error don't forget
2405                          * to put rest of file refs.
2406                          */
2407                         p = flex_array_get(fa, i);
2408                         fput(p->file);
2409                 }
2410                 if (fa)
2411                         flex_array_free(fa);
2412                 mmput(mm);
2413         }
2414         }
2415
2416 out_unlock:
2417         unlock_trace(task);
2418 out_put_task:
2419         put_task_struct(task);
2420 out:
2421         return ret;
2422 }
2423
2424 static const struct file_operations proc_map_files_operations = {
2425         .read           = generic_read_dir,
2426         .readdir        = proc_map_files_readdir,
2427         .llseek         = default_llseek,
2428 };
2429
2430 #endif /* CONFIG_CHECKPOINT_RESTORE */
2431
2432 /*
2433  * /proc/pid/fd needs a special permission handler so that a process can still
2434  * access /proc/self/fd after it has executed a setuid().
2435  */
2436 static int proc_fd_permission(struct inode *inode, int mask)
2437 {
2438         int rv = generic_permission(inode, mask);
2439         if (rv == 0)
2440                 return 0;
2441         if (task_pid(current) == proc_pid(inode))
2442                 rv = 0;
2443         return rv;
2444 }
2445
2446 /*
2447  * proc directories can do almost nothing..
2448  */
2449 static const struct inode_operations proc_fd_inode_operations = {
2450         .lookup         = proc_lookupfd,
2451         .permission     = proc_fd_permission,
2452         .setattr        = proc_setattr,
2453 };
2454
2455 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2456         struct dentry *dentry, struct task_struct *task, const void *ptr)
2457 {
2458         unsigned fd = *(unsigned *)ptr;
2459         struct inode *inode;
2460         struct proc_inode *ei;
2461         struct dentry *error = ERR_PTR(-ENOENT);
2462
2463         inode = proc_pid_make_inode(dir->i_sb, task);
2464         if (!inode)
2465                 goto out;
2466         ei = PROC_I(inode);
2467         ei->fd = fd;
2468         inode->i_mode = S_IFREG | S_IRUSR;
2469         inode->i_fop = &proc_fdinfo_file_operations;
2470         d_set_d_op(dentry, &tid_fd_dentry_operations);
2471         d_add(dentry, inode);
2472         /* Close the race of the process dying before we return the dentry */
2473         if (tid_fd_revalidate(dentry, NULL))
2474                 error = NULL;
2475
2476  out:
2477         return error;
2478 }
2479
2480 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2481                                         struct dentry *dentry,
2482                                         struct nameidata *nd)
2483 {
2484         return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2485 }
2486
2487 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2488 {
2489         return proc_readfd_common(filp, dirent, filldir,
2490                                   proc_fdinfo_instantiate);
2491 }
2492
2493 static const struct file_operations proc_fdinfo_operations = {
2494         .read           = generic_read_dir,
2495         .readdir        = proc_readfdinfo,
2496         .llseek         = default_llseek,
2497 };
2498
2499 /*
2500  * proc directories can do almost nothing..
2501  */
2502 static const struct inode_operations proc_fdinfo_inode_operations = {
2503         .lookup         = proc_lookupfdinfo,
2504         .setattr        = proc_setattr,
2505 };
2506
2507
2508 static struct dentry *proc_pident_instantiate(struct inode *dir,
2509         struct dentry *dentry, struct task_struct *task, const void *ptr)
2510 {
2511         const struct pid_entry *p = ptr;
2512         struct inode *inode;
2513         struct proc_inode *ei;
2514         struct dentry *error = ERR_PTR(-ENOENT);
2515
2516         inode = proc_pid_make_inode(dir->i_sb, task);
2517         if (!inode)
2518                 goto out;
2519
2520         ei = PROC_I(inode);
2521         inode->i_mode = p->mode;
2522         if (S_ISDIR(inode->i_mode))
2523                 set_nlink(inode, 2);    /* Use getattr to fix if necessary */
2524         if (p->iop)
2525                 inode->i_op = p->iop;
2526         if (p->fop)
2527                 inode->i_fop = p->fop;
2528         ei->op = p->op;
2529         d_set_d_op(dentry, &pid_dentry_operations);
2530         d_add(dentry, inode);
2531         /* Close the race of the process dying before we return the dentry */
2532         if (pid_revalidate(dentry, NULL))
2533                 error = NULL;
2534 out:
2535         return error;
2536 }
2537
2538 static struct dentry *proc_pident_lookup(struct inode *dir, 
2539                                          struct dentry *dentry,
2540                                          const struct pid_entry *ents,
2541                                          unsigned int nents)
2542 {
2543         struct dentry *error;
2544         struct task_struct *task = get_proc_task(dir);
2545         const struct pid_entry *p, *last;
2546
2547         error = ERR_PTR(-ENOENT);
2548
2549         if (!task)
2550                 goto out_no_task;
2551
2552         /*
2553          * Yes, it does not scale. And it should not. Don't add
2554          * new entries into /proc/<tgid>/ without very good reasons.
2555          */
2556         last = &ents[nents - 1];
2557         for (p = ents; p <= last; p++) {
2558                 if (p->len != dentry->d_name.len)
2559                         continue;
2560                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2561                         break;
2562         }
2563         if (p > last)
2564                 goto out;
2565
2566         error = proc_pident_instantiate(dir, dentry, task, p);
2567 out:
2568         put_task_struct(task);
2569 out_no_task:
2570         return error;
2571 }
2572
2573 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2574         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2575 {
2576         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2577                                 proc_pident_instantiate, task, p);
2578 }
2579
2580 static int proc_pident_readdir(struct file *filp,
2581                 void *dirent, filldir_t filldir,
2582                 const struct pid_entry *ents, unsigned int nents)
2583 {
2584         int i;
2585         struct dentry *dentry = filp->f_path.dentry;
2586         struct inode *inode = dentry->d_inode;
2587         struct task_struct *task = get_proc_task(inode);
2588         const struct pid_entry *p, *last;
2589         ino_t ino;
2590         int ret;
2591
2592         ret = -ENOENT;
2593         if (!task)
2594                 goto out_no_task;
2595
2596         ret = 0;
2597         i = filp->f_pos;
2598         switch (i) {
2599         case 0:
2600                 ino = inode->i_ino;
2601                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2602                         goto out;
2603                 i++;
2604                 filp->f_pos++;
2605                 /* fall through */
2606         case 1:
2607                 ino = parent_ino(dentry);
2608                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2609                         goto out;
2610                 i++;
2611                 filp->f_pos++;
2612                 /* fall through */
2613         default:
2614                 i -= 2;
2615                 if (i >= nents) {
2616                         ret = 1;
2617                         goto out;
2618                 }
2619                 p = ents + i;
2620                 last = &ents[nents - 1];
2621                 while (p <= last) {
2622                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2623                                 goto out;
2624                         filp->f_pos++;
2625                         p++;
2626                 }
2627         }
2628
2629         ret = 1;
2630 out:
2631         put_task_struct(task);
2632 out_no_task:
2633         return ret;
2634 }
2635
2636 #ifdef CONFIG_SECURITY
2637 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2638                                   size_t count, loff_t *ppos)
2639 {
2640         struct inode * inode = file->f_path.dentry->d_inode;
2641         char *p = NULL;
2642         ssize_t length;
2643         struct task_struct *task = get_proc_task(inode);
2644
2645         if (!task)
2646                 return -ESRCH;
2647
2648         length = security_getprocattr(task,
2649                                       (char*)file->f_path.dentry->d_name.name,
2650                                       &p);
2651         put_task_struct(task);
2652         if (length > 0)
2653                 length = simple_read_from_buffer(buf, count, ppos, p, length);
2654         kfree(p);
2655         return length;
2656 }
2657
2658 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2659                                    size_t count, loff_t *ppos)
2660 {
2661         struct inode * inode = file->f_path.dentry->d_inode;
2662         char *page;
2663         ssize_t length;
2664         struct task_struct *task = get_proc_task(inode);
2665
2666         length = -ESRCH;
2667         if (!task)
2668                 goto out_no_task;
2669         if (count > PAGE_SIZE)
2670                 count = PAGE_SIZE;
2671
2672         /* No partial writes. */
2673         length = -EINVAL;
2674         if (*ppos != 0)
2675                 goto out;
2676
2677         length = -ENOMEM;
2678         page = (char*)__get_free_page(GFP_TEMPORARY);
2679         if (!page)
2680                 goto out;
2681
2682         length = -EFAULT;
2683         if (copy_from_user(page, buf, count))
2684                 goto out_free;
2685
2686         /* Guard against adverse ptrace interaction */
2687         length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2688         if (length < 0)
2689                 goto out_free;
2690
2691         length = security_setprocattr(task,
2692                                       (char*)file->f_path.dentry->d_name.name,
2693                                       (void*)page, count);
2694         mutex_unlock(&task->signal->cred_guard_mutex);
2695 out_free:
2696         free_page((unsigned long) page);
2697 out:
2698         put_task_struct(task);
2699 out_no_task:
2700         return length;
2701 }
2702
2703 static const struct file_operations proc_pid_attr_operations = {
2704         .read           = proc_pid_attr_read,
2705         .write          = proc_pid_attr_write,
2706         .llseek         = generic_file_llseek,
2707 };
2708
2709 static const struct pid_entry attr_dir_stuff[] = {
2710         REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2711         REG("prev",       S_IRUGO,         proc_pid_attr_operations),
2712         REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2713         REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2714         REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2715         REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2716 };
2717
2718 static int proc_attr_dir_readdir(struct file * filp,
2719                              void * dirent, filldir_t filldir)
2720 {
2721         return proc_pident_readdir(filp,dirent,filldir,
2722                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2723 }
2724
2725 static const struct file_operations proc_attr_dir_operations = {
2726         .read           = generic_read_dir,
2727         .readdir        = proc_attr_dir_readdir,
2728         .llseek         = default_llseek,
2729 };
2730
2731 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2732                                 struct dentry *dentry, struct nameidata *nd)
2733 {
2734         return proc_pident_lookup(dir, dentry,
2735                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2736 }
2737
2738 static const struct inode_operations proc_attr_dir_inode_operations = {
2739         .lookup         = proc_attr_dir_lookup,
2740         .getattr        = pid_getattr,
2741         .setattr        = proc_setattr,
2742 };
2743
2744 #endif
2745
2746 #ifdef CONFIG_ELF_CORE
2747 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2748                                          size_t count, loff_t *ppos)
2749 {
2750         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2751         struct mm_struct *mm;
2752         char buffer[PROC_NUMBUF];
2753         size_t len;
2754         int ret;
2755
2756         if (!task)
2757                 return -ESRCH;
2758
2759         ret = 0;
2760         mm = get_task_mm(task);
2761         if (mm) {
2762                 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2763                                ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2764                                 MMF_DUMP_FILTER_SHIFT));
2765                 mmput(mm);
2766                 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2767         }
2768
2769         put_task_struct(task);
2770
2771         return ret;
2772 }
2773
2774 static ssize_t proc_coredump_filter_write(struct file *file,
2775                                           const char __user *buf,
2776                                           size_t count,
2777                                           loff_t *ppos)
2778 {
2779         struct task_struct *task;
2780         struct mm_struct *mm;
2781         char buffer[PROC_NUMBUF], *end;
2782         unsigned int val;
2783         int ret;
2784         int i;
2785         unsigned long mask;
2786
2787         ret = -EFAULT;
2788         memset(buffer, 0, sizeof(buffer));
2789         if (count > sizeof(buffer) - 1)
2790                 count = sizeof(buffer) - 1;
2791         if (copy_from_user(buffer, buf, count))
2792                 goto out_no_task;
2793
2794         ret = -EINVAL;
2795         val = (unsigned int)simple_strtoul(buffer, &end, 0);
2796         if (*end == '\n')
2797                 end++;
2798         if (end - buffer == 0)
2799                 goto out_no_task;
2800
2801         ret = -ESRCH;
2802         task = get_proc_task(file->f_dentry->d_inode);
2803         if (!task)
2804                 goto out_no_task;
2805
2806         ret = end - buffer;
2807         mm = get_task_mm(task);
2808         if (!mm)
2809                 goto out_no_mm;
2810
2811         for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2812                 if (val & mask)
2813                         set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2814                 else
2815                         clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2816         }
2817
2818         mmput(mm);
2819  out_no_mm:
2820         put_task_struct(task);
2821  out_no_task:
2822         return ret;
2823 }
2824
2825 static const struct file_operations proc_coredump_filter_operations = {
2826         .read           = proc_coredump_filter_read,
2827         .write          = proc_coredump_filter_write,
2828         .llseek         = generic_file_llseek,
2829 };
2830 #endif
2831
2832 /*
2833  * /proc/self:
2834  */
2835 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2836                               int buflen)
2837 {
2838         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2839         pid_t tgid = task_tgid_nr_ns(current, ns);
2840         char tmp[PROC_NUMBUF];
2841         if (!tgid)
2842                 return -ENOENT;
2843         sprintf(tmp, "%d", tgid);
2844         return vfs_readlink(dentry,buffer,buflen,tmp);
2845 }
2846
2847 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2848 {
2849         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2850         pid_t tgid = task_tgid_nr_ns(current, ns);
2851         char *name = ERR_PTR(-ENOENT);
2852         if (tgid) {
2853                 name = __getname();
2854                 if (!name)
2855                         name = ERR_PTR(-ENOMEM);
2856                 else
2857                         sprintf(name, "%d", tgid);
2858         }
2859         nd_set_link(nd, name);
2860         return NULL;
2861 }
2862
2863 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2864                                 void *cookie)
2865 {
2866         char *s = nd_get_link(nd);
2867         if (!IS_ERR(s))
2868                 __putname(s);
2869 }
2870
2871 static const struct inode_operations proc_self_inode_operations = {
2872         .readlink       = proc_self_readlink,
2873         .follow_link    = proc_self_follow_link,
2874         .put_link       = proc_self_put_link,
2875 };
2876
2877 /*
2878  * proc base
2879  *
2880  * These are the directory entries in the root directory of /proc
2881  * that properly belong to the /proc filesystem, as they describe
2882  * describe something that is process related.
2883  */
2884 static const struct pid_entry proc_base_stuff[] = {
2885         NOD("self", S_IFLNK|S_IRWXUGO,
2886                 &proc_self_inode_operations, NULL, {}),
2887 };
2888
2889 static struct dentry *proc_base_instantiate(struct inode *dir,
2890         struct dentry *dentry, struct task_struct *task, const void *ptr)
2891 {
2892         const struct pid_entry *p = ptr;
2893         struct inode *inode;
2894         struct proc_inode *ei;
2895         struct dentry *error;
2896
2897         /* Allocate the inode */
2898         error = ERR_PTR(-ENOMEM);
2899         inode = new_inode(dir->i_sb);
2900         if (!inode)
2901                 goto out;
2902
2903         /* Initialize the inode */
2904         ei = PROC_I(inode);
2905         inode->i_ino = get_next_ino();
2906         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2907
2908         /*
2909          * grab the reference to the task.
2910          */
2911         ei->pid = get_task_pid(task, PIDTYPE_PID);
2912         if (!ei->pid)
2913                 goto out_iput;
2914
2915         inode->i_mode = p->mode;
2916         if (S_ISDIR(inode->i_mode))
2917                 set_nlink(inode, 2);
2918         if (S_ISLNK(inode->i_mode))
2919                 inode->i_size = 64;
2920         if (p->iop)
2921                 inode->i_op = p->iop;
2922         if (p->fop)
2923                 inode->i_fop = p->fop;
2924         ei->op = p->op;
2925         d_add(dentry, inode);
2926         error = NULL;
2927 out:
2928         return error;
2929 out_iput:
2930         iput(inode);
2931         goto out;
2932 }
2933
2934 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2935 {
2936         struct dentry *error;
2937         struct task_struct *task = get_proc_task(dir);
2938         const struct pid_entry *p, *last;
2939
2940         error = ERR_PTR(-ENOENT);
2941
2942         if (!task)
2943                 goto out_no_task;
2944
2945         /* Lookup the directory entry */
2946         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2947         for (p = proc_base_stuff; p <= last; p++) {
2948                 if (p->len != dentry->d_name.len)
2949                         continue;
2950                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2951                         break;
2952         }
2953         if (p > last)
2954                 goto out;
2955
2956         error = proc_base_instantiate(dir, dentry, task, p);
2957
2958 out:
2959         put_task_struct(task);
2960 out_no_task:
2961         return error;
2962 }
2963
2964 static int proc_base_fill_cache(struct file *filp, void *dirent,
2965         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2966 {
2967         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2968                                 proc_base_instantiate, task, p);
2969 }
2970
2971 #ifdef CONFIG_TASK_IO_ACCOUNTING
2972 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2973 {
2974         struct task_io_accounting acct = task->ioac;
2975         unsigned long flags;
2976         int result;
2977
2978         result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2979         if (result)
2980                 return result;
2981
2982         if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2983                 result = -EACCES;
2984                 goto out_unlock;
2985         }
2986
2987         if (whole && lock_task_sighand(task, &flags)) {
2988                 struct task_struct *t = task;
2989
2990                 task_io_accounting_add(&acct, &task->signal->ioac);
2991                 while_each_thread(task, t)
2992                         task_io_accounting_add(&acct, &t->ioac);
2993
2994                 unlock_task_sighand(task, &flags);
2995         }
2996         result = sprintf(buffer,
2997                         "rchar: %llu\n"
2998                         "wchar: %llu\n"
2999                         "syscr: %llu\n"
3000                         "syscw: %llu\n"
3001                         "read_bytes: %llu\n"
3002                         "write_bytes: %llu\n"
3003                         "cancelled_write_bytes: %llu\n",
3004                         (unsigned long long)acct.rchar,
3005                         (unsigned long long)acct.wchar,
3006                         (unsigned long long)acct.syscr,
3007                         (unsigned long long)acct.syscw,
3008                         (unsigned long long)acct.read_bytes,
3009                         (unsigned long long)acct.write_bytes,
3010                         (unsigned long long)acct.cancelled_write_bytes);
3011 out_unlock:
3012         mutex_unlock(&task->signal->cred_guard_mutex);
3013         return result;
3014 }
3015
3016 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3017 {
3018         return do_io_accounting(task, buffer, 0);
3019 }
3020
3021 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3022 {
3023         return do_io_accounting(task, buffer, 1);
3024 }
3025 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3026
3027 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3028                                 struct pid *pid, struct task_struct *task)
3029 {
3030         int err = lock_trace(task);
3031         if (!err) {
3032                 seq_printf(m, "%08x\n", task->personality);
3033                 unlock_trace(task);
3034         }
3035         return err;
3036 }
3037
3038 /*
3039  * Thread groups
3040  */
3041 static const struct file_operations proc_task_operations;
3042 static const struct inode_operations proc_task_inode_operations;
3043
3044 static const struct pid_entry tgid_base_stuff[] = {
3045         DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3046         DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3047 #ifdef CONFIG_CHECKPOINT_RESTORE
3048         DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3049 #endif
3050         DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3051         DIR("ns",         S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3052 #ifdef CONFIG_NET
3053         DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3054 #endif
3055         REG("environ",    S_IRUSR, proc_environ_operations),
3056         INF("auxv",       S_IRUSR, proc_pid_auxv),
3057         ONE("status",     S_IRUGO, proc_pid_status),
3058         ONE("personality", S_IRUGO, proc_pid_personality),
3059         INF("limits",     S_IRUGO, proc_pid_limits),
3060 #ifdef CONFIG_SCHED_DEBUG
3061         REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3062 #endif
3063 #ifdef CONFIG_SCHED_AUTOGROUP
3064         REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3065 #endif
3066         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3067 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3068         INF("syscall",    S_IRUGO, proc_pid_syscall),
3069 #endif
3070         INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3071         ONE("stat",       S_IRUGO, proc_tgid_stat),
3072         ONE("statm",      S_IRUGO, proc_pid_statm),
3073         REG("maps",       S_IRUGO, proc_maps_operations),
3074         REG("arm_maps",   S_IRUGO, proc_armv7_maps_operations),
3075 #ifdef CONFIG_NUMA
3076         REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
3077 #endif
3078         REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3079         LNK("cwd",        proc_cwd_link),
3080         LNK("root",       proc_root_link),
3081         LNK("exe",        proc_exe_link),
3082         REG("mounts",     S_IRUGO, proc_mounts_operations),
3083         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3084         REG("mountstats", S_IRUSR, proc_mountstats_operations),
3085 #ifdef CONFIG_PROC_PAGE_MONITOR
3086         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3087         REG("smaps",      S_IRUGO, proc_smaps_operations),
3088         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3089 #endif
3090 #ifdef CONFIG_SECURITY
3091         DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3092 #endif
3093 #ifdef CONFIG_KALLSYMS
3094         INF("wchan",      S_IRUGO, proc_pid_wchan),
3095 #endif
3096 #ifdef CONFIG_STACKTRACE
3097         ONE("stack",      S_IRUGO, proc_pid_stack),
3098 #endif
3099 #ifdef CONFIG_SCHEDSTATS
3100         INF("schedstat",  S_IRUGO, proc_pid_schedstat),
3101 #endif
3102 #ifdef CONFIG_LATENCYTOP
3103         REG("latency",  S_IRUGO, proc_lstats_operations),
3104 #endif
3105 #ifdef CONFIG_PROC_PID_CPUSET
3106         REG("cpuset",     S_IRUGO, proc_cpuset_operations),
3107 #endif
3108 #ifdef CONFIG_CGROUPS
3109         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3110 #endif
3111         INF("oom_score",  S_IRUGO, proc_oom_score),
3112         REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3113         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3114 #ifdef CONFIG_AUDITSYSCALL
3115         REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3116         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3117 #endif
3118 #ifdef CONFIG_FAULT_INJECTION
3119         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3120 #endif
3121 #ifdef CONFIG_ELF_CORE
3122         REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3123 #endif
3124 #ifdef CONFIG_TASK_IO_ACCOUNTING
3125         INF("io",       S_IRUSR, proc_tgid_io_accounting),
3126 #endif
3127 #ifdef CONFIG_HARDWALL
3128         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3129 #endif
3130 };
3131
3132 static int proc_tgid_base_readdir(struct file * filp,
3133                              void * dirent, filldir_t filldir)
3134 {
3135         return proc_pident_readdir(filp,dirent,filldir,
3136                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3137 }
3138
3139 static const struct file_operations proc_tgid_base_operations = {
3140         .read           = generic_read_dir,
3141         .readdir        = proc_tgid_base_readdir,
3142         .llseek         = default_llseek,
3143 };
3144
3145 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3146         return proc_pident_lookup(dir, dentry,
3147                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3148 }
3149
3150 static const struct inode_operations proc_tgid_base_inode_operations = {
3151         .lookup         = proc_tgid_base_lookup,
3152         .getattr        = pid_getattr,
3153         .setattr        = proc_setattr,
3154 };
3155
3156 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3157 {
3158         struct dentry *dentry, *leader, *dir;
3159         char buf[PROC_NUMBUF];
3160         struct qstr name;
3161
3162         name.name = buf;
3163         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3164         dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3165         if (dentry) {
3166                 shrink_dcache_parent(dentry);
3167                 d_drop(dentry);
3168                 dput(dentry);
3169         }
3170
3171         name.name = buf;
3172         name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3173         leader = d_hash_and_lookup(mnt->mnt_root, &name);
3174         if (!leader)
3175                 goto out;
3176
3177         name.name = "task";
3178         name.len = strlen(name.name);
3179         dir = d_hash_and_lookup(leader, &name);
3180         if (!dir)
3181                 goto out_put_leader;
3182
3183         name.name = buf;
3184         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3185         dentry = d_hash_and_lookup(dir, &name);
3186         if (dentry) {
3187                 shrink_dcache_parent(dentry);
3188                 d_drop(dentry);
3189                 dput(dentry);
3190         }
3191
3192         dput(dir);
3193 out_put_leader:
3194         dput(leader);
3195 out:
3196         return;
3197 }
3198
3199 /**
3200  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
3201  * @task: task that should be flushed.
3202  *
3203  * When flushing dentries from proc, one needs to flush them from global
3204  * proc (proc_mnt) and from all the namespaces' procs this task was seen
3205  * in. This call is supposed to do all of this job.
3206  *
3207  * Looks in the dcache for
3208  * /proc/@pid
3209  * /proc/@tgid/task/@pid
3210  * if either directory is present flushes it and all of it'ts children
3211  * from the dcache.
3212  *
3213  * It is safe and reasonable to cache /proc entries for a task until
3214  * that task exits.  After that they just clog up the dcache with
3215  * useless entries, possibly causing useful dcache entries to be
3216  * flushed instead.  This routine is proved to flush those useless
3217  * dcache entries at process exit time.
3218  *
3219  * NOTE: This routine is just an optimization so it does not guarantee
3220  *       that no dcache entries will exist at process exit time it
3221  *       just makes it very unlikely that any will persist.
3222  */
3223
3224 void proc_flush_task(struct task_struct *task)
3225 {
3226         int i;
3227         struct pid *pid, *tgid;
3228         struct upid *upid;
3229
3230         pid = task_pid(task);
3231         tgid = task_tgid(task);
3232
3233         for (i = 0; i <= pid->level; i++) {
3234                 upid = &pid->numbers[i];
3235                 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3236                                         tgid->numbers[i].nr);
3237         }
3238
3239         upid = &pid->numbers[pid->level];
3240         if (upid->nr == 1)
3241                 pid_ns_release_proc(upid->ns);
3242 }
3243
3244 static struct dentry *proc_pid_instantiate(struct inode *dir,
3245                                            struct dentry * dentry,
3246                                            struct task_struct *task, const void *ptr)
3247 {
3248         struct dentry *error = ERR_PTR(-ENOENT);
3249         struct inode *inode;
3250
3251         inode = proc_pid_make_inode(dir->i_sb, task);
3252         if (!inode)
3253                 goto out;
3254
3255         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3256         inode->i_op = &proc_tgid_base_inode_operations;
3257         inode->i_fop = &proc_tgid_base_operations;
3258         inode->i_flags|=S_IMMUTABLE;
3259
3260         set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3261                                                   ARRAY_SIZE(tgid_base_stuff)));
3262
3263         d_set_d_op(dentry, &pid_dentry_operations);
3264
3265         d_add(dentry, inode);
3266         /* Close the race of the process dying before we return the dentry */
3267         if (pid_revalidate(dentry, NULL))
3268                 error = NULL;
3269 out:
3270         return error;
3271 }
3272
3273 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3274 {
3275         struct dentry *result;
3276         struct task_struct *task;
3277         unsigned tgid;
3278         struct pid_namespace *ns;
3279
3280         result = proc_base_lookup(dir, dentry);
3281         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3282                 goto out;
3283
3284         tgid = name_to_int(dentry);
3285         if (tgid == ~0U)
3286                 goto out;
3287
3288         ns = dentry->d_sb->s_fs_info;
3289         rcu_read_lock();
3290         task = find_task_by_pid_ns(tgid, ns);
3291         if (task)
3292                 get_task_struct(task);
3293         rcu_read_unlock();
3294         if (!task)
3295                 goto out;
3296
3297         result = proc_pid_instantiate(dir, dentry, task, NULL);
3298         put_task_struct(task);
3299 out:
3300         return result;
3301 }
3302
3303 /*
3304  * Find the first task with tgid >= tgid
3305  *
3306  */
3307 struct tgid_iter {
3308         unsigned int tgid;
3309         struct task_struct *task;
3310 };
3311 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3312 {
3313         struct pid *pid;
3314
3315         if (iter.task)
3316                 put_task_struct(iter.task);
3317         rcu_read_lock();
3318 retry:
3319         iter.task = NULL;
3320         pid = find_ge_pid(iter.tgid, ns);
3321         if (pid) {
3322                 iter.tgid = pid_nr_ns(pid, ns);
3323                 iter.task = pid_task(pid, PIDTYPE_PID);
3324                 /* What we to know is if the pid we have find is the
3325                  * pid of a thread_group_leader.  Testing for task
3326                  * being a thread_group_leader is the obvious thing
3327                  * todo but there is a window when it fails, due to
3328                  * the pid transfer logic in de_thread.
3329                  *
3330                  * So we perform the straight forward test of seeing
3331                  * if the pid we have found is the pid of a thread
3332                  * group leader, and don't worry if the task we have
3333                  * found doesn't happen to be a thread group leader.
3334                  * As we don't care in the case of readdir.
3335                  */
3336                 if (!iter.task || !has_group_leader_pid(iter.task)) {
3337                         iter.tgid += 1;
3338                         goto retry;
3339                 }
3340                 get_task_struct(iter.task);
3341         }
3342         rcu_read_unlock();
3343         return iter;
3344 }
3345
3346 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3347
3348 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3349         struct tgid_iter iter)
3350 {
3351         char name[PROC_NUMBUF];
3352         int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3353         return proc_fill_cache(filp, dirent, filldir, name, len,
3354                                 proc_pid_instantiate, iter.task, NULL);
3355 }
3356
3357 /* for the /proc/ directory itself, after non-process stuff has been done */
3358 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3359 {
3360         unsigned int nr;
3361         struct task_struct *reaper;
3362         struct tgid_iter iter;
3363         struct pid_namespace *ns;
3364
3365         if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3366                 goto out_no_task;
3367         nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3368
3369         reaper = get_proc_task(filp->f_path.dentry->d_inode);
3370         if (!reaper)
3371                 goto out_no_task;
3372
3373         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3374                 const struct pid_entry *p = &proc_base_stuff[nr];
3375                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3376                         goto out;
3377         }
3378
3379         ns = filp->f_dentry->d_sb->s_fs_info;
3380         iter.task = NULL;
3381         iter.tgid = filp->f_pos - TGID_OFFSET;
3382         for (iter = next_tgid(ns, iter);
3383              iter.task;
3384              iter.tgid += 1, iter = next_tgid(ns, iter)) {
3385                 filp->f_pos = iter.tgid + TGID_OFFSET;
3386                 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3387                         put_task_struct(iter.task);
3388                         goto out;
3389                 }
3390         }
3391         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3392 out:
3393         put_task_struct(reaper);
3394 out_no_task:
3395         return 0;
3396 }
3397
3398 /*
3399  * Tasks
3400  */
3401 static const struct pid_entry tid_base_stuff[] = {
3402         DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3403         DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3404         DIR("ns",        S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3405         REG("environ",   S_IRUSR, proc_environ_operations),
3406         INF("auxv",      S_IRUSR, proc_pid_auxv),
3407         ONE("status",    S_IRUGO, proc_pid_status),
3408         ONE("personality", S_IRUGO, proc_pid_personality),
3409         INF("limits",    S_IRUGO, proc_pid_limits),
3410 #ifdef CONFIG_SCHED_DEBUG
3411         REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3412 #endif
3413         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3414 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3415         INF("syscall",   S_IRUGO, proc_pid_syscall),
3416 #endif
3417         INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3418         ONE("stat",      S_IRUGO, proc_tid_stat),
3419         ONE("statm",     S_IRUGO, proc_pid_statm),
3420         REG("maps",      S_IRUGO, proc_maps_operations),
3421         REG("arm_maps",  S_IRUGO, proc_armv7_maps_operations),
3422 #ifdef CONFIG_NUMA
3423         REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3424 #endif
3425         REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3426         LNK("cwd",       proc_cwd_link),
3427         LNK("root",      proc_root_link),
3428         LNK("exe",       proc_exe_link),
3429         REG("mounts",    S_IRUGO, proc_mounts_operations),
3430         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3431 #ifdef CONFIG_PROC_PAGE_MONITOR
3432         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3433         REG("smaps",     S_IRUGO, proc_smaps_operations),
3434         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3435 #endif
3436 #ifdef CONFIG_SECURITY
3437         DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3438 #endif
3439 #ifdef CONFIG_KALLSYMS
3440         INF("wchan",     S_IRUGO, proc_pid_wchan),
3441 #endif
3442 #ifdef CONFIG_STACKTRACE
3443         ONE("stack",      S_IRUGO, proc_pid_stack),
3444 #endif
3445 #ifdef CONFIG_SCHEDSTATS
3446         INF("schedstat", S_IRUGO, proc_pid_schedstat),
3447 #endif
3448 #ifdef CONFIG_LATENCYTOP
3449         REG("latency",  S_IRUGO, proc_lstats_operations),
3450 #endif
3451 #ifdef CONFIG_PROC_PID_CPUSET
3452         REG("cpuset",    S_IRUGO, proc_cpuset_operations),
3453 #endif
3454 #ifdef CONFIG_CGROUPS
3455         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3456 #endif
3457         INF("oom_score", S_IRUGO, proc_oom_score),
3458         REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3459         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3460 #ifdef CONFIG_AUDITSYSCALL
3461         REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3462         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3463 #endif
3464 #ifdef CONFIG_FAULT_INJECTION
3465         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3466 #endif
3467 #ifdef CONFIG_TASK_IO_ACCOUNTING
3468         INF("io",       S_IRUSR, proc_tid_io_accounting),
3469 #endif
3470 #ifdef CONFIG_HARDWALL
3471         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3472 #endif
3473 };
3474
3475 static int proc_tid_base_readdir(struct file * filp,
3476                              void * dirent, filldir_t filldir)
3477 {
3478         return proc_pident_readdir(filp,dirent,filldir,
3479                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3480 }
3481
3482 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3483         return proc_pident_lookup(dir, dentry,
3484                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3485 }
3486
3487 static const struct file_operations proc_tid_base_operations = {
3488         .read           = generic_read_dir,
3489         .readdir        = proc_tid_base_readdir,
3490         .llseek         = default_llseek,
3491 };
3492
3493 static const struct inode_operations proc_tid_base_inode_operations = {
3494         .lookup         = proc_tid_base_lookup,
3495         .getattr        = pid_getattr,
3496         .setattr        = proc_setattr,
3497 };
3498
3499 static struct dentry *proc_task_instantiate(struct inode *dir,
3500         struct dentry *dentry, struct task_struct *task, const void *ptr)
3501 {
3502         struct dentry *error = ERR_PTR(-ENOENT);
3503         struct inode *inode;
3504         inode = proc_pid_make_inode(dir->i_sb, task);
3505
3506         if (!inode)
3507                 goto out;
3508         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3509         inode->i_op = &proc_tid_base_inode_operations;
3510         inode->i_fop = &proc_tid_base_operations;
3511         inode->i_flags|=S_IMMUTABLE;
3512
3513         set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3514                                                   ARRAY_SIZE(tid_base_stuff)));
3515
3516         d_set_d_op(dentry, &pid_dentry_operations);
3517
3518         d_add(dentry, inode);
3519         /* Close the race of the process dying before we return the dentry */
3520         if (pid_revalidate(dentry, NULL))
3521                 error = NULL;
3522 out:
3523         return error;
3524 }
3525
3526 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3527 {
3528         struct dentry *result = ERR_PTR(-ENOENT);
3529         struct task_struct *task;
3530         struct task_struct *leader = get_proc_task(dir);
3531         unsigned tid;
3532         struct pid_namespace *ns;
3533
3534         if (!leader)
3535                 goto out_no_task;
3536
3537         tid = name_to_int(dentry);
3538         if (tid == ~0U)
3539                 goto out;
3540
3541         ns = dentry->d_sb->s_fs_info;
3542         rcu_read_lock();
3543         task = find_task_by_pid_ns(tid, ns);
3544         if (task)
3545                 get_task_struct(task);
3546         rcu_read_unlock();
3547         if (!task)
3548                 goto out;
3549         if (!same_thread_group(leader, task))
3550                 goto out_drop_task;
3551
3552         result = proc_task_instantiate(dir, dentry, task, NULL);
3553 out_drop_task:
3554         put_task_struct(task);
3555 out:
3556         put_task_struct(leader);
3557 out_no_task:
3558         return result;
3559 }
3560
3561 /*
3562  * Find the first tid of a thread group to return to user space.
3563  *
3564  * Usually this is just the thread group leader, but if the users
3565  * buffer was too small or there was a seek into the middle of the
3566  * directory we have more work todo.
3567  *
3568  * In the case of a short read we start with find_task_by_pid.
3569  *
3570  * In the case of a seek we start with the leader and walk nr
3571  * threads past it.
3572  */
3573 static struct task_struct *first_tid(struct task_struct *leader,
3574                 int tid, int nr, struct pid_namespace *ns)
3575 {
3576         struct task_struct *pos;
3577
3578         rcu_read_lock();
3579         /* Attempt to start with the pid of a thread */
3580         if (tid && (nr > 0)) {
3581                 pos = find_task_by_pid_ns(tid, ns);
3582                 if (pos && (pos->group_leader == leader))
3583                         goto found;
3584         }
3585
3586         /* If nr exceeds the number of threads there is nothing todo */
3587         pos = NULL;
3588         if (nr && nr >= get_nr_threads(leader))
3589                 goto out;
3590
3591         /* If we haven't found our starting place yet start
3592          * with the leader and walk nr threads forward.
3593          */
3594         for (pos = leader; nr > 0; --nr) {
3595                 pos = next_thread(pos);
3596                 if (pos == leader) {
3597                         pos = NULL;
3598                         goto out;
3599                 }
3600         }
3601 found:
3602         get_task_struct(pos);
3603 out:
3604         rcu_read_unlock();
3605         return pos;
3606 }
3607
3608 /*
3609  * Find the next thread in the thread list.
3610  * Return NULL if there is an error or no next thread.
3611  *
3612  * The reference to the input task_struct is released.
3613  */
3614 static struct task_struct *next_tid(struct task_struct *start)
3615 {
3616         struct task_struct *pos = NULL;
3617         rcu_read_lock();
3618         if (pid_alive(start)) {
3619                 pos = next_thread(start);
3620                 if (thread_group_leader(pos))
3621                         pos = NULL;
3622                 else
3623                         get_task_struct(pos);
3624         }
3625         rcu_read_unlock();
3626         put_task_struct(start);
3627         return pos;
3628 }
3629
3630 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3631         struct task_struct *task, int tid)
3632 {
3633         char name[PROC_NUMBUF];
3634         int len = snprintf(name, sizeof(name), "%d", tid);
3635         return proc_fill_cache(filp, dirent, filldir, name, len,
3636                                 proc_task_instantiate, task, NULL);
3637 }
3638
3639 /* for the /proc/TGID/task/ directories */
3640 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3641 {
3642         struct dentry *dentry = filp->f_path.dentry;
3643         struct inode *inode = dentry->d_inode;
3644         struct task_struct *leader = NULL;
3645         struct task_struct *task;
3646         int retval = -ENOENT;
3647         ino_t ino;
3648         int tid;
3649         struct pid_namespace *ns;
3650
3651         task = get_proc_task(inode);
3652         if (!task)
3653                 goto out_no_task;
3654         rcu_read_lock();
3655         if (pid_alive(task)) {
3656                 leader = task->group_leader;
3657                 get_task_struct(leader);
3658         }
3659         rcu_read_unlock();
3660         put_task_struct(task);
3661         if (!leader)
3662                 goto out_no_task;
3663         retval = 0;
3664
3665         switch ((unsigned long)filp->f_pos) {
3666         case 0:
3667                 ino = inode->i_ino;
3668                 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3669                         goto out;
3670                 filp->f_pos++;
3671                 /* fall through */
3672         case 1:
3673                 ino = parent_ino(dentry);
3674                 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3675                         goto out;
3676                 filp->f_pos++;
3677                 /* fall through */
3678         }
3679
3680         /* f_version caches the tgid value that the last readdir call couldn't
3681          * return. lseek aka telldir automagically resets f_version to 0.
3682          */
3683         ns = filp->f_dentry->d_sb->s_fs_info;
3684         tid = (int)filp->f_version;
3685         filp->f_version = 0;
3686         for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3687              task;
3688              task = next_tid(task), filp->f_pos++) {
3689                 tid = task_pid_nr_ns(task, ns);
3690                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3691                         /* returning this tgid failed, save it as the first
3692                          * pid for the next readir call */
3693                         filp->f_version = (u64)tid;
3694                         put_task_struct(task);
3695                         break;
3696                 }
3697         }
3698 out:
3699         put_task_struct(leader);
3700 out_no_task:
3701         return retval;
3702 }
3703
3704 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3705 {
3706         struct inode *inode = dentry->d_inode;
3707         struct task_struct *p = get_proc_task(inode);
3708         generic_fillattr(inode, stat);
3709
3710         if (p) {
3711                 stat->nlink += get_nr_threads(p);
3712                 put_task_struct(p);
3713         }
3714
3715         return 0;
3716 }
3717
3718 static const struct inode_operations proc_task_inode_operations = {
3719         .lookup         = proc_task_lookup,
3720         .getattr        = proc_task_getattr,
3721         .setattr        = proc_setattr,
3722 };
3723
3724 static const struct file_operations proc_task_operations = {
3725         .read           = generic_read_dir,
3726         .readdir        = proc_task_readdir,
3727         .llseek         = default_llseek,
3728 };