vserver 1.9.3
[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, committed;
178         struct page_state ps;
179         unsigned long inactive;
180         unsigned long active;
181         unsigned long free;
182         unsigned long vmtot;
183         struct vmalloc_info vmi;
184
185         get_page_state(&ps);
186         get_zone_counts(&active, &inactive, &free);
187
188 /*
189  * display in kilobytes.
190  */
191 #define K(x) ((x) << (PAGE_SHIFT - 10))
192         si_meminfo(&i);
193         si_swapinfo(&i);
194         committed = atomic_read(&vm_committed_space);
195
196         vmtot = (VMALLOC_END-VMALLOC_START)>>10;
197         vmi = get_vmalloc_info();
198         vmi.used >>= 10;
199         vmi.largest_chunk >>= 10;
200
201         /*
202          * Tagged format, for easy grepping and expansion.
203          */
204         len = sprintf(page,
205                 "MemTotal:     %8lu kB\n"
206                 "MemFree:      %8lu kB\n"
207                 "Buffers:      %8lu kB\n"
208                 "Cached:       %8lu kB\n"
209                 "SwapCached:   %8lu kB\n"
210                 "Active:       %8lu kB\n"
211                 "Inactive:     %8lu kB\n"
212                 "HighTotal:    %8lu kB\n"
213                 "HighFree:     %8lu kB\n"
214                 "LowTotal:     %8lu kB\n"
215                 "LowFree:      %8lu kB\n"
216                 "SwapTotal:    %8lu kB\n"
217                 "SwapFree:     %8lu kB\n"
218                 "Dirty:        %8lu kB\n"
219                 "Writeback:    %8lu kB\n"
220                 "Mapped:       %8lu kB\n"
221                 "Slab:         %8lu kB\n"
222                 "Committed_AS: %8u kB\n"
223                 "PageTables:   %8lu kB\n"
224                 "VmallocTotal: %8lu kB\n"
225                 "VmallocUsed:  %8lu kB\n"
226                 "VmallocChunk: %8lu kB\n",
227                 K(i.totalram),
228                 K(i.freeram),
229                 K(i.bufferram),
230                 K(get_page_cache_size()-total_swapcache_pages-i.bufferram),
231                 K(total_swapcache_pages),
232                 K(active),
233                 K(inactive),
234                 K(i.totalhigh),
235                 K(i.freehigh),
236                 K(i.totalram-i.totalhigh),
237                 K(i.freeram-i.freehigh),
238                 K(i.totalswap),
239                 K(i.freeswap),
240                 K(ps.nr_dirty),
241                 K(ps.nr_writeback),
242                 K(ps.nr_mapped),
243                 K(ps.nr_slab),
244                 K(committed),
245                 K(ps.nr_page_table_pages),
246                 vmtot,
247                 vmi.used,
248                 vmi.largest_chunk
249                 );
250
251                 len += hugetlb_report_meminfo(page + len);
252
253         return proc_calc_metrics(page, start, off, count, eof, len);
254 #undef K
255 }
256
257 extern struct seq_operations fragmentation_op;
258 static int fragmentation_open(struct inode *inode, struct file *file)
259 {
260         (void)inode;
261         return seq_open(file, &fragmentation_op);
262 }
263
264 static struct file_operations fragmentation_file_operations = {
265         .open           = fragmentation_open,
266         .read           = seq_read,
267         .llseek         = seq_lseek,
268         .release        = seq_release,
269 };
270
271 static int version_read_proc(char *page, char **start, off_t off,
272                                  int count, int *eof, void *data)
273 {
274         extern char *linux_banner;
275         int len;
276
277         strcpy(page, linux_banner);
278         len = strlen(page);
279         return proc_calc_metrics(page, start, off, count, eof, len);
280 }
281
282 extern struct seq_operations cpuinfo_op;
283 static int cpuinfo_open(struct inode *inode, struct file *file)
284 {
285         return seq_open(file, &cpuinfo_op);
286 }
287 static struct file_operations proc_cpuinfo_operations = {
288         .open           = cpuinfo_open,
289         .read           = seq_read,
290         .llseek         = seq_lseek,
291         .release        = seq_release,
292 };
293
294 extern struct seq_operations vmstat_op;
295 static int vmstat_open(struct inode *inode, struct file *file)
296 {
297         return seq_open(file, &vmstat_op);
298 }
299 static struct file_operations proc_vmstat_file_operations = {
300         .open           = vmstat_open,
301         .read           = seq_read,
302         .llseek         = seq_lseek,
303         .release        = seq_release,
304 };
305
306 #ifdef CONFIG_PROC_HARDWARE
307 static int hardware_read_proc(char *page, char **start, off_t off,
308                                  int count, int *eof, void *data)
309 {
310         int len = get_hardware_list(page);
311         return proc_calc_metrics(page, start, off, count, eof, len);
312 }
313 #endif
314
315 #ifdef CONFIG_STRAM_PROC
316 static int stram_read_proc(char *page, char **start, off_t off,
317                                  int count, int *eof, void *data)
318 {
319         int len = get_stram_list(page);
320         return proc_calc_metrics(page, start, off, count, eof, len);
321 }
322 #endif
323
324 extern struct seq_operations partitions_op;
325 static int partitions_open(struct inode *inode, struct file *file)
326 {
327         return seq_open(file, &partitions_op);
328 }
329 static struct file_operations proc_partitions_operations = {
330         .open           = partitions_open,
331         .read           = seq_read,
332         .llseek         = seq_lseek,
333         .release        = seq_release,
334 };
335
336 extern struct seq_operations diskstats_op;
337 static int diskstats_open(struct inode *inode, struct file *file)
338 {
339         return seq_open(file, &diskstats_op);
340 }
341 static struct file_operations proc_diskstats_operations = {
342         .open           = diskstats_open,
343         .read           = seq_read,
344         .llseek         = seq_lseek,
345         .release        = seq_release,
346 };
347
348 #ifdef CONFIG_MODULES
349 extern struct seq_operations modules_op;
350 static int modules_open(struct inode *inode, struct file *file)
351 {
352         return seq_open(file, &modules_op);
353 }
354 static struct file_operations proc_modules_operations = {
355         .open           = modules_open,
356         .read           = seq_read,
357         .llseek         = seq_lseek,
358         .release        = seq_release,
359 };
360 #endif
361
362 extern struct seq_operations slabinfo_op;
363 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
364 static int slabinfo_open(struct inode *inode, struct file *file)
365 {
366         return seq_open(file, &slabinfo_op);
367 }
368 static struct file_operations proc_slabinfo_operations = {
369         .open           = slabinfo_open,
370         .read           = seq_read,
371         .write          = slabinfo_write,
372         .llseek         = seq_lseek,
373         .release        = seq_release,
374 };
375
376 int show_stat(struct seq_file *p, void *v)
377 {
378         int i;
379         extern unsigned long total_forks;
380         unsigned long jif;
381         u64     sum = 0, user = 0, nice = 0, system = 0,
382                 idle = 0, iowait = 0, irq = 0, softirq = 0;
383
384         jif = - wall_to_monotonic.tv_sec;
385         if (wall_to_monotonic.tv_nsec)
386                 --jif;
387
388         for_each_cpu(i) {
389                 int j;
390
391                 user += kstat_cpu(i).cpustat.user;
392                 nice += kstat_cpu(i).cpustat.nice;
393                 system += kstat_cpu(i).cpustat.system;
394                 idle += kstat_cpu(i).cpustat.idle;
395                 iowait += kstat_cpu(i).cpustat.iowait;
396                 irq += kstat_cpu(i).cpustat.irq;
397                 softirq += kstat_cpu(i).cpustat.softirq;
398                 for (j = 0 ; j < NR_IRQS ; j++)
399                         sum += kstat_cpu(i).irqs[j];
400         }
401
402         seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu\n",
403                 (unsigned long long)jiffies_64_to_clock_t(user),
404                 (unsigned long long)jiffies_64_to_clock_t(nice),
405                 (unsigned long long)jiffies_64_to_clock_t(system),
406                 (unsigned long long)jiffies_64_to_clock_t(idle),
407                 (unsigned long long)jiffies_64_to_clock_t(iowait),
408                 (unsigned long long)jiffies_64_to_clock_t(irq),
409                 (unsigned long long)jiffies_64_to_clock_t(softirq));
410         for_each_online_cpu(i) {
411
412                 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
413                 user = kstat_cpu(i).cpustat.user;
414                 nice = kstat_cpu(i).cpustat.nice;
415                 system = kstat_cpu(i).cpustat.system;
416                 idle = kstat_cpu(i).cpustat.idle;
417                 iowait = kstat_cpu(i).cpustat.iowait;
418                 irq = kstat_cpu(i).cpustat.irq;
419                 softirq = kstat_cpu(i).cpustat.softirq;
420                 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu\n",
421                         i,
422                         (unsigned long long)jiffies_64_to_clock_t(user),
423                         (unsigned long long)jiffies_64_to_clock_t(nice),
424                         (unsigned long long)jiffies_64_to_clock_t(system),
425                         (unsigned long long)jiffies_64_to_clock_t(idle),
426                         (unsigned long long)jiffies_64_to_clock_t(iowait),
427                         (unsigned long long)jiffies_64_to_clock_t(irq),
428                         (unsigned long long)jiffies_64_to_clock_t(softirq));
429         }
430         seq_printf(p, "intr %llu", (unsigned long long)sum);
431
432 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA)
433         for (i = 0; i < NR_IRQS; i++)
434                 seq_printf(p, " %u", kstat_irqs(i));
435 #endif
436
437         seq_printf(p,
438                 "\nctxt %llu\n"
439                 "btime %lu\n"
440                 "processes %lu\n"
441                 "procs_running %lu\n"
442                 "procs_blocked %lu\n",
443                 nr_context_switches(),
444                 (unsigned long)jif,
445                 total_forks,
446                 nr_running(),
447                 nr_iowait());
448
449         return 0;
450 }
451
452 static int stat_open(struct inode *inode, struct file *file)
453 {
454         unsigned size = 4096 * (1 + num_possible_cpus() / 32);
455         char *buf;
456         struct seq_file *m;
457         int res;
458
459         /* don't ask for more than the kmalloc() max size, currently 128 KB */
460         if (size > 128 * 1024)
461                 size = 128 * 1024;
462         buf = kmalloc(size, GFP_KERNEL);
463         if (!buf)
464                 return -ENOMEM;
465
466         res = single_open(file, show_stat, NULL);
467         if (!res) {
468                 m = file->private_data;
469                 m->buf = buf;
470                 m->size = size;
471         } else
472                 kfree(buf);
473         return res;
474 }
475 static struct file_operations proc_stat_operations = {
476         .open           = stat_open,
477         .read           = seq_read,
478         .llseek         = seq_lseek,
479         .release        = single_release,
480 };
481
482 static int devices_read_proc(char *page, char **start, off_t off,
483                                  int count, int *eof, void *data)
484 {
485         int len = get_chrdev_list(page);
486         len += get_blkdev_list(page+len);
487         return proc_calc_metrics(page, start, off, count, eof, len);
488 }
489
490 /*
491  * /proc/interrupts
492  */
493 static void *int_seq_start(struct seq_file *f, loff_t *pos)
494 {
495         return (*pos <= NR_IRQS) ? pos : NULL;
496 }
497
498 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
499 {
500         (*pos)++;
501         if (*pos > NR_IRQS)
502                 return NULL;
503         return pos;
504 }
505
506 static void int_seq_stop(struct seq_file *f, void *v)
507 {
508         /* Nothing to do */
509 }
510
511
512 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
513 static struct seq_operations int_seq_ops = {
514         .start = int_seq_start,
515         .next  = int_seq_next,
516         .stop  = int_seq_stop,
517         .show  = show_interrupts
518 };
519
520 int interrupts_open(struct inode *inode, struct file *filp)
521 {
522         return seq_open(filp, &int_seq_ops);
523 }
524
525 static struct file_operations proc_interrupts_operations = {
526         .open           = interrupts_open,
527         .read           = seq_read,
528         .llseek         = seq_lseek,
529         .release        = seq_release,
530 };
531
532 static int filesystems_read_proc(char *page, char **start, off_t off,
533                                  int count, int *eof, void *data)
534 {
535         int len = get_filesystem_list(page);
536         return proc_calc_metrics(page, start, off, count, eof, len);
537 }
538
539 static int cmdline_read_proc(char *page, char **start, off_t off,
540                                  int count, int *eof, void *data)
541 {
542         int len;
543
544         len = sprintf(page, "%s\n", saved_command_line);
545         return proc_calc_metrics(page, start, off, count, eof, len);
546 }
547
548 static int locks_read_proc(char *page, char **start, off_t off,
549                                  int count, int *eof, void *data)
550 {
551         int len = get_locks_status(page, start, off, count);
552
553         if (len < count)
554                 *eof = 1;
555         return len;
556 }
557
558 static int execdomains_read_proc(char *page, char **start, off_t off,
559                                  int count, int *eof, void *data)
560 {
561         int len = get_exec_domain_list(page);
562         return proc_calc_metrics(page, start, off, count, eof, len);
563 }
564
565 #ifdef CONFIG_MAGIC_SYSRQ
566 /*
567  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
568  */
569 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
570                                    size_t count, loff_t *ppos)
571 {
572         if (count) {
573                 char c;
574
575                 if (get_user(c, buf))
576                         return -EFAULT;
577                 __handle_sysrq(c, NULL, NULL);
578         }
579         return count;
580 }
581
582 static struct file_operations proc_sysrq_trigger_operations = {
583         .write          = write_sysrq_trigger,
584 };
585 #endif
586
587 struct proc_dir_entry *proc_root_kcore;
588
589 static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
590 {
591         struct proc_dir_entry *entry;
592         entry = create_proc_entry(name, mode, NULL);
593         if (entry)
594                 entry->proc_fops = f;
595 }
596
597 void __init proc_misc_init(void)
598 {
599         struct proc_dir_entry *entry;
600         static struct {
601                 char *name;
602                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
603         } *p, simple_ones[] = {
604                 {"loadavg",     loadavg_read_proc},
605                 {"uptime",      uptime_read_proc},
606                 {"meminfo",     meminfo_read_proc},
607                 {"version",     version_read_proc},
608 #ifdef CONFIG_PROC_HARDWARE
609                 {"hardware",    hardware_read_proc},
610 #endif
611 #ifdef CONFIG_STRAM_PROC
612                 {"stram",       stram_read_proc},
613 #endif
614                 {"devices",     devices_read_proc},
615                 {"filesystems", filesystems_read_proc},
616                 {"cmdline",     cmdline_read_proc},
617                 {"locks",       locks_read_proc},
618                 {"execdomains", execdomains_read_proc},
619                 {NULL,}
620         };
621         for (p = simple_ones; p->name; p++)
622                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
623
624         proc_symlink("mounts", NULL, "self/mounts");
625
626         /* And now for trickier ones */
627         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
628         if (entry)
629                 entry->proc_fops = &proc_kmsg_operations;
630         create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
631         create_seq_entry("partitions", 0, &proc_partitions_operations);
632         create_seq_entry("stat", 0, &proc_stat_operations);
633         create_seq_entry("interrupts", 0, &proc_interrupts_operations);
634         create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
635         create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
636         create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
637         create_seq_entry("diskstats", 0, &proc_diskstats_operations);
638 #ifdef CONFIG_MODULES
639         create_seq_entry("modules", 0, &proc_modules_operations);
640 #endif
641 #ifdef CONFIG_SCHEDSTATS
642         create_seq_entry("schedstat", 0, &proc_schedstat_operations);
643 #endif
644 #ifdef CONFIG_PROC_KCORE
645         proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
646         if (proc_root_kcore) {
647                 proc_root_kcore->proc_fops = &proc_kcore_operations;
648                 proc_root_kcore->size =
649                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
650         }
651 #endif
652 #ifdef CONFIG_MAGIC_SYSRQ
653         entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
654         if (entry)
655                 entry->proc_fops = &proc_sysrq_trigger_operations;
656 #endif
657 #ifdef CONFIG_PPC32
658         {
659                 extern struct file_operations ppc_htab_operations;
660                 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
661                 if (entry)
662                         entry->proc_fops = &ppc_htab_operations;
663         }
664 #endif
665 }