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