proc: switch /proc/version to seq_file
[linux-2.6/btrfs-unstable.git] / fs / proc / proc_misc.c
blob4814b111d836ee382e28ea8d45635c99acfd0b2f
1 /*
2 * linux/fs/proc/proc_misc.c
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
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.
13 * Changes:
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
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/fs.h>
24 #include <linux/tty.h>
25 #include <linux/string.h>
26 #include <linux/mman.h>
27 #include <linux/quicklist.h>
28 #include <linux/proc_fs.h>
29 #include <linux/ioport.h>
30 #include <linux/mm.h>
31 #include <linux/mmzone.h>
32 #include <linux/pagemap.h>
33 #include <linux/irq.h>
34 #include <linux/interrupt.h>
35 #include <linux/swap.h>
36 #include <linux/slab.h>
37 #include <linux/genhd.h>
38 #include <linux/smp.h>
39 #include <linux/signal.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/seq_file.h>
43 #include <linux/times.h>
44 #include <linux/profile.h>
45 #include <linux/utsname.h>
46 #include <linux/blkdev.h>
47 #include <linux/hugetlb.h>
48 #include <linux/jiffies.h>
49 #include <linux/vmalloc.h>
50 #include <linux/crash_dump.h>
51 #include <linux/pid_namespace.h>
52 #include <linux/bootmem.h>
53 #include <asm/uaccess.h>
54 #include <asm/pgtable.h>
55 #include <asm/io.h>
56 #include <asm/tlb.h>
57 #include <asm/div64.h>
58 #include "internal.h"
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.
66 extern int get_hardware_list(char *);
67 extern int get_stram_list(char *);
68 extern int get_exec_domain_list(char *);
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71 int count, int *eof, int len)
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;
81 static int fragmentation_open(struct inode *inode, struct file *file)
83 (void)inode;
84 return seq_open(file, &fragmentation_op);
87 static const struct file_operations fragmentation_file_operations = {
88 .open = fragmentation_open,
89 .read = seq_read,
90 .llseek = seq_lseek,
91 .release = seq_release,
94 static int pagetypeinfo_open(struct inode *inode, struct file *file)
96 return seq_open(file, &pagetypeinfo_op);
99 static const struct file_operations pagetypeinfo_file_ops = {
100 .open = pagetypeinfo_open,
101 .read = seq_read,
102 .llseek = seq_lseek,
103 .release = seq_release,
106 static int zoneinfo_open(struct inode *inode, struct file *file)
108 return seq_open(file, &zoneinfo_op);
111 static const struct file_operations proc_zoneinfo_file_operations = {
112 .open = zoneinfo_open,
113 .read = seq_read,
114 .llseek = seq_lseek,
115 .release = seq_release,
118 extern const struct seq_operations cpuinfo_op;
119 static int cpuinfo_open(struct inode *inode, struct file *file)
121 return seq_open(file, &cpuinfo_op);
124 static const struct file_operations proc_cpuinfo_operations = {
125 .open = cpuinfo_open,
126 .read = seq_read,
127 .llseek = seq_lseek,
128 .release = seq_release,
131 static int devinfo_show(struct seq_file *f, void *v)
133 int i = *(loff_t *) v;
135 if (i < CHRDEV_MAJOR_HASH_SIZE) {
136 if (i == 0)
137 seq_printf(f, "Character devices:\n");
138 chrdev_show(f, i);
140 #ifdef CONFIG_BLOCK
141 else {
142 i -= CHRDEV_MAJOR_HASH_SIZE;
143 if (i == 0)
144 seq_printf(f, "\nBlock devices:\n");
145 blkdev_show(f, i);
147 #endif
148 return 0;
151 static void *devinfo_start(struct seq_file *f, loff_t *pos)
153 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
154 return pos;
155 return NULL;
158 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
160 (*pos)++;
161 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
162 return NULL;
163 return pos;
166 static void devinfo_stop(struct seq_file *f, void *v)
168 /* Nothing to do */
171 static const struct seq_operations devinfo_ops = {
172 .start = devinfo_start,
173 .next = devinfo_next,
174 .stop = devinfo_stop,
175 .show = devinfo_show
178 static int devinfo_open(struct inode *inode, struct file *filp)
180 return seq_open(filp, &devinfo_ops);
183 static const struct file_operations proc_devinfo_operations = {
184 .open = devinfo_open,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = seq_release,
190 static int vmstat_open(struct inode *inode, struct file *file)
192 return seq_open(file, &vmstat_op);
194 static const struct file_operations proc_vmstat_file_operations = {
195 .open = vmstat_open,
196 .read = seq_read,
197 .llseek = seq_lseek,
198 .release = seq_release,
201 #ifdef CONFIG_PROC_HARDWARE
202 static int hardware_read_proc(char *page, char **start, off_t off,
203 int count, int *eof, void *data)
205 int len = get_hardware_list(page);
206 return proc_calc_metrics(page, start, off, count, eof, len);
208 #endif
210 #ifdef CONFIG_STRAM_PROC
211 static int stram_read_proc(char *page, char **start, off_t off,
212 int count, int *eof, void *data)
214 int len = get_stram_list(page);
215 return proc_calc_metrics(page, start, off, count, eof, len);
217 #endif
219 #ifdef CONFIG_BLOCK
220 static int partitions_open(struct inode *inode, struct file *file)
222 return seq_open(file, &partitions_op);
224 static const struct file_operations proc_partitions_operations = {
225 .open = partitions_open,
226 .read = seq_read,
227 .llseek = seq_lseek,
228 .release = seq_release,
231 static int diskstats_open(struct inode *inode, struct file *file)
233 return seq_open(file, &diskstats_op);
235 static const struct file_operations proc_diskstats_operations = {
236 .open = diskstats_open,
237 .read = seq_read,
238 .llseek = seq_lseek,
239 .release = seq_release,
241 #endif
243 #ifdef CONFIG_MODULES
244 extern const struct seq_operations modules_op;
245 static int modules_open(struct inode *inode, struct file *file)
247 return seq_open(file, &modules_op);
249 static const struct file_operations proc_modules_operations = {
250 .open = modules_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
253 .release = seq_release,
255 #endif
257 #ifdef CONFIG_SLABINFO
258 static int slabinfo_open(struct inode *inode, struct file *file)
260 return seq_open(file, &slabinfo_op);
262 static const struct file_operations proc_slabinfo_operations = {
263 .open = slabinfo_open,
264 .read = seq_read,
265 .write = slabinfo_write,
266 .llseek = seq_lseek,
267 .release = seq_release,
270 #ifdef CONFIG_DEBUG_SLAB_LEAK
271 extern const struct seq_operations slabstats_op;
272 static int slabstats_open(struct inode *inode, struct file *file)
274 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
275 int ret = -ENOMEM;
276 if (n) {
277 ret = seq_open(file, &slabstats_op);
278 if (!ret) {
279 struct seq_file *m = file->private_data;
280 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
281 m->private = n;
282 n = NULL;
284 kfree(n);
286 return ret;
289 static const struct file_operations proc_slabstats_operations = {
290 .open = slabstats_open,
291 .read = seq_read,
292 .llseek = seq_lseek,
293 .release = seq_release_private,
295 #endif
296 #endif
298 #ifdef CONFIG_MMU
299 static int vmalloc_open(struct inode *inode, struct file *file)
301 unsigned int *ptr = NULL;
302 int ret;
304 if (NUMA_BUILD)
305 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
306 ret = seq_open(file, &vmalloc_op);
307 if (!ret) {
308 struct seq_file *m = file->private_data;
309 m->private = ptr;
310 } else
311 kfree(ptr);
312 return ret;
315 static const struct file_operations proc_vmalloc_operations = {
316 .open = vmalloc_open,
317 .read = seq_read,
318 .llseek = seq_lseek,
319 .release = seq_release_private,
321 #endif
323 #ifndef arch_irq_stat_cpu
324 #define arch_irq_stat_cpu(cpu) 0
325 #endif
326 #ifndef arch_irq_stat
327 #define arch_irq_stat() 0
328 #endif
330 static int show_stat(struct seq_file *p, void *v)
332 int i, j;
333 unsigned long jif;
334 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
335 cputime64_t guest;
336 u64 sum = 0;
337 struct timespec boottime;
338 unsigned int per_irq_sum;
340 user = nice = system = idle = iowait =
341 irq = softirq = steal = cputime64_zero;
342 guest = cputime64_zero;
343 getboottime(&boottime);
344 jif = boottime.tv_sec;
346 for_each_possible_cpu(i) {
347 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
348 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
349 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
350 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
351 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
352 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
353 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
354 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
355 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
357 for_each_irq_nr(j)
358 sum += kstat_irqs_cpu(j, i);
360 sum += arch_irq_stat_cpu(i);
362 sum += arch_irq_stat();
364 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
365 (unsigned long long)cputime64_to_clock_t(user),
366 (unsigned long long)cputime64_to_clock_t(nice),
367 (unsigned long long)cputime64_to_clock_t(system),
368 (unsigned long long)cputime64_to_clock_t(idle),
369 (unsigned long long)cputime64_to_clock_t(iowait),
370 (unsigned long long)cputime64_to_clock_t(irq),
371 (unsigned long long)cputime64_to_clock_t(softirq),
372 (unsigned long long)cputime64_to_clock_t(steal),
373 (unsigned long long)cputime64_to_clock_t(guest));
374 for_each_online_cpu(i) {
376 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
377 user = kstat_cpu(i).cpustat.user;
378 nice = kstat_cpu(i).cpustat.nice;
379 system = kstat_cpu(i).cpustat.system;
380 idle = kstat_cpu(i).cpustat.idle;
381 iowait = kstat_cpu(i).cpustat.iowait;
382 irq = kstat_cpu(i).cpustat.irq;
383 softirq = kstat_cpu(i).cpustat.softirq;
384 steal = kstat_cpu(i).cpustat.steal;
385 guest = kstat_cpu(i).cpustat.guest;
386 seq_printf(p,
387 "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
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 (unsigned long long)cputime64_to_clock_t(guest));
399 seq_printf(p, "intr %llu", (unsigned long long)sum);
401 /* sum again ? it could be updated? */
402 for_each_irq_nr(j) {
403 per_irq_sum = 0;
405 for_each_possible_cpu(i)
406 per_irq_sum += kstat_irqs_cpu(j, i);
408 seq_printf(p, " %u", per_irq_sum);
411 seq_printf(p,
412 "\nctxt %llu\n"
413 "btime %lu\n"
414 "processes %lu\n"
415 "procs_running %lu\n"
416 "procs_blocked %lu\n",
417 nr_context_switches(),
418 (unsigned long)jif,
419 total_forks,
420 nr_running(),
421 nr_iowait());
423 return 0;
426 static int stat_open(struct inode *inode, struct file *file)
428 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
429 char *buf;
430 struct seq_file *m;
431 int res;
433 /* don't ask for more than the kmalloc() max size, currently 128 KB */
434 if (size > 128 * 1024)
435 size = 128 * 1024;
436 buf = kmalloc(size, GFP_KERNEL);
437 if (!buf)
438 return -ENOMEM;
440 res = single_open(file, show_stat, NULL);
441 if (!res) {
442 m = file->private_data;
443 m->buf = buf;
444 m->size = size;
445 } else
446 kfree(buf);
447 return res;
449 static const struct file_operations proc_stat_operations = {
450 .open = stat_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = single_release,
457 * /proc/interrupts
459 static void *int_seq_start(struct seq_file *f, loff_t *pos)
461 return (*pos <= nr_irqs) ? pos : NULL;
465 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
467 (*pos)++;
468 return (*pos <= nr_irqs) ? pos : NULL;
471 static void int_seq_stop(struct seq_file *f, void *v)
473 /* Nothing to do */
476 static const struct seq_operations int_seq_ops = {
477 .start = int_seq_start,
478 .next = int_seq_next,
479 .stop = int_seq_stop,
480 .show = show_interrupts
483 static int interrupts_open(struct inode *inode, struct file *filp)
485 return seq_open(filp, &int_seq_ops);
488 static const struct file_operations proc_interrupts_operations = {
489 .open = interrupts_open,
490 .read = seq_read,
491 .llseek = seq_lseek,
492 .release = seq_release,
495 static int filesystems_read_proc(char *page, char **start, off_t off,
496 int count, int *eof, void *data)
498 int len = get_filesystem_list(page);
499 return proc_calc_metrics(page, start, off, count, eof, len);
502 static int cmdline_read_proc(char *page, char **start, off_t off,
503 int count, int *eof, void *data)
505 int len;
507 len = sprintf(page, "%s\n", saved_command_line);
508 return proc_calc_metrics(page, start, off, count, eof, len);
511 #ifdef CONFIG_FILE_LOCKING
512 static int locks_open(struct inode *inode, struct file *filp)
514 return seq_open(filp, &locks_seq_operations);
517 static const struct file_operations proc_locks_operations = {
518 .open = locks_open,
519 .read = seq_read,
520 .llseek = seq_lseek,
521 .release = seq_release,
523 #endif /* CONFIG_FILE_LOCKING */
525 static int execdomains_read_proc(char *page, char **start, off_t off,
526 int count, int *eof, void *data)
528 int len = get_exec_domain_list(page);
529 return proc_calc_metrics(page, start, off, count, eof, len);
532 #ifdef CONFIG_PROC_PAGE_MONITOR
533 #define KPMSIZE sizeof(u64)
534 #define KPMMASK (KPMSIZE - 1)
535 /* /proc/kpagecount - an array exposing page counts
537 * Each entry is a u64 representing the corresponding
538 * physical page count.
540 static ssize_t kpagecount_read(struct file *file, char __user *buf,
541 size_t count, loff_t *ppos)
543 u64 __user *out = (u64 __user *)buf;
544 struct page *ppage;
545 unsigned long src = *ppos;
546 unsigned long pfn;
547 ssize_t ret = 0;
548 u64 pcount;
550 pfn = src / KPMSIZE;
551 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
552 if (src & KPMMASK || count & KPMMASK)
553 return -EINVAL;
555 while (count > 0) {
556 ppage = NULL;
557 if (pfn_valid(pfn))
558 ppage = pfn_to_page(pfn);
559 pfn++;
560 if (!ppage)
561 pcount = 0;
562 else
563 pcount = page_mapcount(ppage);
565 if (put_user(pcount, out++)) {
566 ret = -EFAULT;
567 break;
570 count -= KPMSIZE;
573 *ppos += (char __user *)out - buf;
574 if (!ret)
575 ret = (char __user *)out - buf;
576 return ret;
579 static struct file_operations proc_kpagecount_operations = {
580 .llseek = mem_lseek,
581 .read = kpagecount_read,
584 /* /proc/kpageflags - an array exposing page flags
586 * Each entry is a u64 representing the corresponding
587 * physical page flags.
590 /* These macros are used to decouple internal flags from exported ones */
592 #define KPF_LOCKED 0
593 #define KPF_ERROR 1
594 #define KPF_REFERENCED 2
595 #define KPF_UPTODATE 3
596 #define KPF_DIRTY 4
597 #define KPF_LRU 5
598 #define KPF_ACTIVE 6
599 #define KPF_SLAB 7
600 #define KPF_WRITEBACK 8
601 #define KPF_RECLAIM 9
602 #define KPF_BUDDY 10
604 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
606 static ssize_t kpageflags_read(struct file *file, char __user *buf,
607 size_t count, loff_t *ppos)
609 u64 __user *out = (u64 __user *)buf;
610 struct page *ppage;
611 unsigned long src = *ppos;
612 unsigned long pfn;
613 ssize_t ret = 0;
614 u64 kflags, uflags;
616 pfn = src / KPMSIZE;
617 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
618 if (src & KPMMASK || count & KPMMASK)
619 return -EINVAL;
621 while (count > 0) {
622 ppage = NULL;
623 if (pfn_valid(pfn))
624 ppage = pfn_to_page(pfn);
625 pfn++;
626 if (!ppage)
627 kflags = 0;
628 else
629 kflags = ppage->flags;
631 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
632 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
633 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
634 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
635 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
636 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
637 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
638 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
639 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
640 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
641 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
643 if (put_user(uflags, out++)) {
644 ret = -EFAULT;
645 break;
648 count -= KPMSIZE;
651 *ppos += (char __user *)out - buf;
652 if (!ret)
653 ret = (char __user *)out - buf;
654 return ret;
657 static struct file_operations proc_kpageflags_operations = {
658 .llseek = mem_lseek,
659 .read = kpageflags_read,
661 #endif /* CONFIG_PROC_PAGE_MONITOR */
663 struct proc_dir_entry *proc_root_kcore;
665 void __init proc_misc_init(void)
667 static struct {
668 char *name;
669 int (*read_proc)(char*,char**,off_t,int,int*,void*);
670 } *p, simple_ones[] = {
671 #ifdef CONFIG_PROC_HARDWARE
672 {"hardware", hardware_read_proc},
673 #endif
674 #ifdef CONFIG_STRAM_PROC
675 {"stram", stram_read_proc},
676 #endif
677 {"filesystems", filesystems_read_proc},
678 {"cmdline", cmdline_read_proc},
679 {"execdomains", execdomains_read_proc},
680 {NULL,}
682 for (p = simple_ones; p->name; p++)
683 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
685 proc_symlink("mounts", NULL, "self/mounts");
687 /* And now for trickier ones */
688 #ifdef CONFIG_PRINTK
689 proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
690 #endif
691 #ifdef CONFIG_FILE_LOCKING
692 proc_create("locks", 0, NULL, &proc_locks_operations);
693 #endif
694 proc_create("devices", 0, NULL, &proc_devinfo_operations);
695 proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
696 #ifdef CONFIG_BLOCK
697 proc_create("partitions", 0, NULL, &proc_partitions_operations);
698 #endif
699 proc_create("stat", 0, NULL, &proc_stat_operations);
700 proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
701 #ifdef CONFIG_SLABINFO
702 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
703 #ifdef CONFIG_DEBUG_SLAB_LEAK
704 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
705 #endif
706 #endif
707 #ifdef CONFIG_MMU
708 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
709 #endif
710 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
711 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
712 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
713 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
714 #ifdef CONFIG_BLOCK
715 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
716 #endif
717 #ifdef CONFIG_MODULES
718 proc_create("modules", 0, NULL, &proc_modules_operations);
719 #endif
720 #ifdef CONFIG_SCHEDSTATS
721 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
722 #endif
723 #ifdef CONFIG_PROC_KCORE
724 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
725 if (proc_root_kcore)
726 proc_root_kcore->size =
727 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
728 #endif
729 #ifdef CONFIG_PROC_PAGE_MONITOR
730 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
731 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
732 #endif
733 #ifdef CONFIG_PROC_VMCORE
734 proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
735 #endif