]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/proc/proc_misc.c
proc: switch /proc/uptime to seq_file
[linux-2.6-omap-h63xx.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/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"
59
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_exec_domain_list(char *);
69
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71                                  int count, int *eof, int len)
72 {
73         if (len <= off+count) *eof = 1;
74         *start = page + off;
75         len -= off;
76         if (len>count) len = count;
77         if (len<0) len = 0;
78         return len;
79 }
80
81 int __attribute__((weak)) arch_report_meminfo(char *page)
82 {
83         return 0;
84 }
85
86 static int meminfo_read_proc(char *page, char **start, off_t off,
87                                  int count, int *eof, void *data)
88 {
89         struct sysinfo i;
90         int len;
91         unsigned long committed;
92         unsigned long allowed;
93         struct vmalloc_info vmi;
94         long cached;
95         unsigned long pages[NR_LRU_LISTS];
96         int lru;
97
98 /*
99  * display in kilobytes.
100  */
101 #define K(x) ((x) << (PAGE_SHIFT - 10))
102         si_meminfo(&i);
103         si_swapinfo(&i);
104         committed = atomic_long_read(&vm_committed_space);
105         allowed = ((totalram_pages - hugetlb_total_pages())
106                 * sysctl_overcommit_ratio / 100) + total_swap_pages;
107
108         cached = global_page_state(NR_FILE_PAGES) -
109                         total_swapcache_pages - i.bufferram;
110         if (cached < 0)
111                 cached = 0;
112
113         get_vmalloc_info(&vmi);
114
115         for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
116                 pages[lru] = global_page_state(NR_LRU_BASE + lru);
117
118         /*
119          * Tagged format, for easy grepping and expansion.
120          */
121         len = sprintf(page,
122                 "MemTotal:       %8lu kB\n"
123                 "MemFree:        %8lu kB\n"
124                 "Buffers:        %8lu kB\n"
125                 "Cached:         %8lu kB\n"
126                 "SwapCached:     %8lu kB\n"
127                 "Active:         %8lu kB\n"
128                 "Inactive:       %8lu kB\n"
129                 "Active(anon):   %8lu kB\n"
130                 "Inactive(anon): %8lu kB\n"
131                 "Active(file):   %8lu kB\n"
132                 "Inactive(file): %8lu kB\n"
133 #ifdef CONFIG_UNEVICTABLE_LRU
134                 "Unevictable:    %8lu kB\n"
135                 "Mlocked:        %8lu kB\n"
136 #endif
137 #ifdef CONFIG_HIGHMEM
138                 "HighTotal:      %8lu kB\n"
139                 "HighFree:       %8lu kB\n"
140                 "LowTotal:       %8lu kB\n"
141                 "LowFree:        %8lu kB\n"
142 #endif
143                 "SwapTotal:      %8lu kB\n"
144                 "SwapFree:       %8lu kB\n"
145                 "Dirty:          %8lu kB\n"
146                 "Writeback:      %8lu kB\n"
147                 "AnonPages:      %8lu kB\n"
148                 "Mapped:         %8lu kB\n"
149                 "Slab:           %8lu kB\n"
150                 "SReclaimable:   %8lu kB\n"
151                 "SUnreclaim:     %8lu kB\n"
152                 "PageTables:     %8lu kB\n"
153 #ifdef CONFIG_QUICKLIST
154                 "Quicklists:     %8lu kB\n"
155 #endif
156                 "NFS_Unstable:   %8lu kB\n"
157                 "Bounce:         %8lu kB\n"
158                 "WritebackTmp:   %8lu kB\n"
159                 "CommitLimit:    %8lu kB\n"
160                 "Committed_AS:   %8lu kB\n"
161                 "VmallocTotal:   %8lu kB\n"
162                 "VmallocUsed:    %8lu kB\n"
163                 "VmallocChunk:   %8lu kB\n",
164                 K(i.totalram),
165                 K(i.freeram),
166                 K(i.bufferram),
167                 K(cached),
168                 K(total_swapcache_pages),
169                 K(pages[LRU_ACTIVE_ANON]   + pages[LRU_ACTIVE_FILE]),
170                 K(pages[LRU_INACTIVE_ANON] + pages[LRU_INACTIVE_FILE]),
171                 K(pages[LRU_ACTIVE_ANON]),
172                 K(pages[LRU_INACTIVE_ANON]),
173                 K(pages[LRU_ACTIVE_FILE]),
174                 K(pages[LRU_INACTIVE_FILE]),
175 #ifdef CONFIG_UNEVICTABLE_LRU
176                 K(pages[LRU_UNEVICTABLE]),
177                 K(global_page_state(NR_MLOCK)),
178 #endif
179 #ifdef CONFIG_HIGHMEM
180                 K(i.totalhigh),
181                 K(i.freehigh),
182                 K(i.totalram-i.totalhigh),
183                 K(i.freeram-i.freehigh),
184 #endif
185                 K(i.totalswap),
186                 K(i.freeswap),
187                 K(global_page_state(NR_FILE_DIRTY)),
188                 K(global_page_state(NR_WRITEBACK)),
189                 K(global_page_state(NR_ANON_PAGES)),
190                 K(global_page_state(NR_FILE_MAPPED)),
191                 K(global_page_state(NR_SLAB_RECLAIMABLE) +
192                                 global_page_state(NR_SLAB_UNRECLAIMABLE)),
193                 K(global_page_state(NR_SLAB_RECLAIMABLE)),
194                 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
195                 K(global_page_state(NR_PAGETABLE)),
196 #ifdef CONFIG_QUICKLIST
197                 K(quicklist_total_size()),
198 #endif
199                 K(global_page_state(NR_UNSTABLE_NFS)),
200                 K(global_page_state(NR_BOUNCE)),
201                 K(global_page_state(NR_WRITEBACK_TEMP)),
202                 K(allowed),
203                 K(committed),
204                 (unsigned long)VMALLOC_TOTAL >> 10,
205                 vmi.used >> 10,
206                 vmi.largest_chunk >> 10
207                 );
208
209                 len += hugetlb_report_meminfo(page + len);
210
211         len += arch_report_meminfo(page + len);
212
213         return proc_calc_metrics(page, start, off, count, eof, len);
214 #undef K
215 }
216
217 static int fragmentation_open(struct inode *inode, struct file *file)
218 {
219         (void)inode;
220         return seq_open(file, &fragmentation_op);
221 }
222
223 static const struct file_operations fragmentation_file_operations = {
224         .open           = fragmentation_open,
225         .read           = seq_read,
226         .llseek         = seq_lseek,
227         .release        = seq_release,
228 };
229
230 static int pagetypeinfo_open(struct inode *inode, struct file *file)
231 {
232         return seq_open(file, &pagetypeinfo_op);
233 }
234
235 static const struct file_operations pagetypeinfo_file_ops = {
236         .open           = pagetypeinfo_open,
237         .read           = seq_read,
238         .llseek         = seq_lseek,
239         .release        = seq_release,
240 };
241
242 static int zoneinfo_open(struct inode *inode, struct file *file)
243 {
244         return seq_open(file, &zoneinfo_op);
245 }
246
247 static const struct file_operations proc_zoneinfo_file_operations = {
248         .open           = zoneinfo_open,
249         .read           = seq_read,
250         .llseek         = seq_lseek,
251         .release        = seq_release,
252 };
253
254 static int version_read_proc(char *page, char **start, off_t off,
255                                  int count, int *eof, void *data)
256 {
257         int len;
258
259         len = snprintf(page, PAGE_SIZE, linux_proc_banner,
260                 utsname()->sysname,
261                 utsname()->release,
262                 utsname()->version);
263         return proc_calc_metrics(page, start, off, count, eof, len);
264 }
265
266 extern const struct seq_operations cpuinfo_op;
267 static int cpuinfo_open(struct inode *inode, struct file *file)
268 {
269         return seq_open(file, &cpuinfo_op);
270 }
271
272 static const struct file_operations proc_cpuinfo_operations = {
273         .open           = cpuinfo_open,
274         .read           = seq_read,
275         .llseek         = seq_lseek,
276         .release        = seq_release,
277 };
278
279 static int devinfo_show(struct seq_file *f, void *v)
280 {
281         int i = *(loff_t *) v;
282
283         if (i < CHRDEV_MAJOR_HASH_SIZE) {
284                 if (i == 0)
285                         seq_printf(f, "Character devices:\n");
286                 chrdev_show(f, i);
287         }
288 #ifdef CONFIG_BLOCK
289         else {
290                 i -= CHRDEV_MAJOR_HASH_SIZE;
291                 if (i == 0)
292                         seq_printf(f, "\nBlock devices:\n");
293                 blkdev_show(f, i);
294         }
295 #endif
296         return 0;
297 }
298
299 static void *devinfo_start(struct seq_file *f, loff_t *pos)
300 {
301         if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
302                 return pos;
303         return NULL;
304 }
305
306 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
307 {
308         (*pos)++;
309         if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
310                 return NULL;
311         return pos;
312 }
313
314 static void devinfo_stop(struct seq_file *f, void *v)
315 {
316         /* Nothing to do */
317 }
318
319 static const struct seq_operations devinfo_ops = {
320         .start = devinfo_start,
321         .next  = devinfo_next,
322         .stop  = devinfo_stop,
323         .show  = devinfo_show
324 };
325
326 static int devinfo_open(struct inode *inode, struct file *filp)
327 {
328         return seq_open(filp, &devinfo_ops);
329 }
330
331 static const struct file_operations proc_devinfo_operations = {
332         .open           = devinfo_open,
333         .read           = seq_read,
334         .llseek         = seq_lseek,
335         .release        = seq_release,
336 };
337
338 static int vmstat_open(struct inode *inode, struct file *file)
339 {
340         return seq_open(file, &vmstat_op);
341 }
342 static const struct file_operations proc_vmstat_file_operations = {
343         .open           = vmstat_open,
344         .read           = seq_read,
345         .llseek         = seq_lseek,
346         .release        = seq_release,
347 };
348
349 #ifdef CONFIG_PROC_HARDWARE
350 static int hardware_read_proc(char *page, char **start, off_t off,
351                                  int count, int *eof, void *data)
352 {
353         int len = get_hardware_list(page);
354         return proc_calc_metrics(page, start, off, count, eof, len);
355 }
356 #endif
357
358 #ifdef CONFIG_STRAM_PROC
359 static int stram_read_proc(char *page, char **start, off_t off,
360                                  int count, int *eof, void *data)
361 {
362         int len = get_stram_list(page);
363         return proc_calc_metrics(page, start, off, count, eof, len);
364 }
365 #endif
366
367 #ifdef CONFIG_BLOCK
368 static int partitions_open(struct inode *inode, struct file *file)
369 {
370         return seq_open(file, &partitions_op);
371 }
372 static const struct file_operations proc_partitions_operations = {
373         .open           = partitions_open,
374         .read           = seq_read,
375         .llseek         = seq_lseek,
376         .release        = seq_release,
377 };
378
379 static int diskstats_open(struct inode *inode, struct file *file)
380 {
381         return seq_open(file, &diskstats_op);
382 }
383 static const struct file_operations proc_diskstats_operations = {
384         .open           = diskstats_open,
385         .read           = seq_read,
386         .llseek         = seq_lseek,
387         .release        = seq_release,
388 };
389 #endif
390
391 #ifdef CONFIG_MODULES
392 extern const struct seq_operations modules_op;
393 static int modules_open(struct inode *inode, struct file *file)
394 {
395         return seq_open(file, &modules_op);
396 }
397 static const struct file_operations proc_modules_operations = {
398         .open           = modules_open,
399         .read           = seq_read,
400         .llseek         = seq_lseek,
401         .release        = seq_release,
402 };
403 #endif
404
405 #ifdef CONFIG_SLABINFO
406 static int slabinfo_open(struct inode *inode, struct file *file)
407 {
408         return seq_open(file, &slabinfo_op);
409 }
410 static const struct file_operations proc_slabinfo_operations = {
411         .open           = slabinfo_open,
412         .read           = seq_read,
413         .write          = slabinfo_write,
414         .llseek         = seq_lseek,
415         .release        = seq_release,
416 };
417
418 #ifdef CONFIG_DEBUG_SLAB_LEAK
419 extern const struct seq_operations slabstats_op;
420 static int slabstats_open(struct inode *inode, struct file *file)
421 {
422         unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
423         int ret = -ENOMEM;
424         if (n) {
425                 ret = seq_open(file, &slabstats_op);
426                 if (!ret) {
427                         struct seq_file *m = file->private_data;
428                         *n = PAGE_SIZE / (2 * sizeof(unsigned long));
429                         m->private = n;
430                         n = NULL;
431                 }
432                 kfree(n);
433         }
434         return ret;
435 }
436
437 static const struct file_operations proc_slabstats_operations = {
438         .open           = slabstats_open,
439         .read           = seq_read,
440         .llseek         = seq_lseek,
441         .release        = seq_release_private,
442 };
443 #endif
444 #endif
445
446 #ifdef CONFIG_MMU
447 static int vmalloc_open(struct inode *inode, struct file *file)
448 {
449         unsigned int *ptr = NULL;
450         int ret;
451
452         if (NUMA_BUILD)
453                 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
454         ret = seq_open(file, &vmalloc_op);
455         if (!ret) {
456                 struct seq_file *m = file->private_data;
457                 m->private = ptr;
458         } else
459                 kfree(ptr);
460         return ret;
461 }
462
463 static const struct file_operations proc_vmalloc_operations = {
464         .open           = vmalloc_open,
465         .read           = seq_read,
466         .llseek         = seq_lseek,
467         .release        = seq_release_private,
468 };
469 #endif
470
471 #ifndef arch_irq_stat_cpu
472 #define arch_irq_stat_cpu(cpu) 0
473 #endif
474 #ifndef arch_irq_stat
475 #define arch_irq_stat() 0
476 #endif
477
478 static int show_stat(struct seq_file *p, void *v)
479 {
480         int i, j;
481         unsigned long jif;
482         cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
483         cputime64_t guest;
484         u64 sum = 0;
485         struct timespec boottime;
486         unsigned int per_irq_sum;
487
488         user = nice = system = idle = iowait =
489                 irq = softirq = steal = cputime64_zero;
490         guest = cputime64_zero;
491         getboottime(&boottime);
492         jif = boottime.tv_sec;
493
494         for_each_possible_cpu(i) {
495                 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
496                 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
497                 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
498                 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
499                 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
500                 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
501                 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
502                 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
503                 guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
504
505                 for_each_irq_nr(j)
506                         sum += kstat_irqs_cpu(j, i);
507
508                 sum += arch_irq_stat_cpu(i);
509         }
510         sum += arch_irq_stat();
511
512         seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
513                 (unsigned long long)cputime64_to_clock_t(user),
514                 (unsigned long long)cputime64_to_clock_t(nice),
515                 (unsigned long long)cputime64_to_clock_t(system),
516                 (unsigned long long)cputime64_to_clock_t(idle),
517                 (unsigned long long)cputime64_to_clock_t(iowait),
518                 (unsigned long long)cputime64_to_clock_t(irq),
519                 (unsigned long long)cputime64_to_clock_t(softirq),
520                 (unsigned long long)cputime64_to_clock_t(steal),
521                 (unsigned long long)cputime64_to_clock_t(guest));
522         for_each_online_cpu(i) {
523
524                 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
525                 user = kstat_cpu(i).cpustat.user;
526                 nice = kstat_cpu(i).cpustat.nice;
527                 system = kstat_cpu(i).cpustat.system;
528                 idle = kstat_cpu(i).cpustat.idle;
529                 iowait = kstat_cpu(i).cpustat.iowait;
530                 irq = kstat_cpu(i).cpustat.irq;
531                 softirq = kstat_cpu(i).cpustat.softirq;
532                 steal = kstat_cpu(i).cpustat.steal;
533                 guest = kstat_cpu(i).cpustat.guest;
534                 seq_printf(p,
535                         "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
536                         i,
537                         (unsigned long long)cputime64_to_clock_t(user),
538                         (unsigned long long)cputime64_to_clock_t(nice),
539                         (unsigned long long)cputime64_to_clock_t(system),
540                         (unsigned long long)cputime64_to_clock_t(idle),
541                         (unsigned long long)cputime64_to_clock_t(iowait),
542                         (unsigned long long)cputime64_to_clock_t(irq),
543                         (unsigned long long)cputime64_to_clock_t(softirq),
544                         (unsigned long long)cputime64_to_clock_t(steal),
545                         (unsigned long long)cputime64_to_clock_t(guest));
546         }
547         seq_printf(p, "intr %llu", (unsigned long long)sum);
548
549         /* sum again ? it could be updated? */
550         for_each_irq_nr(j) {
551                 per_irq_sum = 0;
552
553                 for_each_possible_cpu(i)
554                         per_irq_sum += kstat_irqs_cpu(j, i);
555
556                 seq_printf(p, " %u", per_irq_sum);
557         }
558
559         seq_printf(p,
560                 "\nctxt %llu\n"
561                 "btime %lu\n"
562                 "processes %lu\n"
563                 "procs_running %lu\n"
564                 "procs_blocked %lu\n",
565                 nr_context_switches(),
566                 (unsigned long)jif,
567                 total_forks,
568                 nr_running(),
569                 nr_iowait());
570
571         return 0;
572 }
573
574 static int stat_open(struct inode *inode, struct file *file)
575 {
576         unsigned size = 4096 * (1 + num_possible_cpus() / 32);
577         char *buf;
578         struct seq_file *m;
579         int res;
580
581         /* don't ask for more than the kmalloc() max size, currently 128 KB */
582         if (size > 128 * 1024)
583                 size = 128 * 1024;
584         buf = kmalloc(size, GFP_KERNEL);
585         if (!buf)
586                 return -ENOMEM;
587
588         res = single_open(file, show_stat, NULL);
589         if (!res) {
590                 m = file->private_data;
591                 m->buf = buf;
592                 m->size = size;
593         } else
594                 kfree(buf);
595         return res;
596 }
597 static const struct file_operations proc_stat_operations = {
598         .open           = stat_open,
599         .read           = seq_read,
600         .llseek         = seq_lseek,
601         .release        = single_release,
602 };
603
604 /*
605  * /proc/interrupts
606  */
607 static void *int_seq_start(struct seq_file *f, loff_t *pos)
608 {
609         return (*pos <= nr_irqs) ? pos : NULL;
610 }
611
612
613 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
614 {
615         (*pos)++;
616         return (*pos <= nr_irqs) ? pos : NULL;
617 }
618
619 static void int_seq_stop(struct seq_file *f, void *v)
620 {
621         /* Nothing to do */
622 }
623
624 static const struct seq_operations int_seq_ops = {
625         .start = int_seq_start,
626         .next  = int_seq_next,
627         .stop  = int_seq_stop,
628         .show  = show_interrupts
629 };
630
631 static int interrupts_open(struct inode *inode, struct file *filp)
632 {
633         return seq_open(filp, &int_seq_ops);
634 }
635
636 static const struct file_operations proc_interrupts_operations = {
637         .open           = interrupts_open,
638         .read           = seq_read,
639         .llseek         = seq_lseek,
640         .release        = seq_release,
641 };
642
643 static int filesystems_read_proc(char *page, char **start, off_t off,
644                                  int count, int *eof, void *data)
645 {
646         int len = get_filesystem_list(page);
647         return proc_calc_metrics(page, start, off, count, eof, len);
648 }
649
650 static int cmdline_read_proc(char *page, char **start, off_t off,
651                                  int count, int *eof, void *data)
652 {
653         int len;
654
655         len = sprintf(page, "%s\n", saved_command_line);
656         return proc_calc_metrics(page, start, off, count, eof, len);
657 }
658
659 #ifdef CONFIG_FILE_LOCKING
660 static int locks_open(struct inode *inode, struct file *filp)
661 {
662         return seq_open(filp, &locks_seq_operations);
663 }
664
665 static const struct file_operations proc_locks_operations = {
666         .open           = locks_open,
667         .read           = seq_read,
668         .llseek         = seq_lseek,
669         .release        = seq_release,
670 };
671 #endif /* CONFIG_FILE_LOCKING */
672
673 static int execdomains_read_proc(char *page, char **start, off_t off,
674                                  int count, int *eof, void *data)
675 {
676         int len = get_exec_domain_list(page);
677         return proc_calc_metrics(page, start, off, count, eof, len);
678 }
679
680 #ifdef CONFIG_PROC_PAGE_MONITOR
681 #define KPMSIZE sizeof(u64)
682 #define KPMMASK (KPMSIZE - 1)
683 /* /proc/kpagecount - an array exposing page counts
684  *
685  * Each entry is a u64 representing the corresponding
686  * physical page count.
687  */
688 static ssize_t kpagecount_read(struct file *file, char __user *buf,
689                              size_t count, loff_t *ppos)
690 {
691         u64 __user *out = (u64 __user *)buf;
692         struct page *ppage;
693         unsigned long src = *ppos;
694         unsigned long pfn;
695         ssize_t ret = 0;
696         u64 pcount;
697
698         pfn = src / KPMSIZE;
699         count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
700         if (src & KPMMASK || count & KPMMASK)
701                 return -EINVAL;
702
703         while (count > 0) {
704                 ppage = NULL;
705                 if (pfn_valid(pfn))
706                         ppage = pfn_to_page(pfn);
707                 pfn++;
708                 if (!ppage)
709                         pcount = 0;
710                 else
711                         pcount = page_mapcount(ppage);
712
713                 if (put_user(pcount, out++)) {
714                         ret = -EFAULT;
715                         break;
716                 }
717
718                 count -= KPMSIZE;
719         }
720
721         *ppos += (char __user *)out - buf;
722         if (!ret)
723                 ret = (char __user *)out - buf;
724         return ret;
725 }
726
727 static struct file_operations proc_kpagecount_operations = {
728         .llseek = mem_lseek,
729         .read = kpagecount_read,
730 };
731
732 /* /proc/kpageflags - an array exposing page flags
733  *
734  * Each entry is a u64 representing the corresponding
735  * physical page flags.
736  */
737
738 /* These macros are used to decouple internal flags from exported ones */
739
740 #define KPF_LOCKED     0
741 #define KPF_ERROR      1
742 #define KPF_REFERENCED 2
743 #define KPF_UPTODATE   3
744 #define KPF_DIRTY      4
745 #define KPF_LRU        5
746 #define KPF_ACTIVE     6
747 #define KPF_SLAB       7
748 #define KPF_WRITEBACK  8
749 #define KPF_RECLAIM    9
750 #define KPF_BUDDY     10
751
752 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
753
754 static ssize_t kpageflags_read(struct file *file, char __user *buf,
755                              size_t count, loff_t *ppos)
756 {
757         u64 __user *out = (u64 __user *)buf;
758         struct page *ppage;
759         unsigned long src = *ppos;
760         unsigned long pfn;
761         ssize_t ret = 0;
762         u64 kflags, uflags;
763
764         pfn = src / KPMSIZE;
765         count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
766         if (src & KPMMASK || count & KPMMASK)
767                 return -EINVAL;
768
769         while (count > 0) {
770                 ppage = NULL;
771                 if (pfn_valid(pfn))
772                         ppage = pfn_to_page(pfn);
773                 pfn++;
774                 if (!ppage)
775                         kflags = 0;
776                 else
777                         kflags = ppage->flags;
778
779                 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
780                         kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
781                         kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
782                         kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
783                         kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
784                         kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
785                         kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
786                         kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
787                         kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
788                         kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
789                         kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
790
791                 if (put_user(uflags, out++)) {
792                         ret = -EFAULT;
793                         break;
794                 }
795
796                 count -= KPMSIZE;
797         }
798
799         *ppos += (char __user *)out - buf;
800         if (!ret)
801                 ret = (char __user *)out - buf;
802         return ret;
803 }
804
805 static struct file_operations proc_kpageflags_operations = {
806         .llseek = mem_lseek,
807         .read = kpageflags_read,
808 };
809 #endif /* CONFIG_PROC_PAGE_MONITOR */
810
811 struct proc_dir_entry *proc_root_kcore;
812
813 void __init proc_misc_init(void)
814 {
815         static struct {
816                 char *name;
817                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
818         } *p, simple_ones[] = {
819                 {"meminfo",     meminfo_read_proc},
820                 {"version",     version_read_proc},
821 #ifdef CONFIG_PROC_HARDWARE
822                 {"hardware",    hardware_read_proc},
823 #endif
824 #ifdef CONFIG_STRAM_PROC
825                 {"stram",       stram_read_proc},
826 #endif
827                 {"filesystems", filesystems_read_proc},
828                 {"cmdline",     cmdline_read_proc},
829                 {"execdomains", execdomains_read_proc},
830                 {NULL,}
831         };
832         for (p = simple_ones; p->name; p++)
833                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
834
835         proc_symlink("mounts", NULL, "self/mounts");
836
837         /* And now for trickier ones */
838 #ifdef CONFIG_PRINTK
839         proc_create("kmsg", S_IRUSR, NULL, &proc_kmsg_operations);
840 #endif
841 #ifdef CONFIG_FILE_LOCKING
842         proc_create("locks", 0, NULL, &proc_locks_operations);
843 #endif
844         proc_create("devices", 0, NULL, &proc_devinfo_operations);
845         proc_create("cpuinfo", 0, NULL, &proc_cpuinfo_operations);
846 #ifdef CONFIG_BLOCK
847         proc_create("partitions", 0, NULL, &proc_partitions_operations);
848 #endif
849         proc_create("stat", 0, NULL, &proc_stat_operations);
850         proc_create("interrupts", 0, NULL, &proc_interrupts_operations);
851 #ifdef CONFIG_SLABINFO
852         proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
853 #ifdef CONFIG_DEBUG_SLAB_LEAK
854         proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
855 #endif
856 #endif
857 #ifdef CONFIG_MMU
858         proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
859 #endif
860         proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
861         proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
862         proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
863         proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
864 #ifdef CONFIG_BLOCK
865         proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
866 #endif
867 #ifdef CONFIG_MODULES
868         proc_create("modules", 0, NULL, &proc_modules_operations);
869 #endif
870 #ifdef CONFIG_SCHEDSTATS
871         proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
872 #endif
873 #ifdef CONFIG_PROC_KCORE
874         proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
875         if (proc_root_kcore)
876                 proc_root_kcore->size =
877                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
878 #endif
879 #ifdef CONFIG_PROC_PAGE_MONITOR
880         proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
881         proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
882 #endif
883 #ifdef CONFIG_PROC_VMCORE
884         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
885 #endif
886 }