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