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