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