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