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