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