900331a634ef570c0c159e5043dae9a99e8be970
[safe/jmp/linux-2.6] / 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/quicklist.h>
28 #include <linux/proc_fs.h>
29 #include <linux/ioport.h>
30 #include <linux/mm.h>
31 #include <linux/mmzone.h>
32 #include <linux/pagemap.h>
33 #include <linux/irq.h>
34 #include <linux/interrupt.h>
35 #include <linux/swap.h>
36 #include <linux/slab.h>
37 #include <linux/genhd.h>
38 #include <linux/smp.h>
39 #include <linux/signal.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/seq_file.h>
43 #include <linux/times.h>
44 #include <linux/profile.h>
45 #include <linux/utsname.h>
46 #include <linux/blkdev.h>
47 #include <linux/hugetlb.h>
48 #include <linux/jiffies.h>
49 #include <linux/vmalloc.h>
50 #include <linux/crash_dump.h>
51 #include <linux/pid_namespace.h>
52 #include <linux/bootmem.h>
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 static int zoneinfo_open(struct inode *inode, struct file *file)
61 {
62         return seq_open(file, &zoneinfo_op);
63 }
64
65 static const struct file_operations proc_zoneinfo_file_operations = {
66         .open           = zoneinfo_open,
67         .read           = seq_read,
68         .llseek         = seq_lseek,
69         .release        = seq_release,
70 };
71
72 static int vmstat_open(struct inode *inode, struct file *file)
73 {
74         return seq_open(file, &vmstat_op);
75 }
76 static const struct file_operations proc_vmstat_file_operations = {
77         .open           = vmstat_open,
78         .read           = seq_read,
79         .llseek         = seq_lseek,
80         .release        = seq_release,
81 };
82
83 #ifdef CONFIG_BLOCK
84 static int diskstats_open(struct inode *inode, struct file *file)
85 {
86         return seq_open(file, &diskstats_op);
87 }
88 static const struct file_operations proc_diskstats_operations = {
89         .open           = diskstats_open,
90         .read           = seq_read,
91         .llseek         = seq_lseek,
92         .release        = seq_release,
93 };
94 #endif
95
96 #ifdef CONFIG_MODULES
97 extern const struct seq_operations modules_op;
98 static int modules_open(struct inode *inode, struct file *file)
99 {
100         return seq_open(file, &modules_op);
101 }
102 static const struct file_operations proc_modules_operations = {
103         .open           = modules_open,
104         .read           = seq_read,
105         .llseek         = seq_lseek,
106         .release        = seq_release,
107 };
108 #endif
109
110 #ifdef CONFIG_PROC_PAGE_MONITOR
111 #define KPMSIZE sizeof(u64)
112 #define KPMMASK (KPMSIZE - 1)
113 /* /proc/kpagecount - an array exposing page counts
114  *
115  * Each entry is a u64 representing the corresponding
116  * physical page count.
117  */
118 static ssize_t kpagecount_read(struct file *file, char __user *buf,
119                              size_t count, loff_t *ppos)
120 {
121         u64 __user *out = (u64 __user *)buf;
122         struct page *ppage;
123         unsigned long src = *ppos;
124         unsigned long pfn;
125         ssize_t ret = 0;
126         u64 pcount;
127
128         pfn = src / KPMSIZE;
129         count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
130         if (src & KPMMASK || count & KPMMASK)
131                 return -EINVAL;
132
133         while (count > 0) {
134                 ppage = NULL;
135                 if (pfn_valid(pfn))
136                         ppage = pfn_to_page(pfn);
137                 pfn++;
138                 if (!ppage)
139                         pcount = 0;
140                 else
141                         pcount = page_mapcount(ppage);
142
143                 if (put_user(pcount, out++)) {
144                         ret = -EFAULT;
145                         break;
146                 }
147
148                 count -= KPMSIZE;
149         }
150
151         *ppos += (char __user *)out - buf;
152         if (!ret)
153                 ret = (char __user *)out - buf;
154         return ret;
155 }
156
157 static struct file_operations proc_kpagecount_operations = {
158         .llseek = mem_lseek,
159         .read = kpagecount_read,
160 };
161
162 /* /proc/kpageflags - an array exposing page flags
163  *
164  * Each entry is a u64 representing the corresponding
165  * physical page flags.
166  */
167
168 /* These macros are used to decouple internal flags from exported ones */
169
170 #define KPF_LOCKED     0
171 #define KPF_ERROR      1
172 #define KPF_REFERENCED 2
173 #define KPF_UPTODATE   3
174 #define KPF_DIRTY      4
175 #define KPF_LRU        5
176 #define KPF_ACTIVE     6
177 #define KPF_SLAB       7
178 #define KPF_WRITEBACK  8
179 #define KPF_RECLAIM    9
180 #define KPF_BUDDY     10
181
182 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
183
184 static ssize_t kpageflags_read(struct file *file, char __user *buf,
185                              size_t count, loff_t *ppos)
186 {
187         u64 __user *out = (u64 __user *)buf;
188         struct page *ppage;
189         unsigned long src = *ppos;
190         unsigned long pfn;
191         ssize_t ret = 0;
192         u64 kflags, uflags;
193
194         pfn = src / KPMSIZE;
195         count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
196         if (src & KPMMASK || count & KPMMASK)
197                 return -EINVAL;
198
199         while (count > 0) {
200                 ppage = NULL;
201                 if (pfn_valid(pfn))
202                         ppage = pfn_to_page(pfn);
203                 pfn++;
204                 if (!ppage)
205                         kflags = 0;
206                 else
207                         kflags = ppage->flags;
208
209                 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
210                         kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
211                         kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
212                         kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
213                         kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
214                         kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
215                         kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
216                         kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
217                         kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
218                         kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
219                         kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
220
221                 if (put_user(uflags, out++)) {
222                         ret = -EFAULT;
223                         break;
224                 }
225
226                 count -= KPMSIZE;
227         }
228
229         *ppos += (char __user *)out - buf;
230         if (!ret)
231                 ret = (char __user *)out - buf;
232         return ret;
233 }
234
235 static struct file_operations proc_kpageflags_operations = {
236         .llseek = mem_lseek,
237         .read = kpageflags_read,
238 };
239 #endif /* CONFIG_PROC_PAGE_MONITOR */
240
241 struct proc_dir_entry *proc_root_kcore;
242
243 void __init proc_misc_init(void)
244 {
245         proc_symlink("mounts", NULL, "self/mounts");
246
247         /* And now for trickier ones */
248         proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
249         proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
250 #ifdef CONFIG_BLOCK
251         proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
252 #endif
253 #ifdef CONFIG_MODULES
254         proc_create("modules", 0, NULL, &proc_modules_operations);
255 #endif
256 #ifdef CONFIG_SCHEDSTATS
257         proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
258 #endif
259 #ifdef CONFIG_PROC_KCORE
260         proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
261         if (proc_root_kcore)
262                 proc_root_kcore->size =
263                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
264 #endif
265 #ifdef CONFIG_PROC_PAGE_MONITOR
266         proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
267         proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
268 #endif
269 #ifdef CONFIG_PROC_VMCORE
270         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
271 #endif
272 }