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