make get_file() return its argument
[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                                 info.file = get_file(vma->vm_file);
2381                                 info.len = snprintf(info.name,
2382                                                 sizeof(info.name), "%lx-%lx",
2383                                                 vma->vm_start, vma->vm_end);
2384                                 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2385                                         BUG();
2386                         }
2387                 }
2388                 up_read(&mm->mmap_sem);
2389
2390                 for (i = 0; i < nr_files; i++) {
2391                         p = flex_array_get(fa, i);
2392                         ret = proc_fill_cache(filp, dirent, filldir,
2393                                               p->name, p->len,
2394                                               proc_map_files_instantiate,
2395                                               task, p->file);
2396                         if (ret)
2397                                 break;
2398                         filp->f_pos++;
2399                         fput(p->file);
2400                 }
2401                 for (; i < nr_files; i++) {
2402                         /*
2403                          * In case of error don't forget
2404                          * to put rest of file refs.
2405                          */
2406                         p = flex_array_get(fa, i);
2407                         fput(p->file);
2408                 }
2409                 if (fa)
2410                         flex_array_free(fa);
2411                 mmput(mm);
2412         }
2413         }
2414
2415 out_unlock:
2416         unlock_trace(task);
2417 out_put_task:
2418         put_task_struct(task);
2419 out:
2420         return ret;
2421 }
2422
2423 static const struct file_operations proc_map_files_operations = {
2424         .read           = generic_read_dir,
2425         .readdir        = proc_map_files_readdir,
2426         .llseek         = default_llseek,
2427 };
2428
2429 #endif /* CONFIG_CHECKPOINT_RESTORE */
2430
2431 /*
2432  * /proc/pid/fd needs a special permission handler so that a process can still
2433  * access /proc/self/fd after it has executed a setuid().
2434  */
2435 static int proc_fd_permission(struct inode *inode, int mask)
2436 {
2437         int rv = generic_permission(inode, mask);
2438         if (rv == 0)
2439                 return 0;
2440         if (task_pid(current) == proc_pid(inode))
2441                 rv = 0;
2442         return rv;
2443 }
2444
2445 /*
2446  * proc directories can do almost nothing..
2447  */
2448 static const struct inode_operations proc_fd_inode_operations = {
2449         .lookup         = proc_lookupfd,
2450         .permission     = proc_fd_permission,
2451         .setattr        = proc_setattr,
2452 };
2453
2454 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2455         struct dentry *dentry, struct task_struct *task, const void *ptr)
2456 {
2457         unsigned fd = *(unsigned *)ptr;
2458         struct inode *inode;
2459         struct proc_inode *ei;
2460         struct dentry *error = ERR_PTR(-ENOENT);
2461
2462         inode = proc_pid_make_inode(dir->i_sb, task);
2463         if (!inode)
2464                 goto out;
2465         ei = PROC_I(inode);
2466         ei->fd = fd;
2467         inode->i_mode = S_IFREG | S_IRUSR;
2468         inode->i_fop = &proc_fdinfo_file_operations;
2469         d_set_d_op(dentry, &tid_fd_dentry_operations);
2470         d_add(dentry, inode);
2471         /* Close the race of the process dying before we return the dentry */
2472         if (tid_fd_revalidate(dentry, NULL))
2473                 error = NULL;
2474
2475  out:
2476         return error;
2477 }
2478
2479 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2480                                         struct dentry *dentry,
2481                                         struct nameidata *nd)
2482 {
2483         return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2484 }
2485
2486 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2487 {
2488         return proc_readfd_common(filp, dirent, filldir,
2489                                   proc_fdinfo_instantiate);
2490 }
2491
2492 static const struct file_operations proc_fdinfo_operations = {
2493         .read           = generic_read_dir,
2494         .readdir        = proc_readfdinfo,
2495         .llseek         = default_llseek,
2496 };
2497
2498 /*
2499  * proc directories can do almost nothing..
2500  */
2501 static const struct inode_operations proc_fdinfo_inode_operations = {
2502         .lookup         = proc_lookupfdinfo,
2503         .setattr        = proc_setattr,
2504 };
2505
2506
2507 static struct dentry *proc_pident_instantiate(struct inode *dir,
2508         struct dentry *dentry, struct task_struct *task, const void *ptr)
2509 {
2510         const struct pid_entry *p = ptr;
2511         struct inode *inode;
2512         struct proc_inode *ei;
2513         struct dentry *error = ERR_PTR(-ENOENT);
2514
2515         inode = proc_pid_make_inode(dir->i_sb, task);
2516         if (!inode)
2517                 goto out;
2518
2519         ei = PROC_I(inode);
2520         inode->i_mode = p->mode;
2521         if (S_ISDIR(inode->i_mode))
2522                 set_nlink(inode, 2);    /* Use getattr to fix if necessary */
2523         if (p->iop)
2524                 inode->i_op = p->iop;
2525         if (p->fop)
2526                 inode->i_fop = p->fop;
2527         ei->op = p->op;
2528         d_set_d_op(dentry, &pid_dentry_operations);
2529         d_add(dentry, inode);
2530         /* Close the race of the process dying before we return the dentry */
2531         if (pid_revalidate(dentry, NULL))
2532                 error = NULL;
2533 out:
2534         return error;
2535 }
2536
2537 static struct dentry *proc_pident_lookup(struct inode *dir, 
2538                                          struct dentry *dentry,
2539                                          const struct pid_entry *ents,
2540                                          unsigned int nents)
2541 {
2542         struct dentry *error;
2543         struct task_struct *task = get_proc_task(dir);
2544         const struct pid_entry *p, *last;
2545
2546         error = ERR_PTR(-ENOENT);
2547
2548         if (!task)
2549                 goto out_no_task;
2550
2551         /*
2552          * Yes, it does not scale. And it should not. Don't add
2553          * new entries into /proc/<tgid>/ without very good reasons.
2554          */
2555         last = &ents[nents - 1];
2556         for (p = ents; p <= last; p++) {
2557                 if (p->len != dentry->d_name.len)
2558                         continue;
2559                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2560                         break;
2561         }
2562         if (p > last)
2563                 goto out;
2564
2565         error = proc_pident_instantiate(dir, dentry, task, p);
2566 out:
2567         put_task_struct(task);
2568 out_no_task:
2569         return error;
2570 }
2571
2572 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2573         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2574 {
2575         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2576                                 proc_pident_instantiate, task, p);
2577 }
2578
2579 static int proc_pident_readdir(struct file *filp,
2580                 void *dirent, filldir_t filldir,
2581                 const struct pid_entry *ents, unsigned int nents)
2582 {
2583         int i;
2584         struct dentry *dentry = filp->f_path.dentry;
2585         struct inode *inode = dentry->d_inode;
2586         struct task_struct *task = get_proc_task(inode);
2587         const struct pid_entry *p, *last;
2588         ino_t ino;
2589         int ret;
2590
2591         ret = -ENOENT;
2592         if (!task)
2593                 goto out_no_task;
2594
2595         ret = 0;
2596         i = filp->f_pos;
2597         switch (i) {
2598         case 0:
2599                 ino = inode->i_ino;
2600                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2601                         goto out;
2602                 i++;
2603                 filp->f_pos++;
2604                 /* fall through */
2605         case 1:
2606                 ino = parent_ino(dentry);
2607                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2608                         goto out;
2609                 i++;
2610                 filp->f_pos++;
2611                 /* fall through */
2612         default:
2613                 i -= 2;
2614                 if (i >= nents) {
2615                         ret = 1;
2616                         goto out;
2617                 }
2618                 p = ents + i;
2619                 last = &ents[nents - 1];
2620                 while (p <= last) {
2621                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2622                                 goto out;
2623                         filp->f_pos++;
2624                         p++;
2625                 }
2626         }
2627
2628         ret = 1;
2629 out:
2630         put_task_struct(task);
2631 out_no_task:
2632         return ret;
2633 }
2634
2635 #ifdef CONFIG_SECURITY
2636 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2637                                   size_t count, loff_t *ppos)
2638 {
2639         struct inode * inode = file->f_path.dentry->d_inode;
2640         char *p = NULL;
2641         ssize_t length;
2642         struct task_struct *task = get_proc_task(inode);
2643
2644         if (!task)
2645                 return -ESRCH;
2646
2647         length = security_getprocattr(task,
2648                                       (char*)file->f_path.dentry->d_name.name,
2649                                       &p);
2650         put_task_struct(task);
2651         if (length > 0)
2652                 length = simple_read_from_buffer(buf, count, ppos, p, length);
2653         kfree(p);
2654         return length;
2655 }
2656
2657 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2658                                    size_t count, loff_t *ppos)
2659 {
2660         struct inode * inode = file->f_path.dentry->d_inode;
2661         char *page;
2662         ssize_t length;
2663         struct task_struct *task = get_proc_task(inode);
2664
2665         length = -ESRCH;
2666         if (!task)
2667                 goto out_no_task;
2668         if (count > PAGE_SIZE)
2669                 count = PAGE_SIZE;
2670
2671         /* No partial writes. */
2672         length = -EINVAL;
2673         if (*ppos != 0)
2674                 goto out;
2675
2676         length = -ENOMEM;
2677         page = (char*)__get_free_page(GFP_TEMPORARY);
2678         if (!page)
2679                 goto out;
2680
2681         length = -EFAULT;
2682         if (copy_from_user(page, buf, count))
2683                 goto out_free;
2684
2685         /* Guard against adverse ptrace interaction */
2686         length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2687         if (length < 0)
2688                 goto out_free;
2689
2690         length = security_setprocattr(task,
2691                                       (char*)file->f_path.dentry->d_name.name,
2692                                       (void*)page, count);
2693         mutex_unlock(&task->signal->cred_guard_mutex);
2694 out_free:
2695         free_page((unsigned long) page);
2696 out:
2697         put_task_struct(task);
2698 out_no_task:
2699         return length;
2700 }
2701
2702 static const struct file_operations proc_pid_attr_operations = {
2703         .read           = proc_pid_attr_read,
2704         .write          = proc_pid_attr_write,
2705         .llseek         = generic_file_llseek,
2706 };
2707
2708 static const struct pid_entry attr_dir_stuff[] = {
2709         REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2710         REG("prev",       S_IRUGO,         proc_pid_attr_operations),
2711         REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2712         REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2713         REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2714         REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2715 };
2716
2717 static int proc_attr_dir_readdir(struct file * filp,
2718                              void * dirent, filldir_t filldir)
2719 {
2720         return proc_pident_readdir(filp,dirent,filldir,
2721                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2722 }
2723
2724 static const struct file_operations proc_attr_dir_operations = {
2725         .read           = generic_read_dir,
2726         .readdir        = proc_attr_dir_readdir,
2727         .llseek         = default_llseek,
2728 };
2729
2730 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2731                                 struct dentry *dentry, struct nameidata *nd)
2732 {
2733         return proc_pident_lookup(dir, dentry,
2734                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2735 }
2736
2737 static const struct inode_operations proc_attr_dir_inode_operations = {
2738         .lookup         = proc_attr_dir_lookup,
2739         .getattr        = pid_getattr,
2740         .setattr        = proc_setattr,
2741 };
2742
2743 #endif
2744
2745 #ifdef CONFIG_ELF_CORE
2746 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2747                                          size_t count, loff_t *ppos)
2748 {
2749         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2750         struct mm_struct *mm;
2751         char buffer[PROC_NUMBUF];
2752         size_t len;
2753         int ret;
2754
2755         if (!task)
2756                 return -ESRCH;
2757
2758         ret = 0;
2759         mm = get_task_mm(task);
2760         if (mm) {
2761                 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2762                                ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2763                                 MMF_DUMP_FILTER_SHIFT));
2764                 mmput(mm);
2765                 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2766         }
2767
2768         put_task_struct(task);
2769
2770         return ret;
2771 }
2772
2773 static ssize_t proc_coredump_filter_write(struct file *file,
2774                                           const char __user *buf,
2775                                           size_t count,
2776                                           loff_t *ppos)
2777 {
2778         struct task_struct *task;
2779         struct mm_struct *mm;
2780         char buffer[PROC_NUMBUF], *end;
2781         unsigned int val;
2782         int ret;
2783         int i;
2784         unsigned long mask;
2785
2786         ret = -EFAULT;
2787         memset(buffer, 0, sizeof(buffer));
2788         if (count > sizeof(buffer) - 1)
2789                 count = sizeof(buffer) - 1;
2790         if (copy_from_user(buffer, buf, count))
2791                 goto out_no_task;
2792
2793         ret = -EINVAL;
2794         val = (unsigned int)simple_strtoul(buffer, &end, 0);
2795         if (*end == '\n')
2796                 end++;
2797         if (end - buffer == 0)
2798                 goto out_no_task;
2799
2800         ret = -ESRCH;
2801         task = get_proc_task(file->f_dentry->d_inode);
2802         if (!task)
2803                 goto out_no_task;
2804
2805         ret = end - buffer;
2806         mm = get_task_mm(task);
2807         if (!mm)
2808                 goto out_no_mm;
2809
2810         for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2811                 if (val & mask)
2812                         set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2813                 else
2814                         clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2815         }
2816
2817         mmput(mm);
2818  out_no_mm:
2819         put_task_struct(task);
2820  out_no_task:
2821         return ret;
2822 }
2823
2824 static const struct file_operations proc_coredump_filter_operations = {
2825         .read           = proc_coredump_filter_read,
2826         .write          = proc_coredump_filter_write,
2827         .llseek         = generic_file_llseek,
2828 };
2829 #endif
2830
2831 /*
2832  * /proc/self:
2833  */
2834 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2835                               int buflen)
2836 {
2837         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2838         pid_t tgid = task_tgid_nr_ns(current, ns);
2839         char tmp[PROC_NUMBUF];
2840         if (!tgid)
2841                 return -ENOENT;
2842         sprintf(tmp, "%d", tgid);
2843         return vfs_readlink(dentry,buffer,buflen,tmp);
2844 }
2845
2846 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2847 {
2848         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2849         pid_t tgid = task_tgid_nr_ns(current, ns);
2850         char *name = ERR_PTR(-ENOENT);
2851         if (tgid) {
2852                 name = __getname();
2853                 if (!name)
2854                         name = ERR_PTR(-ENOMEM);
2855                 else
2856                         sprintf(name, "%d", tgid);
2857         }
2858         nd_set_link(nd, name);
2859         return NULL;
2860 }
2861
2862 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2863                                 void *cookie)
2864 {
2865         char *s = nd_get_link(nd);
2866         if (!IS_ERR(s))
2867                 __putname(s);
2868 }
2869
2870 static const struct inode_operations proc_self_inode_operations = {
2871         .readlink       = proc_self_readlink,
2872         .follow_link    = proc_self_follow_link,
2873         .put_link       = proc_self_put_link,
2874 };
2875
2876 /*
2877  * proc base
2878  *
2879  * These are the directory entries in the root directory of /proc
2880  * that properly belong to the /proc filesystem, as they describe
2881  * describe something that is process related.
2882  */
2883 static const struct pid_entry proc_base_stuff[] = {
2884         NOD("self", S_IFLNK|S_IRWXUGO,
2885                 &proc_self_inode_operations, NULL, {}),
2886 };
2887
2888 static struct dentry *proc_base_instantiate(struct inode *dir,
2889         struct dentry *dentry, struct task_struct *task, const void *ptr)
2890 {
2891         const struct pid_entry *p = ptr;
2892         struct inode *inode;
2893         struct proc_inode *ei;
2894         struct dentry *error;
2895
2896         /* Allocate the inode */
2897         error = ERR_PTR(-ENOMEM);
2898         inode = new_inode(dir->i_sb);
2899         if (!inode)
2900                 goto out;
2901
2902         /* Initialize the inode */
2903         ei = PROC_I(inode);
2904         inode->i_ino = get_next_ino();
2905         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2906
2907         /*
2908          * grab the reference to the task.
2909          */
2910         ei->pid = get_task_pid(task, PIDTYPE_PID);
2911         if (!ei->pid)
2912                 goto out_iput;
2913
2914         inode->i_mode = p->mode;
2915         if (S_ISDIR(inode->i_mode))
2916                 set_nlink(inode, 2);
2917         if (S_ISLNK(inode->i_mode))
2918                 inode->i_size = 64;
2919         if (p->iop)
2920                 inode->i_op = p->iop;
2921         if (p->fop)
2922                 inode->i_fop = p->fop;
2923         ei->op = p->op;
2924         d_add(dentry, inode);
2925         error = NULL;
2926 out:
2927         return error;
2928 out_iput:
2929         iput(inode);
2930         goto out;
2931 }
2932
2933 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2934 {
2935         struct dentry *error;
2936         struct task_struct *task = get_proc_task(dir);
2937         const struct pid_entry *p, *last;
2938
2939         error = ERR_PTR(-ENOENT);
2940
2941         if (!task)
2942                 goto out_no_task;
2943
2944         /* Lookup the directory entry */
2945         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2946         for (p = proc_base_stuff; p <= last; p++) {
2947                 if (p->len != dentry->d_name.len)
2948                         continue;
2949                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2950                         break;
2951         }
2952         if (p > last)
2953                 goto out;
2954
2955         error = proc_base_instantiate(dir, dentry, task, p);
2956
2957 out:
2958         put_task_struct(task);
2959 out_no_task:
2960         return error;
2961 }
2962
2963 static int proc_base_fill_cache(struct file *filp, void *dirent,
2964         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2965 {
2966         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2967                                 proc_base_instantiate, task, p);
2968 }
2969
2970 #ifdef CONFIG_TASK_IO_ACCOUNTING
2971 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2972 {
2973         struct task_io_accounting acct = task->ioac;
2974         unsigned long flags;
2975         int result;
2976
2977         result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2978         if (result)
2979                 return result;
2980
2981         if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2982                 result = -EACCES;
2983                 goto out_unlock;
2984         }
2985
2986         if (whole && lock_task_sighand(task, &flags)) {
2987                 struct task_struct *t = task;
2988
2989                 task_io_accounting_add(&acct, &task->signal->ioac);
2990                 while_each_thread(task, t)
2991                         task_io_accounting_add(&acct, &t->ioac);
2992
2993                 unlock_task_sighand(task, &flags);
2994         }
2995         result = sprintf(buffer,
2996                         "rchar: %llu\n"
2997                         "wchar: %llu\n"
2998                         "syscr: %llu\n"
2999                         "syscw: %llu\n"
3000                         "read_bytes: %llu\n"
3001                         "write_bytes: %llu\n"
3002                         "cancelled_write_bytes: %llu\n",
3003                         (unsigned long long)acct.rchar,
3004                         (unsigned long long)acct.wchar,
3005                         (unsigned long long)acct.syscr,
3006                         (unsigned long long)acct.syscw,
3007                         (unsigned long long)acct.read_bytes,
3008                         (unsigned long long)acct.write_bytes,
3009                         (unsigned long long)acct.cancelled_write_bytes);
3010 out_unlock:
3011         mutex_unlock(&task->signal->cred_guard_mutex);
3012         return result;
3013 }
3014
3015 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3016 {
3017         return do_io_accounting(task, buffer, 0);
3018 }
3019
3020 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3021 {
3022         return do_io_accounting(task, buffer, 1);
3023 }
3024 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3025
3026 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3027                                 struct pid *pid, struct task_struct *task)
3028 {
3029         int err = lock_trace(task);
3030         if (!err) {
3031                 seq_printf(m, "%08x\n", task->personality);
3032                 unlock_trace(task);
3033         }
3034         return err;
3035 }
3036
3037 /*
3038  * Thread groups
3039  */
3040 static const struct file_operations proc_task_operations;
3041 static const struct inode_operations proc_task_inode_operations;
3042
3043 static const struct pid_entry tgid_base_stuff[] = {
3044         DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3045         DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3046 #ifdef CONFIG_CHECKPOINT_RESTORE
3047         DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3048 #endif
3049         DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3050         DIR("ns",         S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3051 #ifdef CONFIG_NET
3052         DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3053 #endif
3054         REG("environ",    S_IRUSR, proc_environ_operations),
3055         INF("auxv",       S_IRUSR, proc_pid_auxv),
3056         ONE("status",     S_IRUGO, proc_pid_status),
3057         ONE("personality", S_IRUGO, proc_pid_personality),
3058         INF("limits",     S_IRUGO, proc_pid_limits),
3059 #ifdef CONFIG_SCHED_DEBUG
3060         REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3061 #endif
3062 #ifdef CONFIG_SCHED_AUTOGROUP
3063         REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3064 #endif
3065         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3066 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3067         INF("syscall",    S_IRUGO, proc_pid_syscall),
3068 #endif
3069         INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3070         ONE("stat",       S_IRUGO, proc_tgid_stat),
3071         ONE("statm",      S_IRUGO, proc_pid_statm),
3072         REG("maps",       S_IRUGO, proc_maps_operations),
3073         REG("arm_maps",   S_IRUGO, proc_armv7_maps_operations),
3074 #ifdef CONFIG_NUMA
3075         REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
3076 #endif
3077         REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3078         LNK("cwd",        proc_cwd_link),
3079         LNK("root",       proc_root_link),
3080         LNK("exe",        proc_exe_link),
3081         REG("mounts",     S_IRUGO, proc_mounts_operations),
3082         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3083         REG("mountstats", S_IRUSR, proc_mountstats_operations),
3084 #ifdef CONFIG_PROC_PAGE_MONITOR
3085         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3086         REG("smaps",      S_IRUGO, proc_smaps_operations),
3087         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3088 #endif
3089 #ifdef CONFIG_SECURITY
3090         DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3091 #endif
3092 #ifdef CONFIG_KALLSYMS
3093         INF("wchan",      S_IRUGO, proc_pid_wchan),
3094 #endif
3095 #ifdef CONFIG_STACKTRACE
3096         ONE("stack",      S_IRUGO, proc_pid_stack),
3097 #endif
3098 #ifdef CONFIG_SCHEDSTATS
3099         INF("schedstat",  S_IRUGO, proc_pid_schedstat),
3100 #endif
3101 #ifdef CONFIG_LATENCYTOP
3102         REG("latency",  S_IRUGO, proc_lstats_operations),
3103 #endif
3104 #ifdef CONFIG_PROC_PID_CPUSET
3105         REG("cpuset",     S_IRUGO, proc_cpuset_operations),
3106 #endif
3107 #ifdef CONFIG_CGROUPS
3108         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3109 #endif
3110         INF("oom_score",  S_IRUGO, proc_oom_score),
3111         REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3112         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3113 #ifdef CONFIG_AUDITSYSCALL
3114         REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3115         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3116 #endif
3117 #ifdef CONFIG_FAULT_INJECTION
3118         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3119 #endif
3120 #ifdef CONFIG_ELF_CORE
3121         REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3122 #endif
3123 #ifdef CONFIG_TASK_IO_ACCOUNTING
3124         INF("io",       S_IRUSR, proc_tgid_io_accounting),
3125 #endif
3126 #ifdef CONFIG_HARDWALL
3127         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3128 #endif
3129 };
3130
3131 static int proc_tgid_base_readdir(struct file * filp,
3132                              void * dirent, filldir_t filldir)
3133 {
3134         return proc_pident_readdir(filp,dirent,filldir,
3135                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3136 }
3137
3138 static const struct file_operations proc_tgid_base_operations = {
3139         .read           = generic_read_dir,
3140         .readdir        = proc_tgid_base_readdir,
3141         .llseek         = default_llseek,
3142 };
3143
3144 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3145         return proc_pident_lookup(dir, dentry,
3146                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3147 }
3148
3149 static const struct inode_operations proc_tgid_base_inode_operations = {
3150         .lookup         = proc_tgid_base_lookup,
3151         .getattr        = pid_getattr,
3152         .setattr        = proc_setattr,
3153 };
3154
3155 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3156 {
3157         struct dentry *dentry, *leader, *dir;
3158         char buf[PROC_NUMBUF];
3159         struct qstr name;
3160
3161         name.name = buf;
3162         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3163         dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3164         if (dentry) {
3165                 shrink_dcache_parent(dentry);
3166                 d_drop(dentry);
3167                 dput(dentry);
3168         }
3169
3170         name.name = buf;
3171         name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3172         leader = d_hash_and_lookup(mnt->mnt_root, &name);
3173         if (!leader)
3174                 goto out;
3175
3176         name.name = "task";
3177         name.len = strlen(name.name);
3178         dir = d_hash_and_lookup(leader, &name);
3179         if (!dir)
3180                 goto out_put_leader;
3181
3182         name.name = buf;
3183         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3184         dentry = d_hash_and_lookup(dir, &name);
3185         if (dentry) {
3186                 shrink_dcache_parent(dentry);
3187                 d_drop(dentry);
3188                 dput(dentry);
3189         }
3190
3191         dput(dir);
3192 out_put_leader:
3193         dput(leader);
3194 out:
3195         return;
3196 }
3197
3198 /**
3199  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
3200  * @task: task that should be flushed.
3201  *
3202  * When flushing dentries from proc, one needs to flush them from global
3203  * proc (proc_mnt) and from all the namespaces' procs this task was seen
3204  * in. This call is supposed to do all of this job.
3205  *
3206  * Looks in the dcache for
3207  * /proc/@pid
3208  * /proc/@tgid/task/@pid
3209  * if either directory is present flushes it and all of it'ts children
3210  * from the dcache.
3211  *
3212  * It is safe and reasonable to cache /proc entries for a task until
3213  * that task exits.  After that they just clog up the dcache with
3214  * useless entries, possibly causing useful dcache entries to be
3215  * flushed instead.  This routine is proved to flush those useless
3216  * dcache entries at process exit time.
3217  *
3218  * NOTE: This routine is just an optimization so it does not guarantee
3219  *       that no dcache entries will exist at process exit time it
3220  *       just makes it very unlikely that any will persist.
3221  */
3222
3223 void proc_flush_task(struct task_struct *task)
3224 {
3225         int i;
3226         struct pid *pid, *tgid;
3227         struct upid *upid;
3228
3229         pid = task_pid(task);
3230         tgid = task_tgid(task);
3231
3232         for (i = 0; i <= pid->level; i++) {
3233                 upid = &pid->numbers[i];
3234                 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3235                                         tgid->numbers[i].nr);
3236         }
3237
3238         upid = &pid->numbers[pid->level];
3239         if (upid->nr == 1)
3240                 pid_ns_release_proc(upid->ns);
3241 }
3242
3243 static struct dentry *proc_pid_instantiate(struct inode *dir,
3244                                            struct dentry * dentry,
3245                                            struct task_struct *task, const void *ptr)
3246 {
3247         struct dentry *error = ERR_PTR(-ENOENT);
3248         struct inode *inode;
3249
3250         inode = proc_pid_make_inode(dir->i_sb, task);
3251         if (!inode)
3252                 goto out;
3253
3254         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3255         inode->i_op = &proc_tgid_base_inode_operations;
3256         inode->i_fop = &proc_tgid_base_operations;
3257         inode->i_flags|=S_IMMUTABLE;
3258
3259         set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3260                                                   ARRAY_SIZE(tgid_base_stuff)));
3261
3262         d_set_d_op(dentry, &pid_dentry_operations);
3263
3264         d_add(dentry, inode);
3265         /* Close the race of the process dying before we return the dentry */
3266         if (pid_revalidate(dentry, NULL))
3267                 error = NULL;
3268 out:
3269         return error;
3270 }
3271
3272 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3273 {
3274         struct dentry *result;
3275         struct task_struct *task;
3276         unsigned tgid;
3277         struct pid_namespace *ns;
3278
3279         result = proc_base_lookup(dir, dentry);
3280         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3281                 goto out;
3282
3283         tgid = name_to_int(dentry);
3284         if (tgid == ~0U)
3285                 goto out;
3286
3287         ns = dentry->d_sb->s_fs_info;
3288         rcu_read_lock();
3289         task = find_task_by_pid_ns(tgid, ns);
3290         if (task)
3291                 get_task_struct(task);
3292         rcu_read_unlock();
3293         if (!task)
3294                 goto out;
3295
3296         result = proc_pid_instantiate(dir, dentry, task, NULL);
3297         put_task_struct(task);
3298 out:
3299         return result;
3300 }
3301
3302 /*
3303  * Find the first task with tgid >= tgid
3304  *
3305  */
3306 struct tgid_iter {
3307         unsigned int tgid;
3308         struct task_struct *task;
3309 };
3310 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3311 {
3312         struct pid *pid;
3313
3314         if (iter.task)
3315                 put_task_struct(iter.task);
3316         rcu_read_lock();
3317 retry:
3318         iter.task = NULL;
3319         pid = find_ge_pid(iter.tgid, ns);
3320         if (pid) {
3321                 iter.tgid = pid_nr_ns(pid, ns);
3322                 iter.task = pid_task(pid, PIDTYPE_PID);
3323                 /* What we to know is if the pid we have find is the
3324                  * pid of a thread_group_leader.  Testing for task
3325                  * being a thread_group_leader is the obvious thing
3326                  * todo but there is a window when it fails, due to
3327                  * the pid transfer logic in de_thread.
3328                  *
3329                  * So we perform the straight forward test of seeing
3330                  * if the pid we have found is the pid of a thread
3331                  * group leader, and don't worry if the task we have
3332                  * found doesn't happen to be a thread group leader.
3333                  * As we don't care in the case of readdir.
3334                  */
3335                 if (!iter.task || !has_group_leader_pid(iter.task)) {
3336                         iter.tgid += 1;
3337                         goto retry;
3338                 }
3339                 get_task_struct(iter.task);
3340         }
3341         rcu_read_unlock();
3342         return iter;
3343 }
3344
3345 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3346
3347 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3348         struct tgid_iter iter)
3349 {
3350         char name[PROC_NUMBUF];
3351         int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3352         return proc_fill_cache(filp, dirent, filldir, name, len,
3353                                 proc_pid_instantiate, iter.task, NULL);
3354 }
3355
3356 /* for the /proc/ directory itself, after non-process stuff has been done */
3357 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3358 {
3359         unsigned int nr;
3360         struct task_struct *reaper;
3361         struct tgid_iter iter;
3362         struct pid_namespace *ns;
3363
3364         if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3365                 goto out_no_task;
3366         nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3367
3368         reaper = get_proc_task(filp->f_path.dentry->d_inode);
3369         if (!reaper)
3370                 goto out_no_task;
3371
3372         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3373                 const struct pid_entry *p = &proc_base_stuff[nr];
3374                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3375                         goto out;
3376         }
3377
3378         ns = filp->f_dentry->d_sb->s_fs_info;
3379         iter.task = NULL;
3380         iter.tgid = filp->f_pos - TGID_OFFSET;
3381         for (iter = next_tgid(ns, iter);
3382              iter.task;
3383              iter.tgid += 1, iter = next_tgid(ns, iter)) {
3384                 filp->f_pos = iter.tgid + TGID_OFFSET;
3385                 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3386                         put_task_struct(iter.task);
3387                         goto out;
3388                 }
3389         }
3390         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3391 out:
3392         put_task_struct(reaper);
3393 out_no_task:
3394         return 0;
3395 }
3396
3397 /*
3398  * Tasks
3399  */
3400 static const struct pid_entry tid_base_stuff[] = {
3401         DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3402         DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3403         DIR("ns",        S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3404         REG("environ",   S_IRUSR, proc_environ_operations),
3405         INF("auxv",      S_IRUSR, proc_pid_auxv),
3406         ONE("status",    S_IRUGO, proc_pid_status),
3407         ONE("personality", S_IRUGO, proc_pid_personality),
3408         INF("limits",    S_IRUGO, proc_pid_limits),
3409 #ifdef CONFIG_SCHED_DEBUG
3410         REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3411 #endif
3412         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3413 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3414         INF("syscall",   S_IRUGO, proc_pid_syscall),
3415 #endif
3416         INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3417         ONE("stat",      S_IRUGO, proc_tid_stat),
3418         ONE("statm",     S_IRUGO, proc_pid_statm),
3419         REG("maps",      S_IRUGO, proc_maps_operations),
3420         REG("arm_maps",  S_IRUGO, proc_armv7_maps_operations),
3421 #ifdef CONFIG_NUMA
3422         REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3423 #endif
3424         REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3425         LNK("cwd",       proc_cwd_link),
3426         LNK("root",      proc_root_link),
3427         LNK("exe",       proc_exe_link),
3428         REG("mounts",    S_IRUGO, proc_mounts_operations),
3429         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3430 #ifdef CONFIG_PROC_PAGE_MONITOR
3431         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3432         REG("smaps",     S_IRUGO, proc_smaps_operations),
3433         REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3434 #endif
3435 #ifdef CONFIG_SECURITY
3436         DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3437 #endif
3438 #ifdef CONFIG_KALLSYMS
3439         INF("wchan",     S_IRUGO, proc_pid_wchan),
3440 #endif
3441 #ifdef CONFIG_STACKTRACE
3442         ONE("stack",      S_IRUGO, proc_pid_stack),
3443 #endif
3444 #ifdef CONFIG_SCHEDSTATS
3445         INF("schedstat", S_IRUGO, proc_pid_schedstat),
3446 #endif
3447 #ifdef CONFIG_LATENCYTOP
3448         REG("latency",  S_IRUGO, proc_lstats_operations),
3449 #endif
3450 #ifdef CONFIG_PROC_PID_CPUSET
3451         REG("cpuset",    S_IRUGO, proc_cpuset_operations),
3452 #endif
3453 #ifdef CONFIG_CGROUPS
3454         REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3455 #endif
3456         INF("oom_score", S_IRUGO, proc_oom_score),
3457         REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3458         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3459 #ifdef CONFIG_AUDITSYSCALL
3460         REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3461         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3462 #endif
3463 #ifdef CONFIG_FAULT_INJECTION
3464         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3465 #endif
3466 #ifdef CONFIG_TASK_IO_ACCOUNTING
3467         INF("io",       S_IRUSR, proc_tid_io_accounting),
3468 #endif
3469 #ifdef CONFIG_HARDWALL
3470         INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3471 #endif
3472 };
3473
3474 static int proc_tid_base_readdir(struct file * filp,
3475                              void * dirent, filldir_t filldir)
3476 {
3477         return proc_pident_readdir(filp,dirent,filldir,
3478                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3479 }
3480
3481 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3482         return proc_pident_lookup(dir, dentry,
3483                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3484 }
3485
3486 static const struct file_operations proc_tid_base_operations = {
3487         .read           = generic_read_dir,
3488         .readdir        = proc_tid_base_readdir,
3489         .llseek         = default_llseek,
3490 };
3491
3492 static const struct inode_operations proc_tid_base_inode_operations = {
3493         .lookup         = proc_tid_base_lookup,
3494         .getattr        = pid_getattr,
3495         .setattr        = proc_setattr,
3496 };
3497
3498 static struct dentry *proc_task_instantiate(struct inode *dir,
3499         struct dentry *dentry, struct task_struct *task, const void *ptr)
3500 {
3501         struct dentry *error = ERR_PTR(-ENOENT);
3502         struct inode *inode;
3503         inode = proc_pid_make_inode(dir->i_sb, task);
3504
3505         if (!inode)
3506                 goto out;
3507         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3508         inode->i_op = &proc_tid_base_inode_operations;
3509         inode->i_fop = &proc_tid_base_operations;
3510         inode->i_flags|=S_IMMUTABLE;
3511
3512         set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3513                                                   ARRAY_SIZE(tid_base_stuff)));
3514
3515         d_set_d_op(dentry, &pid_dentry_operations);
3516
3517         d_add(dentry, inode);
3518         /* Close the race of the process dying before we return the dentry */
3519         if (pid_revalidate(dentry, NULL))
3520                 error = NULL;
3521 out:
3522         return error;
3523 }
3524
3525 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3526 {
3527         struct dentry *result = ERR_PTR(-ENOENT);
3528         struct task_struct *task;
3529         struct task_struct *leader = get_proc_task(dir);
3530         unsigned tid;
3531         struct pid_namespace *ns;
3532
3533         if (!leader)
3534                 goto out_no_task;
3535
3536         tid = name_to_int(dentry);
3537         if (tid == ~0U)
3538                 goto out;
3539
3540         ns = dentry->d_sb->s_fs_info;
3541         rcu_read_lock();
3542         task = find_task_by_pid_ns(tid, ns);
3543         if (task)
3544                 get_task_struct(task);
3545         rcu_read_unlock();
3546         if (!task)
3547                 goto out;
3548         if (!same_thread_group(leader, task))
3549                 goto out_drop_task;
3550
3551         result = proc_task_instantiate(dir, dentry, task, NULL);
3552 out_drop_task:
3553         put_task_struct(task);
3554 out:
3555         put_task_struct(leader);
3556 out_no_task:
3557         return result;
3558 }
3559
3560 /*
3561  * Find the first tid of a thread group to return to user space.
3562  *
3563  * Usually this is just the thread group leader, but if the users
3564  * buffer was too small or there was a seek into the middle of the
3565  * directory we have more work todo.
3566  *
3567  * In the case of a short read we start with find_task_by_pid.
3568  *
3569  * In the case of a seek we start with the leader and walk nr
3570  * threads past it.
3571  */
3572 static struct task_struct *first_tid(struct task_struct *leader,
3573                 int tid, int nr, struct pid_namespace *ns)
3574 {
3575         struct task_struct *pos;
3576
3577         rcu_read_lock();
3578         /* Attempt to start with the pid of a thread */
3579         if (tid && (nr > 0)) {
3580                 pos = find_task_by_pid_ns(tid, ns);
3581                 if (pos && (pos->group_leader == leader))
3582                         goto found;
3583         }
3584
3585         /* If nr exceeds the number of threads there is nothing todo */
3586         pos = NULL;
3587         if (nr && nr >= get_nr_threads(leader))
3588                 goto out;
3589
3590         /* If we haven't found our starting place yet start
3591          * with the leader and walk nr threads forward.
3592          */
3593         for (pos = leader; nr > 0; --nr) {
3594                 pos = next_thread(pos);
3595                 if (pos == leader) {
3596                         pos = NULL;
3597                         goto out;
3598                 }
3599         }
3600 found:
3601         get_task_struct(pos);
3602 out:
3603         rcu_read_unlock();
3604         return pos;
3605 }
3606
3607 /*
3608  * Find the next thread in the thread list.
3609  * Return NULL if there is an error or no next thread.
3610  *
3611  * The reference to the input task_struct is released.
3612  */
3613 static struct task_struct *next_tid(struct task_struct *start)
3614 {
3615         struct task_struct *pos = NULL;
3616         rcu_read_lock();
3617         if (pid_alive(start)) {
3618                 pos = next_thread(start);
3619                 if (thread_group_leader(pos))
3620                         pos = NULL;
3621                 else
3622                         get_task_struct(pos);
3623         }
3624         rcu_read_unlock();
3625         put_task_struct(start);
3626         return pos;
3627 }
3628
3629 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3630         struct task_struct *task, int tid)
3631 {
3632         char name[PROC_NUMBUF];
3633         int len = snprintf(name, sizeof(name), "%d", tid);
3634         return proc_fill_cache(filp, dirent, filldir, name, len,
3635                                 proc_task_instantiate, task, NULL);
3636 }
3637
3638 /* for the /proc/TGID/task/ directories */
3639 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3640 {
3641         struct dentry *dentry = filp->f_path.dentry;
3642         struct inode *inode = dentry->d_inode;
3643         struct task_struct *leader = NULL;
3644         struct task_struct *task;
3645         int retval = -ENOENT;
3646         ino_t ino;
3647         int tid;
3648         struct pid_namespace *ns;
3649
3650         task = get_proc_task(inode);
3651         if (!task)
3652                 goto out_no_task;
3653         rcu_read_lock();
3654         if (pid_alive(task)) {
3655                 leader = task->group_leader;
3656                 get_task_struct(leader);
3657         }
3658         rcu_read_unlock();
3659         put_task_struct(task);
3660         if (!leader)
3661                 goto out_no_task;
3662         retval = 0;
3663
3664         switch ((unsigned long)filp->f_pos) {
3665         case 0:
3666                 ino = inode->i_ino;
3667                 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3668                         goto out;
3669                 filp->f_pos++;
3670                 /* fall through */
3671         case 1:
3672                 ino = parent_ino(dentry);
3673                 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3674                         goto out;
3675                 filp->f_pos++;
3676                 /* fall through */
3677         }
3678
3679         /* f_version caches the tgid value that the last readdir call couldn't
3680          * return. lseek aka telldir automagically resets f_version to 0.
3681          */
3682         ns = filp->f_dentry->d_sb->s_fs_info;
3683         tid = (int)filp->f_version;
3684         filp->f_version = 0;
3685         for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3686              task;
3687              task = next_tid(task), filp->f_pos++) {
3688                 tid = task_pid_nr_ns(task, ns);
3689                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3690                         /* returning this tgid failed, save it as the first
3691                          * pid for the next readir call */
3692                         filp->f_version = (u64)tid;
3693                         put_task_struct(task);
3694                         break;
3695                 }
3696         }
3697 out:
3698         put_task_struct(leader);
3699 out_no_task:
3700         return retval;
3701 }
3702
3703 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3704 {
3705         struct inode *inode = dentry->d_inode;
3706         struct task_struct *p = get_proc_task(inode);
3707         generic_fillattr(inode, stat);
3708
3709         if (p) {
3710                 stat->nlink += get_nr_threads(p);
3711                 put_task_struct(p);
3712         }
3713
3714         return 0;
3715 }
3716
3717 static const struct inode_operations proc_task_inode_operations = {
3718         .lookup         = proc_task_lookup,
3719         .getattr        = proc_task_getattr,
3720         .setattr        = proc_setattr,
3721 };
3722
3723 static const struct file_operations proc_task_operations = {
3724         .read           = generic_read_dir,
3725         .readdir        = proc_task_readdir,
3726         .llseek         = default_llseek,
3727 };