proc: move /proc/interrupts boilerplate code to fs/proc/interrupts.c
[pandora-kernel.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/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 fragmentation_open(struct inode *inode, struct file *file)
61 {
62         (void)inode;
63         return seq_open(file, &fragmentation_op);
64 }
65
66 static const struct file_operations fragmentation_file_operations = {
67         .open           = fragmentation_open,
68         .read           = seq_read,
69         .llseek         = seq_lseek,
70         .release        = seq_release,
71 };
72
73 static int pagetypeinfo_open(struct inode *inode, struct file *file)
74 {
75         return seq_open(file, &pagetypeinfo_op);
76 }
77
78 static const struct file_operations pagetypeinfo_file_ops = {
79         .open           = pagetypeinfo_open,
80         .read           = seq_read,
81         .llseek         = seq_lseek,
82         .release        = seq_release,
83 };
84
85 static int zoneinfo_open(struct inode *inode, struct file *file)
86 {
87         return seq_open(file, &zoneinfo_op);
88 }
89
90 static const struct file_operations proc_zoneinfo_file_operations = {
91         .open           = zoneinfo_open,
92         .read           = seq_read,
93         .llseek         = seq_lseek,
94         .release        = seq_release,
95 };
96
97 static int vmstat_open(struct inode *inode, struct file *file)
98 {
99         return seq_open(file, &vmstat_op);
100 }
101 static const struct file_operations proc_vmstat_file_operations = {
102         .open           = vmstat_open,
103         .read           = seq_read,
104         .llseek         = seq_lseek,
105         .release        = seq_release,
106 };
107
108 #ifdef CONFIG_BLOCK
109 static int diskstats_open(struct inode *inode, struct file *file)
110 {
111         return seq_open(file, &diskstats_op);
112 }
113 static const struct file_operations proc_diskstats_operations = {
114         .open           = diskstats_open,
115         .read           = seq_read,
116         .llseek         = seq_lseek,
117         .release        = seq_release,
118 };
119 #endif
120
121 #ifdef CONFIG_MODULES
122 extern const struct seq_operations modules_op;
123 static int modules_open(struct inode *inode, struct file *file)
124 {
125         return seq_open(file, &modules_op);
126 }
127 static const struct file_operations proc_modules_operations = {
128         .open           = modules_open,
129         .read           = seq_read,
130         .llseek         = seq_lseek,
131         .release        = seq_release,
132 };
133 #endif
134
135 #ifdef CONFIG_SLABINFO
136 static int slabinfo_open(struct inode *inode, struct file *file)
137 {
138         return seq_open(file, &slabinfo_op);
139 }
140 static const struct file_operations proc_slabinfo_operations = {
141         .open           = slabinfo_open,
142         .read           = seq_read,
143         .write          = slabinfo_write,
144         .llseek         = seq_lseek,
145         .release        = seq_release,
146 };
147
148 #ifdef CONFIG_DEBUG_SLAB_LEAK
149 extern const struct seq_operations slabstats_op;
150 static int slabstats_open(struct inode *inode, struct file *file)
151 {
152         unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
153         int ret = -ENOMEM;
154         if (n) {
155                 ret = seq_open(file, &slabstats_op);
156                 if (!ret) {
157                         struct seq_file *m = file->private_data;
158                         *n = PAGE_SIZE / (2 * sizeof(unsigned long));
159                         m->private = n;
160                         n = NULL;
161                 }
162                 kfree(n);
163         }
164         return ret;
165 }
166
167 static const struct file_operations proc_slabstats_operations = {
168         .open           = slabstats_open,
169         .read           = seq_read,
170         .llseek         = seq_lseek,
171         .release        = seq_release_private,
172 };
173 #endif
174 #endif
175
176 #ifdef CONFIG_MMU
177 static int vmalloc_open(struct inode *inode, struct file *file)
178 {
179         unsigned int *ptr = NULL;
180         int ret;
181
182         if (NUMA_BUILD)
183                 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
184         ret = seq_open(file, &vmalloc_op);
185         if (!ret) {
186                 struct seq_file *m = file->private_data;
187                 m->private = ptr;
188         } else
189                 kfree(ptr);
190         return ret;
191 }
192
193 static const struct file_operations proc_vmalloc_operations = {
194         .open           = vmalloc_open,
195         .read           = seq_read,
196         .llseek         = seq_lseek,
197         .release        = seq_release_private,
198 };
199 #endif
200
201 #ifdef CONFIG_PROC_PAGE_MONITOR
202 #define KPMSIZE sizeof(u64)
203 #define KPMMASK (KPMSIZE - 1)
204 /* /proc/kpagecount - an array exposing page counts
205  *
206  * Each entry is a u64 representing the corresponding
207  * physical page count.
208  */
209 static ssize_t kpagecount_read(struct file *file, char __user *buf,
210                              size_t count, loff_t *ppos)
211 {
212         u64 __user *out = (u64 __user *)buf;
213         struct page *ppage;
214         unsigned long src = *ppos;
215         unsigned long pfn;
216         ssize_t ret = 0;
217         u64 pcount;
218
219         pfn = src / KPMSIZE;
220         count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
221         if (src & KPMMASK || count & KPMMASK)
222                 return -EINVAL;
223
224         while (count > 0) {
225                 ppage = NULL;
226                 if (pfn_valid(pfn))
227                         ppage = pfn_to_page(pfn);
228                 pfn++;
229                 if (!ppage)
230                         pcount = 0;
231                 else
232                         pcount = page_mapcount(ppage);
233
234                 if (put_user(pcount, out++)) {
235                         ret = -EFAULT;
236                         break;
237                 }
238
239                 count -= KPMSIZE;
240         }
241
242         *ppos += (char __user *)out - buf;
243         if (!ret)
244                 ret = (char __user *)out - buf;
245         return ret;
246 }
247
248 static struct file_operations proc_kpagecount_operations = {
249         .llseek = mem_lseek,
250         .read = kpagecount_read,
251 };
252
253 /* /proc/kpageflags - an array exposing page flags
254  *
255  * Each entry is a u64 representing the corresponding
256  * physical page flags.
257  */
258
259 /* These macros are used to decouple internal flags from exported ones */
260
261 #define KPF_LOCKED     0
262 #define KPF_ERROR      1
263 #define KPF_REFERENCED 2
264 #define KPF_UPTODATE   3
265 #define KPF_DIRTY      4
266 #define KPF_LRU        5
267 #define KPF_ACTIVE     6
268 #define KPF_SLAB       7
269 #define KPF_WRITEBACK  8
270 #define KPF_RECLAIM    9
271 #define KPF_BUDDY     10
272
273 #define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)
274
275 static ssize_t kpageflags_read(struct file *file, char __user *buf,
276                              size_t count, loff_t *ppos)
277 {
278         u64 __user *out = (u64 __user *)buf;
279         struct page *ppage;
280         unsigned long src = *ppos;
281         unsigned long pfn;
282         ssize_t ret = 0;
283         u64 kflags, uflags;
284
285         pfn = src / KPMSIZE;
286         count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
287         if (src & KPMMASK || count & KPMMASK)
288                 return -EINVAL;
289
290         while (count > 0) {
291                 ppage = NULL;
292                 if (pfn_valid(pfn))
293                         ppage = pfn_to_page(pfn);
294                 pfn++;
295                 if (!ppage)
296                         kflags = 0;
297                 else
298                         kflags = ppage->flags;
299
300                 uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
301                         kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
302                         kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
303                         kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
304                         kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
305                         kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
306                         kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
307                         kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
308                         kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
309                         kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
310                         kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
311
312                 if (put_user(uflags, out++)) {
313                         ret = -EFAULT;
314                         break;
315                 }
316
317                 count -= KPMSIZE;
318         }
319
320         *ppos += (char __user *)out - buf;
321         if (!ret)
322                 ret = (char __user *)out - buf;
323         return ret;
324 }
325
326 static struct file_operations proc_kpageflags_operations = {
327         .llseek = mem_lseek,
328         .read = kpageflags_read,
329 };
330 #endif /* CONFIG_PROC_PAGE_MONITOR */
331
332 struct proc_dir_entry *proc_root_kcore;
333
334 void __init proc_misc_init(void)
335 {
336         proc_symlink("mounts", NULL, "self/mounts");
337
338         /* And now for trickier ones */
339 #ifdef CONFIG_SLABINFO
340         proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
341 #ifdef CONFIG_DEBUG_SLAB_LEAK
342         proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
343 #endif
344 #endif
345 #ifdef CONFIG_MMU
346         proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
347 #endif
348         proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
349         proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
350         proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
351         proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
352 #ifdef CONFIG_BLOCK
353         proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
354 #endif
355 #ifdef CONFIG_MODULES
356         proc_create("modules", 0, NULL, &proc_modules_operations);
357 #endif
358 #ifdef CONFIG_SCHEDSTATS
359         proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
360 #endif
361 #ifdef CONFIG_PROC_KCORE
362         proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
363         if (proc_root_kcore)
364                 proc_root_kcore->size =
365                                 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
366 #endif
367 #ifdef CONFIG_PROC_PAGE_MONITOR
368         proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
369         proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
370 #endif
371 #ifdef CONFIG_PROC_VMCORE
372         proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
373 #endif
374 }