import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/tty.h>
24 #include <linux/string.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/ioport.h>
28 #include <linux/config.h>
29 #include <linux/mm.h>
30 #include <linux/pagemap.h>
31 #include <linux/swap.h>
32 #include <linux/slab.h>
33 #include <linux/smp.h>
34 #include <linux/signal.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/smp_lock.h>
38 #include <linux/seq_file.h>
39 #include <linux/sysrq.h>
40
41 #include <asm/uaccess.h>
42 #include <asm/pgtable.h>
43 #include <asm/io.h>
44
45 #define LOAD_INT(x) ((x) >> FSHIFT)
46 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
47 /*
48  * Warning: stuff below (imported functions) assumes that its output will fit
49  * into one page. For some of those functions it may be wrong. Moreover, we
50  * have a way to deal with that gracefully. Right now I used straightforward
51  * wrappers, but this needs further analysis wrt potential overflows.
52  */
53 extern int get_hardware_list(char *);
54 extern int get_stram_list(char *);
55 #ifdef CONFIG_MODULES
56 extern int get_module_list(char *);
57 #endif
58 extern int get_device_list(char *);
59 extern int get_filesystem_list(char *);
60 extern int get_exec_domain_list(char *);
61 #ifndef CONFIG_X86
62 extern int get_irq_list(char *);
63 #endif
64 extern int get_dma_list(char *);
65 extern int get_locks_status (char *, char **, off_t, int);
66 extern int get_swaparea_info (char *);
67 #ifdef CONFIG_SGI_DS1286
68 extern int get_ds1286_status(char *);
69 #endif
70
71 void proc_sprintf(char *page, off_t *off, int *lenp, const char *format, ...)
72 {
73         int len = *lenp;
74         va_list args;
75
76         /* try to only print whole lines */
77         if (len > PAGE_SIZE-512)
78                 return;
79
80         va_start(args, format);
81         len += vsnprintf(page + len, PAGE_SIZE-len, format, args);
82         va_end(args);
83
84         if (len <= *off) {
85                 *off -= len;
86                 len = 0;
87         }
88
89         *lenp = len;
90 }
91
92 static int proc_calc_metrics(char *page, char **start, off_t off,
93                                  int count, int *eof, int len)
94 {
95         if (len <= off+count) *eof = 1;
96         *start = page + off;
97         len -= off;
98         if (len>count) len = count;
99         if (len<0) len = 0;
100         return len;
101 }
102
103 static int loadavg_read_proc(char *page, char **start, off_t off,
104                                  int count, int *eof, void *data)
105 {
106         int a, b, c;
107         int len;
108
109         a = avenrun[0] + (FIXED_1/200);
110         b = avenrun[1] + (FIXED_1/200);
111         c = avenrun[2] + (FIXED_1/200);
112         len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
113                 LOAD_INT(a), LOAD_FRAC(a),
114                 LOAD_INT(b), LOAD_FRAC(b),
115                 LOAD_INT(c), LOAD_FRAC(c),
116                 nr_running, nr_threads, last_pid);
117         return proc_calc_metrics(page, start, off, count, eof, len);
118 }
119
120 static int uptime_read_proc(char *page, char **start, off_t off,
121                                  int count, int *eof, void *data)
122 {
123         unsigned long uptime;
124         unsigned long idle;
125         int len;
126
127         uptime = jiffies;
128         idle = init_tasks[0]->times.tms_utime + init_tasks[0]->times.tms_stime;
129
130         /* The formula for the fraction parts really is ((t * 100) / HZ) % 100, but
131            that would overflow about every five days at HZ == 100.
132            Therefore the identity a = (a / b) * b + a % b is used so that it is
133            calculated as (((t / HZ) * 100) + ((t % HZ) * 100) / HZ) % 100.
134            The part in front of the '+' always evaluates as 0 (mod 100). All divisions
135            in the above formulas are truncating. For HZ being a power of 10, the
136            calculations simplify to the version in the #else part (if the printf
137            format is adapted to the same number of digits as zeroes in HZ.
138          */
139 #if HZ!=100
140         len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
141                 uptime / HZ,
142                 (((uptime % HZ) * 100) / HZ) % 100,
143                 idle / HZ,
144                 (((idle % HZ) * 100) / HZ) % 100);
145 #else
146         len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
147                 uptime / HZ,
148                 uptime % HZ,
149                 idle / HZ,
150                 idle % HZ);
151 #endif
152         return proc_calc_metrics(page, start, off, count, eof, len);
153 }
154
155 static int meminfo_read_proc(char *page, char **start, off_t off,
156                                  int count, int *eof, void *data)
157 {
158         struct sysinfo i;
159         int len;
160         int pg_size ;
161
162 /*
163  * display in kilobytes.
164  */
165 #define K(x) ((x) << (PAGE_SHIFT - 10))
166 #define B(x) ((unsigned long long)(x) << PAGE_SHIFT)
167         si_meminfo(&i);
168         si_swapinfo(&i);
169         pg_size = page_cache_size - i.bufferram;
170
171         len = sprintf(page, "        total:    used:    free:  shared: buffers:  cached:\n"
172                 "Mem:  %8Lu %8Lu %8Lu %8Lu %8Lu %8Lu\n"
173                 "Swap: %8Lu %8Lu %8Lu\n",
174                 B(i.totalram), B(i.totalram-i.freeram), B(i.freeram),
175                 B(i.sharedram), B(i.bufferram),
176                 B(pg_size), B(i.totalswap),
177                 B(i.totalswap-i.freeswap), B(i.freeswap));
178         /*
179          * Tagged format, for easy grepping and expansion.
180          * The above will go away eventually, once the tools
181          * have been updated.
182          */
183         len += sprintf(page+len,
184                 "MemTotal:     %8lu kB\n"
185                 "MemFree:      %8lu kB\n"
186                 "MemShared:    %8lu kB\n"
187                 "Buffers:      %8lu kB\n"
188                 "Cached:       %8lu kB\n"
189                 "SwapCached:   %8lu kB\n"
190                 "Active:       %8u kB\n"
191                 "Inactive:     %8u kB\n"
192                 "HighTotal:    %8lu kB\n"
193                 "HighFree:     %8lu kB\n"
194                 "LowTotal:     %8lu kB\n"
195                 "LowFree:      %8lu kB\n"
196                 "SwapTotal:    %8lu kB\n"
197                 "SwapFree:     %8lu kB\n",
198                 K(i.totalram),
199                 K(i.freeram),
200                 K(i.sharedram),
201                 K(i.bufferram),
202                 K(pg_size - swapper_space.nrpages),
203                 K(swapper_space.nrpages),
204                 K(nr_active_pages),
205                 K(nr_inactive_pages),
206                 K(i.totalhigh),
207                 K(i.freehigh),
208                 K(i.totalram-i.totalhigh),
209                 K(i.freeram-i.freehigh),
210                 K(i.totalswap),
211                 K(i.freeswap));
212
213         return proc_calc_metrics(page, start, off, count, eof, len);
214 #undef B
215 #undef K
216 }
217
218 static int version_read_proc(char *page, char **start, off_t off,
219                                  int count, int *eof, void *data)
220 {
221         extern char *linux_banner;
222         int len;
223
224         strcpy(page, linux_banner);
225         len = strlen(page);
226         return proc_calc_metrics(page, start, off, count, eof, len);
227 }
228
229 extern struct seq_operations cpuinfo_op;
230 static int cpuinfo_open(struct inode *inode, struct file *file)
231 {
232         return seq_open(file, &cpuinfo_op);
233 }
234 static struct file_operations proc_cpuinfo_operations = {
235         open:           cpuinfo_open,
236         read:           seq_read,
237         llseek:         seq_lseek,
238         release:        seq_release,
239 };
240
241 #ifdef CONFIG_PROC_HARDWARE
242 static int hardware_read_proc(char *page, char **start, off_t off,
243                                  int count, int *eof, void *data)
244 {
245         int len = get_hardware_list(page);
246         return proc_calc_metrics(page, start, off, count, eof, len);
247 }
248 #endif
249
250 #ifdef CONFIG_STRAM_PROC
251 static int stram_read_proc(char *page, char **start, off_t off,
252                                  int count, int *eof, void *data)
253 {
254         int len = get_stram_list(page);
255         return proc_calc_metrics(page, start, off, count, eof, len);
256 }
257 #endif
258
259 extern struct seq_operations partitions_op;
260 static int partitions_open(struct inode *inode, struct file *file)
261 {
262         return seq_open(file, &partitions_op);
263 }
264 static struct file_operations proc_partitions_operations = {
265         open:           partitions_open,
266         read:           seq_read,
267         llseek:         seq_lseek,
268         release:        seq_release,
269 };
270
271 #ifdef CONFIG_MODULES
272 static int modules_read_proc(char *page, char **start, off_t off,
273                                  int count, int *eof, void *data)
274 {
275         int len = get_module_list(page);
276         return proc_calc_metrics(page, start, off, count, eof, len);
277 }
278
279 extern struct seq_operations ksyms_op;
280 static int ksyms_open(struct inode *inode, struct file *file)
281 {
282         return seq_open(file, &ksyms_op);
283 }
284 static struct file_operations proc_ksyms_operations = {
285         open:           ksyms_open,
286         read:           seq_read,
287         llseek:         seq_lseek,
288         release:        seq_release,
289 };
290 #endif
291
292 extern struct seq_operations slabinfo_op;
293 extern ssize_t slabinfo_write(struct file *, const char *, size_t, loff_t *);
294 static int slabinfo_open(struct inode *inode, struct file *file)
295 {
296         return seq_open(file, &slabinfo_op);
297 }
298 static struct file_operations proc_slabinfo_operations = {
299         open:           slabinfo_open,
300         read:           seq_read,
301         write:          slabinfo_write,
302         llseek:         seq_lseek,
303         release:        seq_release,
304 };
305
306 static int kstat_read_proc(char *page, char **start, off_t off,
307                                  int count, int *eof, void *data)
308 {
309         int i, len = 0;
310         extern unsigned long total_forks;
311         unsigned long jif = jiffies;
312         unsigned int sum = 0, user = 0, nice = 0, system = 0;
313         int major, disk;
314
315         for (i = 0 ; i < smp_num_cpus; i++) {
316                 int cpu = cpu_logical_map(i), j;
317
318                 user += kstat.per_cpu_user[cpu];
319                 nice += kstat.per_cpu_nice[cpu];
320                 system += kstat.per_cpu_system[cpu];
321 #if !defined(CONFIG_ARCH_S390)
322                 for (j = 0 ; j < NR_IRQS ; j++)
323                         sum += kstat.irqs[cpu][j];
324 #endif
325         }
326
327         proc_sprintf(page, &off, &len,
328                       "cpu  %u %u %u %lu\n", user, nice, system,
329                       jif * smp_num_cpus - (user + nice + system));
330         for (i = 0 ; i < smp_num_cpus; i++)
331                 proc_sprintf(page, &off, &len,
332                         "cpu%d %u %u %u %lu\n",
333                         i,
334                         kstat.per_cpu_user[cpu_logical_map(i)],
335                         kstat.per_cpu_nice[cpu_logical_map(i)],
336                         kstat.per_cpu_system[cpu_logical_map(i)],
337                         jif - (  kstat.per_cpu_user[cpu_logical_map(i)] \
338                                    + kstat.per_cpu_nice[cpu_logical_map(i)] \
339                                    + kstat.per_cpu_system[cpu_logical_map(i)]));
340         proc_sprintf(page, &off, &len,
341                 "page %u %u\n"
342                 "swap %u %u\n"
343                 "intr %u",
344                         kstat.pgpgin >> 1,
345                         kstat.pgpgout >> 1,
346                         kstat.pswpin,
347                         kstat.pswpout,
348                         sum
349         );
350 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_ALPHA)
351         for (i = 0 ; i < NR_IRQS ; i++)
352                 proc_sprintf(page, &off, &len,
353                              " %u", kstat_irqs(i));
354 #endif
355
356         proc_sprintf(page, &off, &len, "\ndisk_io: ");
357
358         for (major = 0; major < DK_MAX_MAJOR; major++) {
359                 for (disk = 0; disk < DK_MAX_DISK; disk++) {
360                         int active = kstat.dk_drive[major][disk] +
361                                 kstat.dk_drive_rblk[major][disk] +
362                                 kstat.dk_drive_wblk[major][disk];
363                         if (active)
364                                 proc_sprintf(page, &off, &len,
365                                         "(%u,%u):(%u,%u,%u,%u,%u) ",
366                                         major, disk,
367                                         kstat.dk_drive[major][disk],
368                                         kstat.dk_drive_rio[major][disk],
369                                         kstat.dk_drive_rblk[major][disk],
370                                         kstat.dk_drive_wio[major][disk],
371                                         kstat.dk_drive_wblk[major][disk]
372                         );
373                 }
374         }
375
376         proc_sprintf(page, &off, &len,
377                 "\nctxt %u\n"
378                 "btime %lu\n"
379                 "processes %lu\n",
380                 kstat.context_swtch,
381                 xtime.tv_sec - jif / HZ,
382                 total_forks);
383
384         return proc_calc_metrics(page, start, off, count, eof, len);
385 }
386
387 static int devices_read_proc(char *page, char **start, off_t off,
388                                  int count, int *eof, void *data)
389 {
390         int len = get_device_list(page);
391         return proc_calc_metrics(page, start, off, count, eof, len);
392 }
393
394 #ifndef CONFIG_X86
395 #if !defined(CONFIG_ARCH_S390)
396 static int interrupts_read_proc(char *page, char **start, off_t off,
397                                  int count, int *eof, void *data)
398 {
399         int len = get_irq_list(page);
400         return proc_calc_metrics(page, start, off, count, eof, len);
401 }
402 #endif
403
404 #else /* !CONFIG_X86 */
405
406 extern int show_interrupts(struct seq_file *p, void *v);
407 static int interrupts_open(struct inode *inode, struct file *file)
408 {
409         unsigned size = PAGE_SIZE * (1 + smp_num_cpus / 8);
410         char *buf = kmalloc(size, GFP_KERNEL);
411         struct seq_file *m;
412         int res;
413
414         if (!buf)
415                 return -ENOMEM;
416         res = single_open(file, show_interrupts, NULL);
417         if (!res) {
418                 m = file->private_data;
419                 m->buf = buf;
420                 m->size = size;
421         } else
422                 kfree(buf);
423         return res;
424 }
425 static struct file_operations proc_interrupts_operations = {
426         .open           = interrupts_open,
427         .read           = seq_read,
428         .llseek         = seq_lseek,
429         .release        = single_release,
430 };
431 #endif /* !CONFIG_X86 */
432
433 extern struct file_operations proc_ioports_operations;
434 extern struct file_operations proc_iomem_operations;
435
436 static int filesystems_read_proc(char *page, char **start, off_t off,
437                                  int count, int *eof, void *data)
438 {
439         int len = get_filesystem_list(page);
440         return proc_calc_metrics(page, start, off, count, eof, len);
441 }
442
443 static int dma_read_proc(char *page, char **start, off_t off,
444                                  int count, int *eof, void *data)
445 {
446         int len = get_dma_list(page);
447         return proc_calc_metrics(page, start, off, count, eof, len);
448 }
449
450 static int cmdline_read_proc(char *page, char **start, off_t off,
451                                  int count, int *eof, void *data)
452 {
453         extern char saved_command_line[];
454         int len = 0;
455
456         proc_sprintf(page, &off, &len, "%s\n", saved_command_line);
457         return proc_calc_metrics(page, start, off, count, eof, len);
458 }
459
460 #ifdef CONFIG_SGI_DS1286
461 static int ds1286_read_proc(char *page, char **start, off_t off,
462                                  int count, int *eof, void *data)
463 {
464         int len = get_ds1286_status(page);
465         return proc_calc_metrics(page, start, off, count, eof, len);
466 }
467 #endif
468
469 static int locks_read_proc(char *page, char **start, off_t off,
470                                  int count, int *eof, void *data)
471 {
472         int len;
473         lock_kernel();
474         len = get_locks_status(page, start, off, count);
475         unlock_kernel();
476         if (len < count) *eof = 1;
477         return len;
478 }
479
480 static int execdomains_read_proc(char *page, char **start, off_t off,
481                                  int count, int *eof, void *data)
482 {
483         int len = get_exec_domain_list(page);
484         return proc_calc_metrics(page, start, off, count, eof, len);
485 }
486
487 static int swaps_read_proc(char *page, char **start, off_t off,
488                                  int count, int *eof, void *data)
489 {
490         int len = get_swaparea_info(page);
491         return proc_calc_metrics(page, start, off, count, eof, len);
492 }
493
494 /*
495  * This function accesses profiling information. The returned data is
496  * binary: the sampling step and the actual contents of the profile
497  * buffer. Use of the program readprofile is recommended in order to
498  * get meaningful info out of these data.
499  */
500 static ssize_t read_profile(struct file *file, char *buf,
501                             size_t count, loff_t *ppos)
502 {
503         loff_t n = *ppos;
504         unsigned p = n;
505         ssize_t read;
506         char * pnt;
507         unsigned int sample_step = 1 << prof_shift;
508
509         if (p != n || p >= (prof_len+1)*sizeof(unsigned int))
510                 return 0;
511         if (count > (prof_len+1)*sizeof(unsigned int) - p)
512                 count = (prof_len+1)*sizeof(unsigned int) - p;
513         read = 0;
514
515         while (p < sizeof(unsigned int) && count > 0) {
516                 put_user(*((char *)(&sample_step)+p),buf);
517                 buf++; p++; count--; read++;
518         }
519         pnt = (char *)prof_buffer + p - sizeof(unsigned int);
520         if (copy_to_user(buf,(void *)pnt,count))
521                 return -EFAULT;
522         read += count;
523         *ppos = n + read;
524         return read;
525 }
526
527 /*
528  * Writing to /proc/profile resets the counters
529  *
530  * Writing a 'profiling multiplier' value into it also re-sets the profiling
531  * interrupt frequency, on architectures that support this.
532  */
533 static ssize_t write_profile(struct file * file, const char * buf,
534                              size_t count, loff_t *ppos)
535 {
536 #ifdef CONFIG_SMP
537         extern int setup_profiling_timer (unsigned int multiplier);
538
539         if (count==sizeof(int)) {
540                 unsigned int multiplier;
541
542                 if (copy_from_user(&multiplier, buf, sizeof(int)))
543                         return -EFAULT;
544
545                 if (setup_profiling_timer(multiplier))
546                         return -EINVAL;
547         }
548 #endif
549
550         memset(prof_buffer, 0, prof_len * sizeof(*prof_buffer));
551         return count;
552 }
553
554 static struct file_operations proc_profile_operations = {
555         read:           read_profile,
556         write:          write_profile,
557 };
558
559 #ifdef CONFIG_MAGIC_SYSRQ
560 /*
561  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
562  */
563 static ssize_t write_sysrq_trigger(struct file *file, const char *buf,
564                                      size_t count, loff_t *ppos)
565 {
566         if (count) {
567                 char c;
568
569                 if (get_user(c, buf))
570                         return -EFAULT;
571                 handle_sysrq(c, NULL, NULL, NULL);
572         }
573         return count;
574 }
575
576 static struct file_operations proc_sysrq_trigger_operations = {
577         .write          = write_sysrq_trigger,
578 };
579 #endif
580
581 struct proc_dir_entry *proc_root_kcore;
582
583 static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
584 {
585         struct proc_dir_entry *entry;
586         entry = create_proc_entry(name, mode, NULL);
587         if (entry)
588                 entry->proc_fops = f;
589 }
590
591 void __init proc_misc_init(void)
592 {
593         struct proc_dir_entry *entry;
594         static struct {
595                 char *name;
596                 int (*read_proc)(char*,char**,off_t,int,int*,void*);
597         } *p, simple_ones[] = {
598                 {"loadavg",     loadavg_read_proc},
599                 {"uptime",      uptime_read_proc},
600                 {"meminfo",     meminfo_read_proc},
601                 {"version",     version_read_proc},
602 #ifdef CONFIG_PROC_HARDWARE
603                 {"hardware",    hardware_read_proc},
604 #endif
605 #ifdef CONFIG_STRAM_PROC
606                 {"stram",       stram_read_proc},
607 #endif
608 #ifdef CONFIG_MODULES
609                 {"modules",     modules_read_proc},
610 #endif
611                 {"stat",        kstat_read_proc},
612                 {"devices",     devices_read_proc},
613 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_X86)
614                 {"interrupts",  interrupts_read_proc},
615 #endif
616                 {"filesystems", filesystems_read_proc},
617                 {"dma",         dma_read_proc},
618                 {"cmdline",     cmdline_read_proc},
619 #ifdef CONFIG_SGI_DS1286
620                 {"rtc",         ds1286_read_proc},
621 #endif
622                 {"locks",       locks_read_proc},
623                 {"swaps",       swaps_read_proc},
624                 {"execdomains", execdomains_read_proc},
625                 {NULL,}
626         };
627         for (p = simple_ones; p->name; p++)
628                 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
629
630         proc_symlink("mounts", NULL, "self/mounts");
631
632         /* And now for trickier ones */
633         entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
634         if (entry)
635                 entry->proc_fops = &proc_kmsg_operations;
636         create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
637 #if defined(CONFIG_X86)
638         create_seq_entry("interrupts", 0, &proc_interrupts_operations);
639 #endif
640         create_seq_entry("ioports", 0, &proc_ioports_operations);
641         create_seq_entry("iomem", 0, &proc_iomem_operations);
642         create_seq_entry("partitions", 0, &proc_partitions_operations);
643         create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
644 #ifdef CONFIG_MODULES
645         create_seq_entry("ksyms", 0, &proc_ksyms_operations);
646 #endif
647         proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
648         if (proc_root_kcore) {
649                 proc_root_kcore->proc_fops = &proc_kcore_operations;
650                 proc_root_kcore->size =
651                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
652         }
653         if (prof_shift) {
654                 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
655                 if (entry) {
656                         entry->proc_fops = &proc_profile_operations;
657                         entry->size = (1+prof_len) * sizeof(unsigned int);
658                 }
659         }
660 #ifdef CONFIG_MAGIC_SYSRQ
661         entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
662         if (entry)
663                 entry->proc_fops = &proc_sysrq_trigger_operations;
664 #endif
665 #ifdef CONFIG_PPC32
666         {
667                 extern struct file_operations ppc_htab_operations;
668                 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
669                 if (entry)
670                         entry->proc_fops = &ppc_htab_operations;
671         }
672 #endif
673 }