Merge with rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / fs / proc / proc_misc.c
1 /*
2  *  linux/fs/proc/proc_misc.c
3  *
4  *  linux/fs/proc/array.c
5  *  Copyright (C) 1992  by Linus Torvalds
6  *  based on ideas by Darren Senn
7  *
8  *  This used to be the part of array.c. See the rest of history and credits
9  *  there. I took this into a separate file and switched the thing to generic
10  *  proc_file_inode_operations, leaving in array.c only per-process stuff.
11  *  Inumbers allocation made dynamic (via create_proc_entry()).  AV, May 1999.
12  *
13  * Changes:
14  * Fulton Green      :  Encapsulated position metric calculations.
15  *                      <kernel@FultonGreen.com>
16  */
17
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/tty.h>
24 #include <linux/string.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/ioport.h>
28 #include <linux/config.h>
29 #include <linux/mm.h>
30 #include <linux/mmzone.h>
31 #include <linux/pagemap.h>
32 #include <linux/swap.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/signal.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/smp_lock.h>
39 #include <linux/seq_file.h>
40 #include <linux/times.h>
41 #include <linux/profile.h>
42 #include <linux/blkdev.h>
43 #include <linux/hugetlb.h>
44 #include <linux/jiffies.h>
45 #include <linux/sysrq.h>
46 #include <linux/vmalloc.h>
47 #include <asm/uaccess.h>
48 #include <asm/pgtable.h>
49 #include <asm/io.h>
50 #include <asm/tlb.h>
51 #include <asm/div64.h>
52 #include "internal.h"
53
54 #define LOAD_INT(x) ((x) >> FSHIFT)
55 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
56 /*
57  * Warning: stuff below (imported functions) assumes that its output will fit
58  * into one page. For some of those functions it may be wrong. Moreover, we
59  * have a way to deal with that gracefully. Right now I used straightforward
60  * wrappers, but this needs further analysis wrt potential overflows.
61  */
62 extern int get_hardware_list(char *);
63 extern int get_stram_list(char *);
64 extern int get_chrdev_list(char *);
65 extern int get_filesystem_list(char *);
66 extern int get_exec_domain_list(char *);
67 extern int get_dma_list(char *);
68 extern int get_locks_status (char *, char **, off_t, int);
69
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71                                  int count, int *eof, int len)
72 {
73         if (len <= off+count) *eof = 1;
74         *start = page + off;
75         len -= off;
76         if (len>count) len = count;
77         if (len<0) len = 0;
78         return len;
79 }
80
81 static int loadavg_read_proc(char *page, char **start, off_t off,
82                                  int count, int *eof, void *data)
83 {
84         int a, b, c;
85         int len;
86
87         a = avenrun[0] + (FIXED_1/200);
88         b = avenrun[1] + (FIXED_1/200);
89         c = avenrun[2] + (FIXED_1/200);
90         len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
91                 LOAD_INT(a), LOAD_FRAC(a),
92                 LOAD_INT(b), LOAD_FRAC(b),
93                 LOAD_INT(c), LOAD_FRAC(c),
94                 nr_running(), nr_threads, last_pid);
95         return proc_calc_metrics(page, start, off, count, eof, len);
96 }
97
98 static int uptime_read_proc(char *page, char **start, off_t off,
99                                  int count, int *eof, void *data)
100 {
101         struct timespec uptime;
102         struct timespec idle;
103         int len;
104         cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
105
106         do_posix_clock_monotonic_gettime(&uptime);
107         cputime_to_timespec(idletime, &idle);
108         len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
109                         (unsigned long) uptime.tv_sec,
110                         (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
111                         (unsigned long) idle.tv_sec,
112                         (idle.tv_nsec / (NSEC_PER_SEC / 100)));
113
114         return proc_calc_metrics(page, start, off, count, eof, len);
115 }
116
117 static int meminfo_read_proc(char *page, char **start, off_t off,
118                                  int count, int *eof, void *data)
119 {
120         struct sysinfo i;
121         int len;
122         struct page_state ps;
123         unsigned long inactive;
124         unsigned long active;
125         unsigned long free;
126         unsigned long committed;
127         unsigned long allowed;
128         struct vmalloc_info vmi;
129         long cached;
130
131         get_page_state(&ps);
132         get_zone_counts(&active, &inactive, &free);
133
134 /*
135  * display in kilobytes.
136  */
137 #define K(x) ((x) << (PAGE_SHIFT - 10))
138         si_meminfo(&i);
139         si_swapinfo(&i);
140         committed = atomic_read(&vm_committed_space);
141         allowed = ((totalram_pages - hugetlb_total_pages())
142                 * sysctl_overcommit_ratio / 100) + total_swap_pages;
143
144         cached = get_page_cache_size() - total_swapcache_pages - i.bufferram;
145         if (cached < 0)
146                 cached = 0;
147
148         get_vmalloc_info(&vmi);
149
150         /*
151          * Tagged format, for easy grepping and expansion.
152          */
153         len = sprintf(page,
154                 "MemTotal:     %8lu kB\n"
155                 "MemFree:      %8lu kB\n"
156                 "Buffers:      %8lu kB\n"
157                 "Cached:       %8lu kB\n"
158                 "SwapCached:   %8lu kB\n"
159                 "Active:       %8lu kB\n"
160                 "Inactive:     %8lu kB\n"
161                 "HighTotal:    %8lu kB\n"
162                 "HighFree:     %8lu kB\n"
163                 "LowTotal:     %8lu kB\n"
164                 "LowFree:      %8lu kB\n"
165                 "SwapTotal:    %8lu kB\n"
166                 "SwapFree:     %8lu kB\n"
167                 "Dirty:        %8lu kB\n"
168                 "Writeback:    %8lu kB\n"
169                 "Mapped:       %8lu kB\n"
170                 "Slab:         %8lu kB\n"
171                 "CommitLimit:  %8lu kB\n"
172                 "Committed_AS: %8lu kB\n"
173                 "PageTables:   %8lu kB\n"
174                 "VmallocTotal: %8lu kB\n"
175                 "VmallocUsed:  %8lu kB\n"
176                 "VmallocChunk: %8lu kB\n",
177                 K(i.totalram),
178                 K(i.freeram),
179                 K(i.bufferram),
180                 K(cached),
181                 K(total_swapcache_pages),
182                 K(active),
183                 K(inactive),
184                 K(i.totalhigh),
185                 K(i.freehigh),
186                 K(i.totalram-i.totalhigh),
187                 K(i.freeram-i.freehigh),
188                 K(i.totalswap),
189                 K(i.freeswap),
190                 K(ps.nr_dirty),
191                 K(ps.nr_writeback),
192                 K(ps.nr_mapped),
193                 K(ps.nr_slab),
194                 K(allowed),
195                 K(committed),
196                 K(ps.nr_page_table_pages),
197                 (unsigned long)VMALLOC_TOTAL >> 10,
198                 vmi.used >> 10,
199                 vmi.largest_chunk >> 10
200                 );
201
202                 len += hugetlb_report_meminfo(page + len);
203
204         return proc_calc_metrics(page, start, off, count, eof, len);
205 #undef K
206 }
207
208 extern struct seq_operations fragmentation_op;
209 static int fragmentation_open(struct inode *inode, struct file *file)
210 {
211         (void)inode;
212         return seq_open(file, &fragmentation_op);
213 }
214
215 static struct file_operations fragmentation_file_operations = {
216         .open           = fragmentation_open,
217         .read           = seq_read,
218         .llseek         = seq_lseek,
219         .release        = seq_release,
220 };
221
222 extern struct seq_operations zoneinfo_op;
223 static int zoneinfo_open(struct inode *inode, struct file *file)
224 {
225         return seq_open(file, &zoneinfo_op);
226 }
227
228 static struct file_operations proc_zoneinfo_file_operations = {
229         .open           = zoneinfo_open,
230         .read           = seq_read,
231         .llseek         = seq_lseek,
232         .release        = seq_release,
233 };
234
235 static int version_read_proc(char *page, char **start, off_t off,
236                                  int count, int *eof, void *data)
237 {
238         int len;
239
240         strcpy(page, linux_banner);
241         len = strlen(page);
242         return proc_calc_metrics(page, start, off, count, eof, len);
243 }
244
245 extern struct seq_operations cpuinfo_op;
246 static int cpuinfo_open(struct inode *inode, struct file *file)
247 {
248         return seq_open(file, &cpuinfo_op);
249 }
250 static struct file_operations proc_cpuinfo_operations = {
251         .open           = cpuinfo_open,
252         .read           = seq_read,
253         .llseek         = seq_lseek,
254         .release        = seq_release,
255 };
256
257 extern struct seq_operations vmstat_op;
258 static int vmstat_open(struct inode *inode, struct file *file)
259 {
260         return seq_open(file, &vmstat_op);
261 }
262 static struct file_operations proc_vmstat_file_operations = {
263         .open           = vmstat_open,
264         .read           = seq_read,
265         .llseek         = seq_lseek,
266         .release        = seq_release,
267 };
268
269 #ifdef CONFIG_PROC_HARDWARE
270 static int hardware_read_proc(char *page, char **start, off_t off,
271                                  int count, int *eof, void *data)
272 {
273         int len = get_hardware_list(page);
274         return proc_calc_metrics(page, start, off, count, eof, len);
275 }
276 #endif
277
278 #ifdef CONFIG_STRAM_PROC
279 static int stram_read_proc(char *page, char **start, off_t off,
280                                  int count, int *eof, void *data)
281 {
282         int len = get_stram_list(page);
283         return proc_calc_metrics(page, start, off, count, eof, len);
284 }
285 #endif
286
287 extern struct seq_operations partitions_op;
288 static int partitions_open(struct inode *inode, struct file *file)
289 {
290         return seq_open(file, &partitions_op);
291 }
292 static struct file_operations proc_partitions_operations = {
293         .open           = partitions_open,
294         .read           = seq_read,
295         .llseek         = seq_lseek,
296         .release        = seq_release,
297 };
298
299 extern struct seq_operations diskstats_op;
300 static int diskstats_open(struct inode *inode, struct file *file)
301 {
302         return seq_open(file, &diskstats_op);
303 }
304 static struct file_operations proc_diskstats_operations = {
305         .open           = diskstats_open,
306         .read           = seq_read,
307         .llseek         = seq_lseek,
308         .release        = seq_release,
309 };
310
311 #ifdef CONFIG_MODULES
312 extern struct seq_operations modules_op;
313 static int modules_open(struct inode *inode, struct file *file)
314 {
315         return seq_open(file, &modules_op);
316 }
317 static struct file_operations proc_modules_operations = {
318         .open           = modules_open,
319         .read           = seq_read,
320         .llseek         = seq_lseek,
321         .release        = seq_release,
322 };
323 #endif
324
325 extern struct seq_operations slabinfo_op;
326 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
327 static int slabinfo_open(struct inode *inode, struct file *file)
328 {
329         return seq_open(file, &slabinfo_op);
330 }
331 static struct file_operations proc_slabinfo_operations = {
332         .open           = slabinfo_open,
333         .read           = seq_read,
334         .write          = slabinfo_write,
335         .llseek         = seq_lseek,
336         .release        = seq_release,
337 };
338
339 static int show_stat(struct seq_file *p, void *v)
340 {
341         int i;
342         unsigned long jif;
343         cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
344         u64 sum = 0;
345
346         user = nice = system = idle = iowait =
347                 irq = softirq = steal = cputime64_zero;
348         jif = - wall_to_monotonic.tv_sec;
349         if (wall_to_monotonic.tv_nsec)
350                 --jif;
351
352         for_each_cpu(i) {
353                 int j;
354
355                 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
356                 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
357                 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
358                 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
359                 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
360                 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
361                 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
362                 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
363                 for (j = 0 ; j < NR_IRQS ; j++)
364                         sum += kstat_cpu(i).irqs[j];
365         }
366
367         seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu %llu\n",
368                 (unsigned long long)cputime64_to_clock_t(user),
369                 (unsigned long long)cputime64_to_clock_t(nice),
370                 (unsigned long long)cputime64_to_clock_t(system),
371                 (unsigned long long)cputime64_to_clock_t(idle),
372                 (unsigned long long)cputime64_to_clock_t(iowait),
373                 (unsigned long long)cputime64_to_clock_t(irq),
374                 (unsigned long long)cputime64_to_clock_t(softirq),
375                 (unsigned long long)cputime64_to_clock_t(steal));
376         for_each_online_cpu(i) {
377
378                 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
379                 user = kstat_cpu(i).cpustat.user;
380                 nice = kstat_cpu(i).cpustat.nice;
381                 system = kstat_cpu(i).cpustat.system;
382                 idle = kstat_cpu(i).cpustat.idle;
383                 iowait = kstat_cpu(i).cpustat.iowait;
384                 irq = kstat_cpu(i).cpustat.irq;
385                 softirq = kstat_cpu(i).cpustat.softirq;
386                 steal = kstat_cpu(i).cpustat.steal;
387                 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
388                         i,
389                         (unsigned long long)cputime64_to_clock_t(user),
390                         (unsigned long long)cputime64_to_clock_t(nice),
391                         (unsigned long long)cputime64_to_clock_t(system),
392                         (unsigned long long)cputime64_to_clock_t(idle),
393                         (unsigned long long)cputime64_to_clock_t(iowait),
394                         (unsigned long long)cputime64_to_clock_t(irq),
395                         (unsigned long long)cputime64_to_clock_t(softirq),
396                         (unsigned long long)cputime64_to_clock_t(steal));
397         }
398         seq_printf(p, "intr %llu", (unsigned long long)sum);
399
400 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA)
401         for (i = 0; i < NR_IRQS; i++)
402                 seq_printf(p, " %u", kstat_irqs(i));
403 #endif
404
405         seq_printf(p,
406                 "\nctxt %llu\n"
407                 "btime %lu\n"
408                 "processes %lu\n"
409                 "procs_running %lu\n"
410                 "procs_blocked %lu\n",
411                 nr_context_switches(),
412                 (unsigned long)jif,
413                 total_forks,
414                 nr_running(),
415                 nr_iowait());
416
417         return 0;
418 }
419
420 static int stat_open(struct inode *inode, struct file *file)
421 {
422         unsigned size = 4096 * (1 + num_possible_cpus() / 32);
423         char *buf;
424         struct seq_file *m;
425         int res;
426
427         /* don't ask for more than the kmalloc() max size, currently 128 KB */
428         if (size > 128 * 1024)
429                 size = 128 * 1024;
430         buf = kmalloc(size, GFP_KERNEL);
431         if (!buf)
432                 return -ENOMEM;
433
434         res = single_open(file, show_stat, NULL);
435         if (!res) {
436                 m = file->private_data;
437                 m->buf = buf;
438                 m->size = size;
439         } else
440                 kfree(buf);
441         return res;
442 }
443 static struct file_operations proc_stat_operations = {
444         .open           = stat_open,
445         .read           = seq_read,
446         .llseek         = seq_lseek,
447         .release        = single_release,
448 };
449
450 static int devices_read_proc(char *page, char **start, off_t off,
451                                  int count, int *eof, void *data)
452 {
453         int len = get_chrdev_list(page);
454         len += get_blkdev_list(page+len, len);
455         return proc_calc_metrics(page, start, off, count, eof, len);
456 }
457
458 /*
459  * /proc/interrupts
460  */
461 static void *int_seq_start(struct seq_file *f, loff_t *pos)
462 {
463         return (*pos <= NR_IRQS) ? pos : NULL;
464 }
465
466 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
467 {
468         (*pos)++;
469         if (*pos > NR_IRQS)
470                 return NULL;
471         return pos;
472 }
473
474 static void int_seq_stop(struct seq_file *f, void *v)
475 {
476         /* Nothing to do */
477 }
478
479
480 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
481 static struct seq_operations int_seq_ops = {
482         .start = int_seq_start,
483         .next  = int_seq_next,
484         .stop  = int_seq_stop,
485         .show  = show_interrupts
486 };
487
488 static int interrupts_open(struct inode *inode, struct file *filp)
489 {
490         return seq_open(filp, &int_seq_ops);
491 }
492
493 static struct file_operations proc_interrupts_operations = {
494         .open           = interrupts_open,
495         .read           = seq_read,
496         .llseek         = seq_lseek,
497         .release        = seq_release,
498 };
499
500 static int filesystems_read_proc(char *page, char **start, off_t off,
501                                  int count, int *eof, void *data)
502 {
503         int len = get_filesystem_list(page);
504         return proc_calc_metrics(page, start, off, count, eof, len);
505 }
506
507 static int cmdline_read_proc(char *page, char **start, off_t off,
508                                  int count, int *eof, void *data)
509 {
510         int len;
511
512         len = sprintf(page, "%s\n", saved_command_line);
513         return proc_calc_metrics(page, start, off, count, eof, len);
514 }
515
516 static int locks_read_proc(char *page, char **start, off_t off,
517                                  int count, int *eof, void *data)
518 {
519         int len = get_locks_status(page, start, off, count);
520
521         if (len < count)
522                 *eof = 1;
523         return len;
524 }
525
526 static int execdomains_read_proc(char *page, char **start, off_t off,
527                                  int count, int *eof, void *data)
528 {
529         int len = get_exec_domain_list(page);
530         return proc_calc_metrics(page, start, off, count, eof, len);
531 }
532
533 #ifdef CONFIG_MAGIC_SYSRQ
534 /*
535  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
536  */
537 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
538                                    size_t count, loff_t *ppos)
539 {
540         if (count) {
541                 char c;
542
543                 if (get_user(c, buf))
544                         return -EFAULT;
545                 __handle_sysrq(c, NULL, NULL, 0);
546         }
547         return count;
548 }
549
550 static struct file_operations proc_sysrq_trigger_operations = {
551         .write          = write_sysrq_trigger,
552 };
553 #endif
554
555 struct proc_dir_entry *proc_root_kcore;
556
557 void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
558 {
559         struct proc_dir_entry *entry;
560         entry = create_proc_entry(name, mode, NULL);
561         if (entry)
562                 entry->proc_fops = f;
563 }
564
565 void __init proc_misc_init(void)
566 {
567         struct proc_dir_entry *entry;
568         static struct {
569                 char *name;
570                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
571         } *p, simple_ones[] = {
572                 {"loadavg",     loadavg_read_proc},
573                 {"uptime",      uptime_read_proc},
574                 {"meminfo",     meminfo_read_proc},
575                 {"version",     version_read_proc},
576 #ifdef CONFIG_PROC_HARDWARE
577                 {"hardware",    hardware_read_proc},
578 #endif
579 #ifdef CONFIG_STRAM_PROC
580                 {"stram",       stram_read_proc},
581 #endif
582                 {"devices",     devices_read_proc},
583                 {"filesystems", filesystems_read_proc},
584                 {"cmdline",     cmdline_read_proc},
585                 {"locks",       locks_read_proc},
586                 {"execdomains", execdomains_read_proc},
587                 {NULL,}
588         };
589         for (p = simple_ones; p->name; p++)
590                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
591
592         proc_symlink("mounts", NULL, "self/mounts");
593
594         /* And now for trickier ones */
595         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
596         if (entry)
597                 entry->proc_fops = &proc_kmsg_operations;
598         create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
599         create_seq_entry("partitions", 0, &proc_partitions_operations);
600         create_seq_entry("stat", 0, &proc_stat_operations);
601         create_seq_entry("interrupts", 0, &proc_interrupts_operations);
602         create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
603         create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
604         create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
605         create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
606         create_seq_entry("diskstats", 0, &proc_diskstats_operations);
607 #ifdef CONFIG_MODULES
608         create_seq_entry("modules", 0, &proc_modules_operations);
609 #endif
610 #ifdef CONFIG_SCHEDSTATS
611         create_seq_entry("schedstat", 0, &proc_schedstat_operations);
612 #endif
613 #ifdef CONFIG_PROC_KCORE
614         proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
615         if (proc_root_kcore) {
616                 proc_root_kcore->proc_fops = &proc_kcore_operations;
617                 proc_root_kcore->size =
618                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
619         }
620 #endif
621 #ifdef CONFIG_MAGIC_SYSRQ
622         entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
623         if (entry)
624                 entry->proc_fops = &proc_sysrq_trigger_operations;
625 #endif
626 #ifdef CONFIG_PPC32
627         {
628                 extern struct file_operations ppc_htab_operations;
629                 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
630                 if (entry)
631                         entry->proc_fops = &ppc_htab_operations;
632         }
633 #endif
634 }