blob: 92ea7743fe8f59dd97f925b8e7923a463fb5b022 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Neil Horman7170be52006-01-14 13:20:38 -080023#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mm.h>
30#include <linux/mmzone.h>
31#include <linux/pagemap.h>
32#include <linux/swap.h>
33#include <linux/slab.h>
34#include <linux/smp.h>
35#include <linux/signal.h>
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/smp_lock.h>
39#include <linux/seq_file.h>
40#include <linux/times.h>
41#include <linux/profile.h>
Andrew Morton7bf65382006-12-08 02:41:14 -080042#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/hugetlb.h>
45#include <linux/jiffies.h>
46#include <linux/sysrq.h>
47#include <linux/vmalloc.h>
Vivek Goyal666bfdd2005-06-25 14:58:21 -070048#include <linux/crash_dump.h>
Sukadev Bhattiprolu61a58c62006-12-08 02:37:58 -080049#include <linux/pid_namespace.h>
Linus Torvalds89937802006-12-11 09:28:46 -080050#include <linux/compile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#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 */
66extern int get_hardware_list(char *);
67extern int get_stram_list(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern int get_filesystem_list(char *);
69extern int get_exec_domain_list(char *);
70extern int get_dma_list(char *);
71extern int get_locks_status (char *, char **, off_t, int);
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;
89
90 a = avenrun[0] + (FIXED_1/200);
91 b = avenrun[1] + (FIXED_1/200);
92 c = avenrun[2] + (FIXED_1/200);
93 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
94 LOAD_INT(a), LOAD_FRAC(a),
95 LOAD_INT(b), LOAD_FRAC(b),
96 LOAD_INT(c), LOAD_FRAC(c),
Cedric Le Goater6cc1b222006-12-08 02:38:00 -080097 nr_running(), nr_threads, current->nsproxy->pid_ns->last_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return proc_calc_metrics(page, start, off, count, eof, len);
99}
100
101static int uptime_read_proc(char *page, char **start, off_t off,
102 int count, int *eof, void *data)
103{
104 struct timespec uptime;
105 struct timespec idle;
106 int len;
107 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
108
109 do_posix_clock_monotonic_gettime(&uptime);
110 cputime_to_timespec(idletime, &idle);
111 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
112 (unsigned long) uptime.tv_sec,
113 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
114 (unsigned long) idle.tv_sec,
115 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
116
117 return proc_calc_metrics(page, start, off, count, eof, len);
118}
119
120static int meminfo_read_proc(char *page, char **start, off_t off,
121 int count, int *eof, void *data)
122{
123 struct sysinfo i;
124 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long inactive;
126 unsigned long active;
127 unsigned long free;
128 unsigned long committed;
129 unsigned long allowed;
130 struct vmalloc_info vmi;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700131 long cached;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 get_zone_counts(&active, &inactive, &free);
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
Christoph Lameter347ce432006-06-30 01:55:35 -0700145 cached = global_page_state(NR_FILE_PAGES) -
146 total_swapcache_pages - i.bufferram;
Martin Hicks4c4c4022005-04-16 15:24:08 -0700147 if (cached < 0)
148 cached = 0;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700163#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 "HighTotal: %8lu kB\n"
165 "HighFree: %8lu kB\n"
166 "LowTotal: %8lu kB\n"
167 "LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -0700168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 "SwapTotal: %8lu kB\n"
170 "SwapFree: %8lu kB\n"
171 "Dirty: %8lu kB\n"
172 "Writeback: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700173 "AnonPages: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 "Mapped: %8lu kB\n"
175 "Slab: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -0700176 "SReclaimable: %8lu kB\n"
177 "SUnreclaim: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -0700178 "PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -0700179 "NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700180 "Bounce: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 "CommitLimit: %8lu kB\n"
182 "Committed_AS: %8lu kB\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 "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),
Martin Hicks4c4c4022005-04-16 15:24:08 -0700189 K(cached),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 K(total_swapcache_pages),
191 K(active),
192 K(inactive),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700193#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 K(i.totalhigh),
195 K(i.freehigh),
196 K(i.totalram-i.totalhigh),
197 K(i.freeram-i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -0700198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 K(i.totalswap),
200 K(i.freeswap),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700201 K(global_page_state(NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -0700202 K(global_page_state(NR_WRITEBACK)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700203 K(global_page_state(NR_ANON_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700204 K(global_page_state(NR_FILE_MAPPED)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700205 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)),
Christoph Lameterdf849a12006-06-30 01:55:38 -0700209 K(global_page_state(NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700210 K(global_page_state(NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700211 K(global_page_state(NR_BOUNCE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 K(allowed),
213 K(committed),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 (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
225extern struct seq_operations fragmentation_op;
226static int fragmentation_open(struct inode *inode, struct file *file)
227{
228 (void)inode;
229 return seq_open(file, &fragmentation_op);
230}
231
232static struct file_operations fragmentation_file_operations = {
233 .open = fragmentation_open,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = seq_release,
237};
238
Nikita Danilov295ab932005-06-21 17:14:38 -0700239extern struct seq_operations zoneinfo_op;
240static int zoneinfo_open(struct inode *inode, struct file *file)
241{
242 return seq_open(file, &zoneinfo_op);
243}
244
245static struct file_operations proc_zoneinfo_file_operations = {
246 .open = zoneinfo_open,
247 .read = seq_read,
248 .llseek = seq_lseek,
249 .release = seq_release,
250};
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int version_read_proc(char *page, char **start, off_t off,
253 int count, int *eof, void *data)
254{
255 int len;
256
Linus Torvalds89937802006-12-11 09:28:46 -0800257 /* FIXED STRING! Don't touch! */
258 len = snprintf(page, PAGE_SIZE,
259 "%s version %s"
260 " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
261 " (" LINUX_COMPILER ")"
262 " %s\n",
263 utsname()->sysname,
264 utsname()->release,
265 utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return proc_calc_metrics(page, start, off, count, eof, len);
267}
268
269extern struct seq_operations cpuinfo_op;
270static int cpuinfo_open(struct inode *inode, struct file *file)
271{
272 return seq_open(file, &cpuinfo_op);
273}
Neil Horman7170be52006-01-14 13:20:38 -0800274
Joe Korty68eef3b2006-03-31 02:30:32 -0800275static struct file_operations proc_cpuinfo_operations = {
276 .open = cpuinfo_open,
Neil Horman7170be52006-01-14 13:20:38 -0800277 .read = seq_read,
278 .llseek = seq_lseek,
279 .release = seq_release,
280};
281
Joe Korty68eef3b2006-03-31 02:30:32 -0800282static int devinfo_show(struct seq_file *f, void *v)
283{
284 int i = *(loff_t *) v;
285
286 if (i < CHRDEV_MAJOR_HASH_SIZE) {
287 if (i == 0)
288 seq_printf(f, "Character devices:\n");
289 chrdev_show(f, i);
David Howells93614012006-09-30 20:45:40 +0200290 }
291#ifdef CONFIG_BLOCK
292 else {
Joe Korty68eef3b2006-03-31 02:30:32 -0800293 i -= CHRDEV_MAJOR_HASH_SIZE;
294 if (i == 0)
295 seq_printf(f, "\nBlock devices:\n");
296 blkdev_show(f, i);
297 }
David Howells93614012006-09-30 20:45:40 +0200298#endif
Joe Korty68eef3b2006-03-31 02:30:32 -0800299 return 0;
300}
301
302static void *devinfo_start(struct seq_file *f, loff_t *pos)
303{
304 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
305 return pos;
306 return NULL;
307}
308
309static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
310{
311 (*pos)++;
312 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
313 return NULL;
314 return pos;
315}
316
317static void devinfo_stop(struct seq_file *f, void *v)
318{
319 /* Nothing to do */
320}
321
322static struct seq_operations devinfo_ops = {
323 .start = devinfo_start,
324 .next = devinfo_next,
325 .stop = devinfo_stop,
326 .show = devinfo_show
327};
328
329static int devinfo_open(struct inode *inode, struct file *filp)
330{
331 return seq_open(filp, &devinfo_ops);
332}
333
334static struct file_operations proc_devinfo_operations = {
335 .open = devinfo_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .read = seq_read,
337 .llseek = seq_lseek,
338 .release = seq_release,
339};
340
341extern struct seq_operations vmstat_op;
342static int vmstat_open(struct inode *inode, struct file *file)
343{
344 return seq_open(file, &vmstat_op);
345}
346static struct file_operations proc_vmstat_file_operations = {
347 .open = vmstat_open,
348 .read = seq_read,
349 .llseek = seq_lseek,
350 .release = seq_release,
351};
352
353#ifdef CONFIG_PROC_HARDWARE
354static int hardware_read_proc(char *page, char **start, off_t off,
355 int count, int *eof, void *data)
356{
357 int len = get_hardware_list(page);
358 return proc_calc_metrics(page, start, off, count, eof, len);
359}
360#endif
361
362#ifdef CONFIG_STRAM_PROC
363static int stram_read_proc(char *page, char **start, off_t off,
364 int count, int *eof, void *data)
365{
366 int len = get_stram_list(page);
367 return proc_calc_metrics(page, start, off, count, eof, len);
368}
369#endif
370
David Howells93614012006-09-30 20:45:40 +0200371#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372extern struct seq_operations partitions_op;
373static int partitions_open(struct inode *inode, struct file *file)
374{
375 return seq_open(file, &partitions_op);
376}
377static struct file_operations proc_partitions_operations = {
378 .open = partitions_open,
379 .read = seq_read,
380 .llseek = seq_lseek,
381 .release = seq_release,
382};
383
384extern struct seq_operations diskstats_op;
385static int diskstats_open(struct inode *inode, struct file *file)
386{
387 return seq_open(file, &diskstats_op);
388}
389static struct file_operations proc_diskstats_operations = {
390 .open = diskstats_open,
391 .read = seq_read,
392 .llseek = seq_lseek,
393 .release = seq_release,
394};
David Howells93614012006-09-30 20:45:40 +0200395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397#ifdef CONFIG_MODULES
398extern struct seq_operations modules_op;
399static int modules_open(struct inode *inode, struct file *file)
400{
401 return seq_open(file, &modules_op);
402}
403static struct file_operations proc_modules_operations = {
404 .open = modules_open,
405 .read = seq_read,
406 .llseek = seq_lseek,
407 .release = seq_release,
408};
409#endif
410
Matt Mackall10cef602006-01-08 01:01:45 -0800411#ifdef CONFIG_SLAB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412extern struct seq_operations slabinfo_op;
413extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
414static int slabinfo_open(struct inode *inode, struct file *file)
415{
416 return seq_open(file, &slabinfo_op);
417}
418static struct file_operations proc_slabinfo_operations = {
419 .open = slabinfo_open,
420 .read = seq_read,
421 .write = slabinfo_write,
422 .llseek = seq_lseek,
423 .release = seq_release,
424};
Al Viro871751e2006-03-25 03:06:39 -0800425
426#ifdef CONFIG_DEBUG_SLAB_LEAK
427extern struct seq_operations slabstats_op;
428static int slabstats_open(struct inode *inode, struct file *file)
429{
430 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
431 int ret = -ENOMEM;
432 if (n) {
433 ret = seq_open(file, &slabstats_op);
434 if (!ret) {
435 struct seq_file *m = file->private_data;
436 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
437 m->private = n;
438 n = NULL;
439 }
440 kfree(n);
441 }
442 return ret;
443}
444
445static int slabstats_release(struct inode *inode, struct file *file)
446{
447 struct seq_file *m = file->private_data;
448 kfree(m->private);
449 return seq_release(inode, file);
450}
451
452static struct file_operations proc_slabstats_operations = {
453 .open = slabstats_open,
454 .read = seq_read,
455 .llseek = seq_lseek,
456 .release = slabstats_release,
457};
458#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461static int show_stat(struct seq_file *p, void *v)
462{
463 int i;
464 unsigned long jif;
465 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
466 u64 sum = 0;
467
468 user = nice = system = idle = iowait =
469 irq = softirq = steal = cputime64_zero;
470 jif = - wall_to_monotonic.tv_sec;
471 if (wall_to_monotonic.tv_nsec)
472 --jif;
473
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800474 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int j;
476
477 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
478 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
479 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
480 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
481 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
482 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
483 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
484 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
485 for (j = 0 ; j < NR_IRQS ; j++)
486 sum += kstat_cpu(i).irqs[j];
487 }
488
489 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu\n",
490 (unsigned long long)cputime64_to_clock_t(user),
491 (unsigned long long)cputime64_to_clock_t(nice),
492 (unsigned long long)cputime64_to_clock_t(system),
493 (unsigned long long)cputime64_to_clock_t(idle),
494 (unsigned long long)cputime64_to_clock_t(iowait),
495 (unsigned long long)cputime64_to_clock_t(irq),
496 (unsigned long long)cputime64_to_clock_t(softirq),
497 (unsigned long long)cputime64_to_clock_t(steal));
498 for_each_online_cpu(i) {
499
500 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
501 user = kstat_cpu(i).cpustat.user;
502 nice = kstat_cpu(i).cpustat.nice;
503 system = kstat_cpu(i).cpustat.system;
504 idle = kstat_cpu(i).cpustat.idle;
505 iowait = kstat_cpu(i).cpustat.iowait;
506 irq = kstat_cpu(i).cpustat.irq;
507 softirq = kstat_cpu(i).cpustat.softirq;
508 steal = kstat_cpu(i).cpustat.steal;
509 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
510 i,
511 (unsigned long long)cputime64_to_clock_t(user),
512 (unsigned long long)cputime64_to_clock_t(nice),
513 (unsigned long long)cputime64_to_clock_t(system),
514 (unsigned long long)cputime64_to_clock_t(idle),
515 (unsigned long long)cputime64_to_clock_t(iowait),
516 (unsigned long long)cputime64_to_clock_t(irq),
517 (unsigned long long)cputime64_to_clock_t(softirq),
518 (unsigned long long)cputime64_to_clock_t(steal));
519 }
520 seq_printf(p, "intr %llu", (unsigned long long)sum);
521
schwab@suse.dea1854612006-02-03 03:04:24 -0800522#if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = 0; i < NR_IRQS; i++)
524 seq_printf(p, " %u", kstat_irqs(i));
525#endif
526
527 seq_printf(p,
528 "\nctxt %llu\n"
529 "btime %lu\n"
530 "processes %lu\n"
531 "procs_running %lu\n"
532 "procs_blocked %lu\n",
533 nr_context_switches(),
534 (unsigned long)jif,
535 total_forks,
536 nr_running(),
537 nr_iowait());
538
539 return 0;
540}
541
542static int stat_open(struct inode *inode, struct file *file)
543{
544 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
545 char *buf;
546 struct seq_file *m;
547 int res;
548
549 /* don't ask for more than the kmalloc() max size, currently 128 KB */
550 if (size > 128 * 1024)
551 size = 128 * 1024;
552 buf = kmalloc(size, GFP_KERNEL);
553 if (!buf)
554 return -ENOMEM;
555
556 res = single_open(file, show_stat, NULL);
557 if (!res) {
558 m = file->private_data;
559 m->buf = buf;
560 m->size = size;
561 } else
562 kfree(buf);
563 return res;
564}
565static struct file_operations proc_stat_operations = {
566 .open = stat_open,
567 .read = seq_read,
568 .llseek = seq_lseek,
569 .release = single_release,
570};
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * /proc/interrupts
574 */
575static void *int_seq_start(struct seq_file *f, loff_t *pos)
576{
577 return (*pos <= NR_IRQS) ? pos : NULL;
578}
579
580static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
581{
582 (*pos)++;
583 if (*pos > NR_IRQS)
584 return NULL;
585 return pos;
586}
587
588static void int_seq_stop(struct seq_file *f, void *v)
589{
590 /* Nothing to do */
591}
592
593
594extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
595static struct seq_operations int_seq_ops = {
596 .start = int_seq_start,
597 .next = int_seq_next,
598 .stop = int_seq_stop,
599 .show = show_interrupts
600};
601
602static int interrupts_open(struct inode *inode, struct file *filp)
603{
604 return seq_open(filp, &int_seq_ops);
605}
606
607static struct file_operations proc_interrupts_operations = {
608 .open = interrupts_open,
609 .read = seq_read,
610 .llseek = seq_lseek,
611 .release = seq_release,
612};
613
614static int filesystems_read_proc(char *page, char **start, off_t off,
615 int count, int *eof, void *data)
616{
617 int len = get_filesystem_list(page);
618 return proc_calc_metrics(page, start, off, count, eof, len);
619}
620
621static int cmdline_read_proc(char *page, char **start, off_t off,
622 int count, int *eof, void *data)
623{
624 int len;
625
626 len = sprintf(page, "%s\n", saved_command_line);
627 return proc_calc_metrics(page, start, off, count, eof, len);
628}
629
630static int locks_read_proc(char *page, char **start, off_t off,
631 int count, int *eof, void *data)
632{
633 int len = get_locks_status(page, start, off, count);
634
635 if (len < count)
636 *eof = 1;
637 return len;
638}
639
640static int execdomains_read_proc(char *page, char **start, off_t off,
641 int count, int *eof, void *data)
642{
643 int len = get_exec_domain_list(page);
644 return proc_calc_metrics(page, start, off, count, eof, len);
645}
646
647#ifdef CONFIG_MAGIC_SYSRQ
648/*
649 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
650 */
651static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
652 size_t count, loff_t *ppos)
653{
654 if (count) {
655 char c;
656
657 if (get_user(c, buf))
658 return -EFAULT;
David Howells7d12e782006-10-05 14:55:46 +0100659 __handle_sysrq(c, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 return count;
662}
663
664static struct file_operations proc_sysrq_trigger_operations = {
665 .write = write_sysrq_trigger,
666};
667#endif
668
669struct proc_dir_entry *proc_root_kcore;
670
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800671void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct proc_dir_entry *entry;
674 entry = create_proc_entry(name, mode, NULL);
675 if (entry)
676 entry->proc_fops = f;
677}
678
679void __init proc_misc_init(void)
680{
681 struct proc_dir_entry *entry;
682 static struct {
683 char *name;
684 int (*read_proc)(char*,char**,off_t,int,int*,void*);
685 } *p, simple_ones[] = {
686 {"loadavg", loadavg_read_proc},
687 {"uptime", uptime_read_proc},
688 {"meminfo", meminfo_read_proc},
689 {"version", version_read_proc},
690#ifdef CONFIG_PROC_HARDWARE
691 {"hardware", hardware_read_proc},
692#endif
693#ifdef CONFIG_STRAM_PROC
694 {"stram", stram_read_proc},
695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 {"filesystems", filesystems_read_proc},
697 {"cmdline", cmdline_read_proc},
698 {"locks", locks_read_proc},
699 {"execdomains", execdomains_read_proc},
700 {NULL,}
701 };
702 for (p = simple_ones; p->name; p++)
703 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
704
705 proc_symlink("mounts", NULL, "self/mounts");
706
707 /* And now for trickier ones */
Mike Galbraithc36264d2006-12-06 20:37:42 -0800708#ifdef CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
710 if (entry)
711 entry->proc_fops = &proc_kmsg_operations;
Mike Galbraithc36264d2006-12-06 20:37:42 -0800712#endif
Neil Horman7170be52006-01-14 13:20:38 -0800713 create_seq_entry("devices", 0, &proc_devinfo_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
David Howells93614012006-09-30 20:45:40 +0200715#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 create_seq_entry("partitions", 0, &proc_partitions_operations);
David Howells93614012006-09-30 20:45:40 +0200717#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 create_seq_entry("stat", 0, &proc_stat_operations);
719 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
Matt Mackall10cef602006-01-08 01:01:45 -0800720#ifdef CONFIG_SLAB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
Al Viro871751e2006-03-25 03:06:39 -0800722#ifdef CONFIG_DEBUG_SLAB_LEAK
723 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
724#endif
Matt Mackall10cef602006-01-08 01:01:45 -0800725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
727 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
Nikita Danilov295ab932005-06-21 17:14:38 -0700728 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
David Howells93614012006-09-30 20:45:40 +0200729#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
David Howells93614012006-09-30 20:45:40 +0200731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#ifdef CONFIG_MODULES
733 create_seq_entry("modules", 0, &proc_modules_operations);
734#endif
735#ifdef CONFIG_SCHEDSTATS
736 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
737#endif
738#ifdef CONFIG_PROC_KCORE
739 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
740 if (proc_root_kcore) {
741 proc_root_kcore->proc_fops = &proc_kcore_operations;
742 proc_root_kcore->size =
743 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
744 }
745#endif
Vivek Goyal666bfdd2005-06-25 14:58:21 -0700746#ifdef CONFIG_PROC_VMCORE
747 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
748 if (proc_vmcore)
749 proc_vmcore->proc_fops = &proc_vmcore_operations;
750#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#ifdef CONFIG_MAGIC_SYSRQ
752 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
753 if (entry)
754 entry->proc_fops = &proc_sysrq_trigger_operations;
755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}