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