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