sched: speed up and simplify vslice calculations
[safe/jmp/linux-2.6] / kernel / sysctl.c
1 /*
2  * sysctl.c: General linux system control interface
3  *
4  * Begun 24 March 1995, Stephen Tweedie
5  * Added /proc support, Dec 1995
6  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9  * Dynamic registration fixes, Stephen Tweedie.
10  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12  *  Horn.
13  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16  *  Wendling.
17  * The list_for_each() macro wasn't appropriate for the sysctl loop.
18  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
19  */
20
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/swap.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/proc_fs.h>
27 #include <linux/capability.h>
28 #include <linux/ctype.h>
29 #include <linux/utsname.h>
30 #include <linux/smp_lock.h>
31 #include <linux/fs.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/kobject.h>
35 #include <linux/net.h>
36 #include <linux/sysrq.h>
37 #include <linux/highuid.h>
38 #include <linux/writeback.h>
39 #include <linux/hugetlb.h>
40 #include <linux/security.h>
41 #include <linux/initrd.h>
42 #include <linux/times.h>
43 #include <linux/limits.h>
44 #include <linux/dcache.h>
45 #include <linux/syscalls.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/acpi.h>
48 #include <linux/reboot.h>
49
50 #include <asm/uaccess.h>
51 #include <asm/processor.h>
52
53 #ifdef CONFIG_X86
54 #include <asm/nmi.h>
55 #include <asm/stacktrace.h>
56 #endif
57
58 #if defined(CONFIG_SYSCTL)
59
60 /* External variables not in a header file. */
61 extern int C_A_D;
62 extern int print_fatal_signals;
63 extern int sysctl_overcommit_memory;
64 extern int sysctl_overcommit_ratio;
65 extern int sysctl_panic_on_oom;
66 extern int max_threads;
67 extern int core_uses_pid;
68 extern int suid_dumpable;
69 extern char core_pattern[];
70 extern int pid_max;
71 extern int min_free_kbytes;
72 extern int printk_ratelimit_jiffies;
73 extern int printk_ratelimit_burst;
74 extern int pid_max_min, pid_max_max;
75 extern int sysctl_drop_caches;
76 extern int percpu_pagelist_fraction;
77 extern int compat_log;
78 extern int maps_protect;
79 extern int sysctl_stat_interval;
80 extern int audit_argv_kb;
81
82 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
83 static int maxolduid = 65535;
84 static int minolduid;
85 static int min_percpu_pagelist_fract = 8;
86
87 static int ngroups_max = NGROUPS_MAX;
88
89 #ifdef CONFIG_KMOD
90 extern char modprobe_path[];
91 #endif
92 #ifdef CONFIG_CHR_DEV_SG
93 extern int sg_big_buff;
94 #endif
95
96 #ifdef __sparc__
97 extern char reboot_command [];
98 extern int stop_a_enabled;
99 extern int scons_pwroff;
100 #endif
101
102 #ifdef __hppa__
103 extern int pwrsw_enabled;
104 extern int unaligned_enabled;
105 #endif
106
107 #ifdef CONFIG_S390
108 #ifdef CONFIG_MATHEMU
109 extern int sysctl_ieee_emulation_warnings;
110 #endif
111 extern int sysctl_userprocess_debug;
112 extern int spin_retry;
113 #endif
114
115 extern int sysctl_hz_timer;
116
117 #ifdef CONFIG_BSD_PROCESS_ACCT
118 extern int acct_parm[];
119 #endif
120
121 #ifdef CONFIG_IA64
122 extern int no_unaligned_warning;
123 #endif
124
125 #ifdef CONFIG_RT_MUTEXES
126 extern int max_lock_depth;
127 #endif
128
129 #ifdef CONFIG_SYSCTL_SYSCALL
130 static int parse_table(int __user *, int, void __user *, size_t __user *,
131                 void __user *, size_t, ctl_table *);
132 #endif
133
134
135 #ifdef CONFIG_PROC_SYSCTL
136 static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
137                   void __user *buffer, size_t *lenp, loff_t *ppos);
138 static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
139                                void __user *buffer, size_t *lenp, loff_t *ppos);
140 #endif
141
142 static ctl_table root_table[];
143 static struct ctl_table_header root_table_header =
144         { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
145
146 static ctl_table kern_table[];
147 static ctl_table vm_table[];
148 static ctl_table fs_table[];
149 static ctl_table debug_table[];
150 static ctl_table dev_table[];
151 extern ctl_table random_table[];
152 #ifdef CONFIG_UNIX98_PTYS
153 extern ctl_table pty_table[];
154 #endif
155 #ifdef CONFIG_INOTIFY_USER
156 extern ctl_table inotify_table[];
157 #endif
158
159 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
160 int sysctl_legacy_va_layout;
161 #endif
162
163 extern int prove_locking;
164 extern int lock_stat;
165
166 /* The default sysctl tables: */
167
168 static ctl_table root_table[] = {
169         {
170                 .ctl_name       = CTL_KERN,
171                 .procname       = "kernel",
172                 .mode           = 0555,
173                 .child          = kern_table,
174         },
175         {
176                 .ctl_name       = CTL_VM,
177                 .procname       = "vm",
178                 .mode           = 0555,
179                 .child          = vm_table,
180         },
181 #ifdef CONFIG_NET
182         {
183                 .ctl_name       = CTL_NET,
184                 .procname       = "net",
185                 .mode           = 0555,
186                 .child          = net_table,
187         },
188 #endif
189         {
190                 .ctl_name       = CTL_FS,
191                 .procname       = "fs",
192                 .mode           = 0555,
193                 .child          = fs_table,
194         },
195         {
196                 .ctl_name       = CTL_DEBUG,
197                 .procname       = "debug",
198                 .mode           = 0555,
199                 .child          = debug_table,
200         },
201         {
202                 .ctl_name       = CTL_DEV,
203                 .procname       = "dev",
204                 .mode           = 0555,
205                 .child          = dev_table,
206         },
207 /*
208  * NOTE: do not add new entries to this table unless you have read
209  * Documentation/sysctl/ctl_unnumbered.txt
210  */
211         { .ctl_name = 0 }
212 };
213
214 #ifdef CONFIG_SCHED_DEBUG
215 static unsigned long min_sched_granularity_ns = 100000;         /* 100 usecs */
216 static unsigned long max_sched_granularity_ns = 1000000000;     /* 1 second */
217 static unsigned long min_wakeup_granularity_ns;                 /* 0 usecs */
218 static unsigned long max_wakeup_granularity_ns = 1000000000;    /* 1 second */
219 #endif
220
221 static ctl_table kern_table[] = {
222 #ifdef CONFIG_SCHED_DEBUG
223         {
224                 .ctl_name       = CTL_UNNUMBERED,
225                 .procname       = "sched_nr_latency",
226                 .data           = &sysctl_sched_nr_latency,
227                 .maxlen         = sizeof(unsigned int),
228                 .mode           = 0644,
229                 .proc_handler   = &proc_dointvec,
230         },
231         {
232                 .ctl_name       = CTL_UNNUMBERED,
233                 .procname       = "sched_latency_ns",
234                 .data           = &sysctl_sched_latency,
235                 .maxlen         = sizeof(unsigned int),
236                 .mode           = 0644,
237                 .proc_handler   = &proc_dointvec_minmax,
238                 .strategy       = &sysctl_intvec,
239                 .extra1         = &min_sched_granularity_ns,
240                 .extra2         = &max_sched_granularity_ns,
241         },
242         {
243                 .ctl_name       = CTL_UNNUMBERED,
244                 .procname       = "sched_wakeup_granularity_ns",
245                 .data           = &sysctl_sched_wakeup_granularity,
246                 .maxlen         = sizeof(unsigned int),
247                 .mode           = 0644,
248                 .proc_handler   = &proc_dointvec_minmax,
249                 .strategy       = &sysctl_intvec,
250                 .extra1         = &min_wakeup_granularity_ns,
251                 .extra2         = &max_wakeup_granularity_ns,
252         },
253         {
254                 .ctl_name       = CTL_UNNUMBERED,
255                 .procname       = "sched_batch_wakeup_granularity_ns",
256                 .data           = &sysctl_sched_batch_wakeup_granularity,
257                 .maxlen         = sizeof(unsigned int),
258                 .mode           = 0644,
259                 .proc_handler   = &proc_dointvec_minmax,
260                 .strategy       = &sysctl_intvec,
261                 .extra1         = &min_wakeup_granularity_ns,
262                 .extra2         = &max_wakeup_granularity_ns,
263         },
264         {
265                 .ctl_name       = CTL_UNNUMBERED,
266                 .procname       = "sched_child_runs_first",
267                 .data           = &sysctl_sched_child_runs_first,
268                 .maxlen         = sizeof(unsigned int),
269                 .mode           = 0644,
270                 .proc_handler   = &proc_dointvec,
271         },
272         {
273                 .ctl_name       = CTL_UNNUMBERED,
274                 .procname       = "sched_features",
275                 .data           = &sysctl_sched_features,
276                 .maxlen         = sizeof(unsigned int),
277                 .mode           = 0644,
278                 .proc_handler   = &proc_dointvec,
279         },
280 #endif
281         {
282                 .ctl_name       = CTL_UNNUMBERED,
283                 .procname       = "sched_compat_yield",
284                 .data           = &sysctl_sched_compat_yield,
285                 .maxlen         = sizeof(unsigned int),
286                 .mode           = 0644,
287                 .proc_handler   = &proc_dointvec,
288         },
289 #ifdef CONFIG_PROVE_LOCKING
290         {
291                 .ctl_name       = CTL_UNNUMBERED,
292                 .procname       = "prove_locking",
293                 .data           = &prove_locking,
294                 .maxlen         = sizeof(int),
295                 .mode           = 0644,
296                 .proc_handler   = &proc_dointvec,
297         },
298 #endif
299 #ifdef CONFIG_LOCK_STAT
300         {
301                 .ctl_name       = CTL_UNNUMBERED,
302                 .procname       = "lock_stat",
303                 .data           = &lock_stat,
304                 .maxlen         = sizeof(int),
305                 .mode           = 0644,
306                 .proc_handler   = &proc_dointvec,
307         },
308 #endif
309         {
310                 .ctl_name       = KERN_PANIC,
311                 .procname       = "panic",
312                 .data           = &panic_timeout,
313                 .maxlen         = sizeof(int),
314                 .mode           = 0644,
315                 .proc_handler   = &proc_dointvec,
316         },
317         {
318                 .ctl_name       = KERN_CORE_USES_PID,
319                 .procname       = "core_uses_pid",
320                 .data           = &core_uses_pid,
321                 .maxlen         = sizeof(int),
322                 .mode           = 0644,
323                 .proc_handler   = &proc_dointvec,
324         },
325 #ifdef CONFIG_AUDITSYSCALL
326         {
327                 .ctl_name       = CTL_UNNUMBERED,
328                 .procname       = "audit_argv_kb",
329                 .data           = &audit_argv_kb,
330                 .maxlen         = sizeof(int),
331                 .mode           = 0644,
332                 .proc_handler   = &proc_dointvec,
333         },
334 #endif
335         {
336                 .ctl_name       = KERN_CORE_PATTERN,
337                 .procname       = "core_pattern",
338                 .data           = core_pattern,
339                 .maxlen         = CORENAME_MAX_SIZE,
340                 .mode           = 0644,
341                 .proc_handler   = &proc_dostring,
342                 .strategy       = &sysctl_string,
343         },
344 #ifdef CONFIG_PROC_SYSCTL
345         {
346                 .ctl_name       = KERN_TAINTED,
347                 .procname       = "tainted",
348                 .data           = &tainted,
349                 .maxlen         = sizeof(int),
350                 .mode           = 0644,
351                 .proc_handler   = &proc_dointvec_taint,
352         },
353 #endif
354         {
355                 .ctl_name       = KERN_CAP_BSET,
356                 .procname       = "cap-bound",
357                 .data           = &cap_bset,
358                 .maxlen         = sizeof(kernel_cap_t),
359                 .mode           = 0600,
360                 .proc_handler   = &proc_dointvec_bset,
361         },
362 #ifdef CONFIG_BLK_DEV_INITRD
363         {
364                 .ctl_name       = KERN_REALROOTDEV,
365                 .procname       = "real-root-dev",
366                 .data           = &real_root_dev,
367                 .maxlen         = sizeof(int),
368                 .mode           = 0644,
369                 .proc_handler   = &proc_dointvec,
370         },
371 #endif
372         {
373                 .ctl_name       = CTL_UNNUMBERED,
374                 .procname       = "print-fatal-signals",
375                 .data           = &print_fatal_signals,
376                 .maxlen         = sizeof(int),
377                 .mode           = 0644,
378                 .proc_handler   = &proc_dointvec,
379         },
380 #ifdef __sparc__
381         {
382                 .ctl_name       = KERN_SPARC_REBOOT,
383                 .procname       = "reboot-cmd",
384                 .data           = reboot_command,
385                 .maxlen         = 256,
386                 .mode           = 0644,
387                 .proc_handler   = &proc_dostring,
388                 .strategy       = &sysctl_string,
389         },
390         {
391                 .ctl_name       = KERN_SPARC_STOP_A,
392                 .procname       = "stop-a",
393                 .data           = &stop_a_enabled,
394                 .maxlen         = sizeof (int),
395                 .mode           = 0644,
396                 .proc_handler   = &proc_dointvec,
397         },
398         {
399                 .ctl_name       = KERN_SPARC_SCONS_PWROFF,
400                 .procname       = "scons-poweroff",
401                 .data           = &scons_pwroff,
402                 .maxlen         = sizeof (int),
403                 .mode           = 0644,
404                 .proc_handler   = &proc_dointvec,
405         },
406 #endif
407 #ifdef __hppa__
408         {
409                 .ctl_name       = KERN_HPPA_PWRSW,
410                 .procname       = "soft-power",
411                 .data           = &pwrsw_enabled,
412                 .maxlen         = sizeof (int),
413                 .mode           = 0644,
414                 .proc_handler   = &proc_dointvec,
415         },
416         {
417                 .ctl_name       = KERN_HPPA_UNALIGNED,
418                 .procname       = "unaligned-trap",
419                 .data           = &unaligned_enabled,
420                 .maxlen         = sizeof (int),
421                 .mode           = 0644,
422                 .proc_handler   = &proc_dointvec,
423         },
424 #endif
425         {
426                 .ctl_name       = KERN_CTLALTDEL,
427                 .procname       = "ctrl-alt-del",
428                 .data           = &C_A_D,
429                 .maxlen         = sizeof(int),
430                 .mode           = 0644,
431                 .proc_handler   = &proc_dointvec,
432         },
433         {
434                 .ctl_name       = KERN_PRINTK,
435                 .procname       = "printk",
436                 .data           = &console_loglevel,
437                 .maxlen         = 4*sizeof(int),
438                 .mode           = 0644,
439                 .proc_handler   = &proc_dointvec,
440         },
441 #ifdef CONFIG_KMOD
442         {
443                 .ctl_name       = KERN_MODPROBE,
444                 .procname       = "modprobe",
445                 .data           = &modprobe_path,
446                 .maxlen         = KMOD_PATH_LEN,
447                 .mode           = 0644,
448                 .proc_handler   = &proc_dostring,
449                 .strategy       = &sysctl_string,
450         },
451 #endif
452 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
453         {
454                 .ctl_name       = KERN_HOTPLUG,
455                 .procname       = "hotplug",
456                 .data           = &uevent_helper,
457                 .maxlen         = UEVENT_HELPER_PATH_LEN,
458                 .mode           = 0644,
459                 .proc_handler   = &proc_dostring,
460                 .strategy       = &sysctl_string,
461         },
462 #endif
463 #ifdef CONFIG_CHR_DEV_SG
464         {
465                 .ctl_name       = KERN_SG_BIG_BUFF,
466                 .procname       = "sg-big-buff",
467                 .data           = &sg_big_buff,
468                 .maxlen         = sizeof (int),
469                 .mode           = 0444,
470                 .proc_handler   = &proc_dointvec,
471         },
472 #endif
473 #ifdef CONFIG_BSD_PROCESS_ACCT
474         {
475                 .ctl_name       = KERN_ACCT,
476                 .procname       = "acct",
477                 .data           = &acct_parm,
478                 .maxlen         = 3*sizeof(int),
479                 .mode           = 0644,
480                 .proc_handler   = &proc_dointvec,
481         },
482 #endif
483 #ifdef CONFIG_MAGIC_SYSRQ
484         {
485                 .ctl_name       = KERN_SYSRQ,
486                 .procname       = "sysrq",
487                 .data           = &__sysrq_enabled,
488                 .maxlen         = sizeof (int),
489                 .mode           = 0644,
490                 .proc_handler   = &proc_dointvec,
491         },
492 #endif
493 #ifdef CONFIG_PROC_SYSCTL
494         {
495                 .ctl_name       = KERN_CADPID,
496                 .procname       = "cad_pid",
497                 .data           = NULL,
498                 .maxlen         = sizeof (int),
499                 .mode           = 0600,
500                 .proc_handler   = &proc_do_cad_pid,
501         },
502 #endif
503         {
504                 .ctl_name       = KERN_MAX_THREADS,
505                 .procname       = "threads-max",
506                 .data           = &max_threads,
507                 .maxlen         = sizeof(int),
508                 .mode           = 0644,
509                 .proc_handler   = &proc_dointvec,
510         },
511         {
512                 .ctl_name       = KERN_RANDOM,
513                 .procname       = "random",
514                 .mode           = 0555,
515                 .child          = random_table,
516         },
517 #ifdef CONFIG_UNIX98_PTYS
518         {
519                 .ctl_name       = KERN_PTY,
520                 .procname       = "pty",
521                 .mode           = 0555,
522                 .child          = pty_table,
523         },
524 #endif
525         {
526                 .ctl_name       = KERN_OVERFLOWUID,
527                 .procname       = "overflowuid",
528                 .data           = &overflowuid,
529                 .maxlen         = sizeof(int),
530                 .mode           = 0644,
531                 .proc_handler   = &proc_dointvec_minmax,
532                 .strategy       = &sysctl_intvec,
533                 .extra1         = &minolduid,
534                 .extra2         = &maxolduid,
535         },
536         {
537                 .ctl_name       = KERN_OVERFLOWGID,
538                 .procname       = "overflowgid",
539                 .data           = &overflowgid,
540                 .maxlen         = sizeof(int),
541                 .mode           = 0644,
542                 .proc_handler   = &proc_dointvec_minmax,
543                 .strategy       = &sysctl_intvec,
544                 .extra1         = &minolduid,
545                 .extra2         = &maxolduid,
546         },
547 #ifdef CONFIG_S390
548 #ifdef CONFIG_MATHEMU
549         {
550                 .ctl_name       = KERN_IEEE_EMULATION_WARNINGS,
551                 .procname       = "ieee_emulation_warnings",
552                 .data           = &sysctl_ieee_emulation_warnings,
553                 .maxlen         = sizeof(int),
554                 .mode           = 0644,
555                 .proc_handler   = &proc_dointvec,
556         },
557 #endif
558 #ifdef CONFIG_NO_IDLE_HZ
559         {
560                 .ctl_name       = KERN_HZ_TIMER,
561                 .procname       = "hz_timer",
562                 .data           = &sysctl_hz_timer,
563                 .maxlen         = sizeof(int),
564                 .mode           = 0644,
565                 .proc_handler   = &proc_dointvec,
566         },
567 #endif
568         {
569                 .ctl_name       = KERN_S390_USER_DEBUG_LOGGING,
570                 .procname       = "userprocess_debug",
571                 .data           = &sysctl_userprocess_debug,
572                 .maxlen         = sizeof(int),
573                 .mode           = 0644,
574                 .proc_handler   = &proc_dointvec,
575         },
576 #endif
577         {
578                 .ctl_name       = KERN_PIDMAX,
579                 .procname       = "pid_max",
580                 .data           = &pid_max,
581                 .maxlen         = sizeof (int),
582                 .mode           = 0644,
583                 .proc_handler   = &proc_dointvec_minmax,
584                 .strategy       = sysctl_intvec,
585                 .extra1         = &pid_max_min,
586                 .extra2         = &pid_max_max,
587         },
588         {
589                 .ctl_name       = KERN_PANIC_ON_OOPS,
590                 .procname       = "panic_on_oops",
591                 .data           = &panic_on_oops,
592                 .maxlen         = sizeof(int),
593                 .mode           = 0644,
594                 .proc_handler   = &proc_dointvec,
595         },
596         {
597                 .ctl_name       = KERN_PRINTK_RATELIMIT,
598                 .procname       = "printk_ratelimit",
599                 .data           = &printk_ratelimit_jiffies,
600                 .maxlen         = sizeof(int),
601                 .mode           = 0644,
602                 .proc_handler   = &proc_dointvec_jiffies,
603                 .strategy       = &sysctl_jiffies,
604         },
605         {
606                 .ctl_name       = KERN_PRINTK_RATELIMIT_BURST,
607                 .procname       = "printk_ratelimit_burst",
608                 .data           = &printk_ratelimit_burst,
609                 .maxlen         = sizeof(int),
610                 .mode           = 0644,
611                 .proc_handler   = &proc_dointvec,
612         },
613         {
614                 .ctl_name       = KERN_NGROUPS_MAX,
615                 .procname       = "ngroups_max",
616                 .data           = &ngroups_max,
617                 .maxlen         = sizeof (int),
618                 .mode           = 0444,
619                 .proc_handler   = &proc_dointvec,
620         },
621 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
622         {
623                 .ctl_name       = KERN_UNKNOWN_NMI_PANIC,
624                 .procname       = "unknown_nmi_panic",
625                 .data           = &unknown_nmi_panic,
626                 .maxlen         = sizeof (int),
627                 .mode           = 0644,
628                 .proc_handler   = &proc_dointvec,
629         },
630         {
631                 .ctl_name       = KERN_NMI_WATCHDOG,
632                 .procname       = "nmi_watchdog",
633                 .data           = &nmi_watchdog_enabled,
634                 .maxlen         = sizeof (int),
635                 .mode           = 0644,
636                 .proc_handler   = &proc_nmi_enabled,
637         },
638 #endif
639 #if defined(CONFIG_X86)
640         {
641                 .ctl_name       = KERN_PANIC_ON_NMI,
642                 .procname       = "panic_on_unrecovered_nmi",
643                 .data           = &panic_on_unrecovered_nmi,
644                 .maxlen         = sizeof(int),
645                 .mode           = 0644,
646                 .proc_handler   = &proc_dointvec,
647         },
648         {
649                 .ctl_name       = KERN_BOOTLOADER_TYPE,
650                 .procname       = "bootloader_type",
651                 .data           = &bootloader_type,
652                 .maxlen         = sizeof (int),
653                 .mode           = 0444,
654                 .proc_handler   = &proc_dointvec,
655         },
656         {
657                 .ctl_name       = CTL_UNNUMBERED,
658                 .procname       = "kstack_depth_to_print",
659                 .data           = &kstack_depth_to_print,
660                 .maxlen         = sizeof(int),
661                 .mode           = 0644,
662                 .proc_handler   = &proc_dointvec,
663         },
664 #endif
665 #if defined(CONFIG_MMU)
666         {
667                 .ctl_name       = KERN_RANDOMIZE,
668                 .procname       = "randomize_va_space",
669                 .data           = &randomize_va_space,
670                 .maxlen         = sizeof(int),
671                 .mode           = 0644,
672                 .proc_handler   = &proc_dointvec,
673         },
674 #endif
675 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
676         {
677                 .ctl_name       = KERN_SPIN_RETRY,
678                 .procname       = "spin_retry",
679                 .data           = &spin_retry,
680                 .maxlen         = sizeof (int),
681                 .mode           = 0644,
682                 .proc_handler   = &proc_dointvec,
683         },
684 #endif
685 #if     defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
686         {
687                 .ctl_name       = KERN_ACPI_VIDEO_FLAGS,
688                 .procname       = "acpi_video_flags",
689                 .data           = &acpi_realmode_flags,
690                 .maxlen         = sizeof (unsigned long),
691                 .mode           = 0644,
692                 .proc_handler   = &proc_doulongvec_minmax,
693         },
694 #endif
695 #ifdef CONFIG_IA64
696         {
697                 .ctl_name       = KERN_IA64_UNALIGNED,
698                 .procname       = "ignore-unaligned-usertrap",
699                 .data           = &no_unaligned_warning,
700                 .maxlen         = sizeof (int),
701                 .mode           = 0644,
702                 .proc_handler   = &proc_dointvec,
703         },
704 #endif
705 #ifdef CONFIG_COMPAT
706         {
707                 .ctl_name       = KERN_COMPAT_LOG,
708                 .procname       = "compat-log",
709                 .data           = &compat_log,
710                 .maxlen         = sizeof (int),
711                 .mode           = 0644,
712                 .proc_handler   = &proc_dointvec,
713         },
714 #endif
715 #ifdef CONFIG_RT_MUTEXES
716         {
717                 .ctl_name       = KERN_MAX_LOCK_DEPTH,
718                 .procname       = "max_lock_depth",
719                 .data           = &max_lock_depth,
720                 .maxlen         = sizeof(int),
721                 .mode           = 0644,
722                 .proc_handler   = &proc_dointvec,
723         },
724 #endif
725 #ifdef CONFIG_PROC_FS
726         {
727                 .ctl_name       = CTL_UNNUMBERED,
728                 .procname       = "maps_protect",
729                 .data           = &maps_protect,
730                 .maxlen         = sizeof(int),
731                 .mode           = 0644,
732                 .proc_handler   = &proc_dointvec,
733         },
734 #endif
735         {
736                 .ctl_name       = CTL_UNNUMBERED,
737                 .procname       = "poweroff_cmd",
738                 .data           = &poweroff_cmd,
739                 .maxlen         = POWEROFF_CMD_PATH_LEN,
740                 .mode           = 0644,
741                 .proc_handler   = &proc_dostring,
742                 .strategy       = &sysctl_string,
743         },
744 /*
745  * NOTE: do not add new entries to this table unless you have read
746  * Documentation/sysctl/ctl_unnumbered.txt
747  */
748         { .ctl_name = 0 }
749 };
750
751 /* Constants for minimum and maximum testing in vm_table.
752    We use these as one-element integer vectors. */
753 static int zero;
754 static int two = 2;
755 static int one_hundred = 100;
756
757
758 static ctl_table vm_table[] = {
759         {
760                 .ctl_name       = VM_OVERCOMMIT_MEMORY,
761                 .procname       = "overcommit_memory",
762                 .data           = &sysctl_overcommit_memory,
763                 .maxlen         = sizeof(sysctl_overcommit_memory),
764                 .mode           = 0644,
765                 .proc_handler   = &proc_dointvec,
766         },
767         {
768                 .ctl_name       = VM_PANIC_ON_OOM,
769                 .procname       = "panic_on_oom",
770                 .data           = &sysctl_panic_on_oom,
771                 .maxlen         = sizeof(sysctl_panic_on_oom),
772                 .mode           = 0644,
773                 .proc_handler   = &proc_dointvec,
774         },
775         {
776                 .ctl_name       = VM_OVERCOMMIT_RATIO,
777                 .procname       = "overcommit_ratio",
778                 .data           = &sysctl_overcommit_ratio,
779                 .maxlen         = sizeof(sysctl_overcommit_ratio),
780                 .mode           = 0644,
781                 .proc_handler   = &proc_dointvec,
782         },
783         {
784                 .ctl_name       = VM_PAGE_CLUSTER,
785                 .procname       = "page-cluster", 
786                 .data           = &page_cluster,
787                 .maxlen         = sizeof(int),
788                 .mode           = 0644,
789                 .proc_handler   = &proc_dointvec,
790         },
791         {
792                 .ctl_name       = VM_DIRTY_BACKGROUND,
793                 .procname       = "dirty_background_ratio",
794                 .data           = &dirty_background_ratio,
795                 .maxlen         = sizeof(dirty_background_ratio),
796                 .mode           = 0644,
797                 .proc_handler   = &proc_dointvec_minmax,
798                 .strategy       = &sysctl_intvec,
799                 .extra1         = &zero,
800                 .extra2         = &one_hundred,
801         },
802         {
803                 .ctl_name       = VM_DIRTY_RATIO,
804                 .procname       = "dirty_ratio",
805                 .data           = &vm_dirty_ratio,
806                 .maxlen         = sizeof(vm_dirty_ratio),
807                 .mode           = 0644,
808                 .proc_handler   = &proc_dointvec_minmax,
809                 .strategy       = &sysctl_intvec,
810                 .extra1         = &zero,
811                 .extra2         = &one_hundred,
812         },
813         {
814                 .ctl_name       = VM_DIRTY_WB_CS,
815                 .procname       = "dirty_writeback_centisecs",
816                 .data           = &dirty_writeback_interval,
817                 .maxlen         = sizeof(dirty_writeback_interval),
818                 .mode           = 0644,
819                 .proc_handler   = &dirty_writeback_centisecs_handler,
820         },
821         {
822                 .ctl_name       = VM_DIRTY_EXPIRE_CS,
823                 .procname       = "dirty_expire_centisecs",
824                 .data           = &dirty_expire_interval,
825                 .maxlen         = sizeof(dirty_expire_interval),
826                 .mode           = 0644,
827                 .proc_handler   = &proc_dointvec_userhz_jiffies,
828         },
829         {
830                 .ctl_name       = VM_NR_PDFLUSH_THREADS,
831                 .procname       = "nr_pdflush_threads",
832                 .data           = &nr_pdflush_threads,
833                 .maxlen         = sizeof nr_pdflush_threads,
834                 .mode           = 0444 /* read-only*/,
835                 .proc_handler   = &proc_dointvec,
836         },
837         {
838                 .ctl_name       = VM_SWAPPINESS,
839                 .procname       = "swappiness",
840                 .data           = &vm_swappiness,
841                 .maxlen         = sizeof(vm_swappiness),
842                 .mode           = 0644,
843                 .proc_handler   = &proc_dointvec_minmax,
844                 .strategy       = &sysctl_intvec,
845                 .extra1         = &zero,
846                 .extra2         = &one_hundred,
847         },
848 #ifdef CONFIG_HUGETLB_PAGE
849          {
850                 .ctl_name       = VM_HUGETLB_PAGES,
851                 .procname       = "nr_hugepages",
852                 .data           = &max_huge_pages,
853                 .maxlen         = sizeof(unsigned long),
854                 .mode           = 0644,
855                 .proc_handler   = &hugetlb_sysctl_handler,
856                 .extra1         = (void *)&hugetlb_zero,
857                 .extra2         = (void *)&hugetlb_infinity,
858          },
859          {
860                 .ctl_name       = VM_HUGETLB_GROUP,
861                 .procname       = "hugetlb_shm_group",
862                 .data           = &sysctl_hugetlb_shm_group,
863                 .maxlen         = sizeof(gid_t),
864                 .mode           = 0644,
865                 .proc_handler   = &proc_dointvec,
866          },
867          {
868                 .ctl_name       = CTL_UNNUMBERED,
869                 .procname       = "hugepages_treat_as_movable",
870                 .data           = &hugepages_treat_as_movable,
871                 .maxlen         = sizeof(int),
872                 .mode           = 0644,
873                 .proc_handler   = &hugetlb_treat_movable_handler,
874         },
875 #endif
876         {
877                 .ctl_name       = VM_LOWMEM_RESERVE_RATIO,
878                 .procname       = "lowmem_reserve_ratio",
879                 .data           = &sysctl_lowmem_reserve_ratio,
880                 .maxlen         = sizeof(sysctl_lowmem_reserve_ratio),
881                 .mode           = 0644,
882                 .proc_handler   = &lowmem_reserve_ratio_sysctl_handler,
883                 .strategy       = &sysctl_intvec,
884         },
885         {
886                 .ctl_name       = VM_DROP_PAGECACHE,
887                 .procname       = "drop_caches",
888                 .data           = &sysctl_drop_caches,
889                 .maxlen         = sizeof(int),
890                 .mode           = 0644,
891                 .proc_handler   = drop_caches_sysctl_handler,
892                 .strategy       = &sysctl_intvec,
893         },
894         {
895                 .ctl_name       = VM_MIN_FREE_KBYTES,
896                 .procname       = "min_free_kbytes",
897                 .data           = &min_free_kbytes,
898                 .maxlen         = sizeof(min_free_kbytes),
899                 .mode           = 0644,
900                 .proc_handler   = &min_free_kbytes_sysctl_handler,
901                 .strategy       = &sysctl_intvec,
902                 .extra1         = &zero,
903         },
904         {
905                 .ctl_name       = VM_PERCPU_PAGELIST_FRACTION,
906                 .procname       = "percpu_pagelist_fraction",
907                 .data           = &percpu_pagelist_fraction,
908                 .maxlen         = sizeof(percpu_pagelist_fraction),
909                 .mode           = 0644,
910                 .proc_handler   = &percpu_pagelist_fraction_sysctl_handler,
911                 .strategy       = &sysctl_intvec,
912                 .extra1         = &min_percpu_pagelist_fract,
913         },
914 #ifdef CONFIG_MMU
915         {
916                 .ctl_name       = VM_MAX_MAP_COUNT,
917                 .procname       = "max_map_count",
918                 .data           = &sysctl_max_map_count,
919                 .maxlen         = sizeof(sysctl_max_map_count),
920                 .mode           = 0644,
921                 .proc_handler   = &proc_dointvec
922         },
923 #endif
924         {
925                 .ctl_name       = VM_LAPTOP_MODE,
926                 .procname       = "laptop_mode",
927                 .data           = &laptop_mode,
928                 .maxlen         = sizeof(laptop_mode),
929                 .mode           = 0644,
930                 .proc_handler   = &proc_dointvec_jiffies,
931                 .strategy       = &sysctl_jiffies,
932         },
933         {
934                 .ctl_name       = VM_BLOCK_DUMP,
935                 .procname       = "block_dump",
936                 .data           = &block_dump,
937                 .maxlen         = sizeof(block_dump),
938                 .mode           = 0644,
939                 .proc_handler   = &proc_dointvec,
940                 .strategy       = &sysctl_intvec,
941                 .extra1         = &zero,
942         },
943         {
944                 .ctl_name       = VM_VFS_CACHE_PRESSURE,
945                 .procname       = "vfs_cache_pressure",
946                 .data           = &sysctl_vfs_cache_pressure,
947                 .maxlen         = sizeof(sysctl_vfs_cache_pressure),
948                 .mode           = 0644,
949                 .proc_handler   = &proc_dointvec,
950                 .strategy       = &sysctl_intvec,
951                 .extra1         = &zero,
952         },
953 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
954         {
955                 .ctl_name       = VM_LEGACY_VA_LAYOUT,
956                 .procname       = "legacy_va_layout",
957                 .data           = &sysctl_legacy_va_layout,
958                 .maxlen         = sizeof(sysctl_legacy_va_layout),
959                 .mode           = 0644,
960                 .proc_handler   = &proc_dointvec,
961                 .strategy       = &sysctl_intvec,
962                 .extra1         = &zero,
963         },
964 #endif
965 #ifdef CONFIG_NUMA
966         {
967                 .ctl_name       = VM_ZONE_RECLAIM_MODE,
968                 .procname       = "zone_reclaim_mode",
969                 .data           = &zone_reclaim_mode,
970                 .maxlen         = sizeof(zone_reclaim_mode),
971                 .mode           = 0644,
972                 .proc_handler   = &proc_dointvec,
973                 .strategy       = &sysctl_intvec,
974                 .extra1         = &zero,
975         },
976         {
977                 .ctl_name       = VM_MIN_UNMAPPED,
978                 .procname       = "min_unmapped_ratio",
979                 .data           = &sysctl_min_unmapped_ratio,
980                 .maxlen         = sizeof(sysctl_min_unmapped_ratio),
981                 .mode           = 0644,
982                 .proc_handler   = &sysctl_min_unmapped_ratio_sysctl_handler,
983                 .strategy       = &sysctl_intvec,
984                 .extra1         = &zero,
985                 .extra2         = &one_hundred,
986         },
987         {
988                 .ctl_name       = VM_MIN_SLAB,
989                 .procname       = "min_slab_ratio",
990                 .data           = &sysctl_min_slab_ratio,
991                 .maxlen         = sizeof(sysctl_min_slab_ratio),
992                 .mode           = 0644,
993                 .proc_handler   = &sysctl_min_slab_ratio_sysctl_handler,
994                 .strategy       = &sysctl_intvec,
995                 .extra1         = &zero,
996                 .extra2         = &one_hundred,
997         },
998 #endif
999 #ifdef CONFIG_SMP
1000         {
1001                 .ctl_name       = CTL_UNNUMBERED,
1002                 .procname       = "stat_interval",
1003                 .data           = &sysctl_stat_interval,
1004                 .maxlen         = sizeof(sysctl_stat_interval),
1005                 .mode           = 0644,
1006                 .proc_handler   = &proc_dointvec_jiffies,
1007                 .strategy       = &sysctl_jiffies,
1008         },
1009 #endif
1010 #ifdef CONFIG_SECURITY
1011         {
1012                 .ctl_name       = CTL_UNNUMBERED,
1013                 .procname       = "mmap_min_addr",
1014                 .data           = &mmap_min_addr,
1015                 .maxlen         = sizeof(unsigned long),
1016                 .mode           = 0644,
1017                 .proc_handler   = &proc_doulongvec_minmax,
1018         },
1019 #endif
1020 #ifdef CONFIG_NUMA
1021         {
1022                 .ctl_name       = CTL_UNNUMBERED,
1023                 .procname       = "numa_zonelist_order",
1024                 .data           = &numa_zonelist_order,
1025                 .maxlen         = NUMA_ZONELIST_ORDER_LEN,
1026                 .mode           = 0644,
1027                 .proc_handler   = &numa_zonelist_order_handler,
1028                 .strategy       = &sysctl_string,
1029         },
1030 #endif
1031 #if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
1032    (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
1033         {
1034                 .ctl_name       = VM_VDSO_ENABLED,
1035                 .procname       = "vdso_enabled",
1036                 .data           = &vdso_enabled,
1037                 .maxlen         = sizeof(vdso_enabled),
1038                 .mode           = 0644,
1039                 .proc_handler   = &proc_dointvec,
1040                 .strategy       = &sysctl_intvec,
1041                 .extra1         = &zero,
1042         },
1043 #endif
1044 /*
1045  * NOTE: do not add new entries to this table unless you have read
1046  * Documentation/sysctl/ctl_unnumbered.txt
1047  */
1048         { .ctl_name = 0 }
1049 };
1050
1051 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1052 static ctl_table binfmt_misc_table[] = {
1053         { .ctl_name = 0 }
1054 };
1055 #endif
1056
1057 static ctl_table fs_table[] = {
1058         {
1059                 .ctl_name       = FS_NRINODE,
1060                 .procname       = "inode-nr",
1061                 .data           = &inodes_stat,
1062                 .maxlen         = 2*sizeof(int),
1063                 .mode           = 0444,
1064                 .proc_handler   = &proc_dointvec,
1065         },
1066         {
1067                 .ctl_name       = FS_STATINODE,
1068                 .procname       = "inode-state",
1069                 .data           = &inodes_stat,
1070                 .maxlen         = 7*sizeof(int),
1071                 .mode           = 0444,
1072                 .proc_handler   = &proc_dointvec,
1073         },
1074         {
1075                 .ctl_name       = FS_NRFILE,
1076                 .procname       = "file-nr",
1077                 .data           = &files_stat,
1078                 .maxlen         = 3*sizeof(int),
1079                 .mode           = 0444,
1080                 .proc_handler   = &proc_nr_files,
1081         },
1082         {
1083                 .ctl_name       = FS_MAXFILE,
1084                 .procname       = "file-max",
1085                 .data           = &files_stat.max_files,
1086                 .maxlen         = sizeof(int),
1087                 .mode           = 0644,
1088                 .proc_handler   = &proc_dointvec,
1089         },
1090         {
1091                 .ctl_name       = FS_DENTRY,
1092                 .procname       = "dentry-state",
1093                 .data           = &dentry_stat,
1094                 .maxlen         = 6*sizeof(int),
1095                 .mode           = 0444,
1096                 .proc_handler   = &proc_dointvec,
1097         },
1098         {
1099                 .ctl_name       = FS_OVERFLOWUID,
1100                 .procname       = "overflowuid",
1101                 .data           = &fs_overflowuid,
1102                 .maxlen         = sizeof(int),
1103                 .mode           = 0644,
1104                 .proc_handler   = &proc_dointvec_minmax,
1105                 .strategy       = &sysctl_intvec,
1106                 .extra1         = &minolduid,
1107                 .extra2         = &maxolduid,
1108         },
1109         {
1110                 .ctl_name       = FS_OVERFLOWGID,
1111                 .procname       = "overflowgid",
1112                 .data           = &fs_overflowgid,
1113                 .maxlen         = sizeof(int),
1114                 .mode           = 0644,
1115                 .proc_handler   = &proc_dointvec_minmax,
1116                 .strategy       = &sysctl_intvec,
1117                 .extra1         = &minolduid,
1118                 .extra2         = &maxolduid,
1119         },
1120         {
1121                 .ctl_name       = FS_LEASES,
1122                 .procname       = "leases-enable",
1123                 .data           = &leases_enable,
1124                 .maxlen         = sizeof(int),
1125                 .mode           = 0644,
1126                 .proc_handler   = &proc_dointvec,
1127         },
1128 #ifdef CONFIG_DNOTIFY
1129         {
1130                 .ctl_name       = FS_DIR_NOTIFY,
1131                 .procname       = "dir-notify-enable",
1132                 .data           = &dir_notify_enable,
1133                 .maxlen         = sizeof(int),
1134                 .mode           = 0644,
1135                 .proc_handler   = &proc_dointvec,
1136         },
1137 #endif
1138 #ifdef CONFIG_MMU
1139         {
1140                 .ctl_name       = FS_LEASE_TIME,
1141                 .procname       = "lease-break-time",
1142                 .data           = &lease_break_time,
1143                 .maxlen         = sizeof(int),
1144                 .mode           = 0644,
1145                 .proc_handler   = &proc_dointvec_minmax,
1146                 .strategy       = &sysctl_intvec,
1147                 .extra1         = &zero,
1148                 .extra2         = &two,
1149         },
1150         {
1151                 .ctl_name       = FS_AIO_NR,
1152                 .procname       = "aio-nr",
1153                 .data           = &aio_nr,
1154                 .maxlen         = sizeof(aio_nr),
1155                 .mode           = 0444,
1156                 .proc_handler   = &proc_doulongvec_minmax,
1157         },
1158         {
1159                 .ctl_name       = FS_AIO_MAX_NR,
1160                 .procname       = "aio-max-nr",
1161                 .data           = &aio_max_nr,
1162                 .maxlen         = sizeof(aio_max_nr),
1163                 .mode           = 0644,
1164                 .proc_handler   = &proc_doulongvec_minmax,
1165         },
1166 #ifdef CONFIG_INOTIFY_USER
1167         {
1168                 .ctl_name       = FS_INOTIFY,
1169                 .procname       = "inotify",
1170                 .mode           = 0555,
1171                 .child          = inotify_table,
1172         },
1173 #endif  
1174 #endif
1175         {
1176                 .ctl_name       = KERN_SETUID_DUMPABLE,
1177                 .procname       = "suid_dumpable",
1178                 .data           = &suid_dumpable,
1179                 .maxlen         = sizeof(int),
1180                 .mode           = 0644,
1181                 .proc_handler   = &proc_dointvec,
1182         },
1183 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1184         {
1185                 .ctl_name       = CTL_UNNUMBERED,
1186                 .procname       = "binfmt_misc",
1187                 .mode           = 0555,
1188                 .child          = binfmt_misc_table,
1189         },
1190 #endif
1191 /*
1192  * NOTE: do not add new entries to this table unless you have read
1193  * Documentation/sysctl/ctl_unnumbered.txt
1194  */
1195         { .ctl_name = 0 }
1196 };
1197
1198 static ctl_table debug_table[] = {
1199 #if defined(CONFIG_X86) || defined(CONFIG_PPC)
1200         {
1201                 .ctl_name       = CTL_UNNUMBERED,
1202                 .procname       = "exception-trace",
1203                 .data           = &show_unhandled_signals,
1204                 .maxlen         = sizeof(int),
1205                 .mode           = 0644,
1206                 .proc_handler   = proc_dointvec
1207         },
1208 #endif
1209         { .ctl_name = 0 }
1210 };
1211
1212 static ctl_table dev_table[] = {
1213         { .ctl_name = 0 }
1214 };
1215
1216 static DEFINE_SPINLOCK(sysctl_lock);
1217
1218 /* called under sysctl_lock */
1219 static int use_table(struct ctl_table_header *p)
1220 {
1221         if (unlikely(p->unregistering))
1222                 return 0;
1223         p->used++;
1224         return 1;
1225 }
1226
1227 /* called under sysctl_lock */
1228 static void unuse_table(struct ctl_table_header *p)
1229 {
1230         if (!--p->used)
1231                 if (unlikely(p->unregistering))
1232                         complete(p->unregistering);
1233 }
1234
1235 /* called under sysctl_lock, will reacquire if has to wait */
1236 static void start_unregistering(struct ctl_table_header *p)
1237 {
1238         /*
1239          * if p->used is 0, nobody will ever touch that entry again;
1240          * we'll eliminate all paths to it before dropping sysctl_lock
1241          */
1242         if (unlikely(p->used)) {
1243                 struct completion wait;
1244                 init_completion(&wait);
1245                 p->unregistering = &wait;
1246                 spin_unlock(&sysctl_lock);
1247                 wait_for_completion(&wait);
1248                 spin_lock(&sysctl_lock);
1249         }
1250         /*
1251          * do not remove from the list until nobody holds it; walking the
1252          * list in do_sysctl() relies on that.
1253          */
1254         list_del_init(&p->ctl_entry);
1255 }
1256
1257 void sysctl_head_finish(struct ctl_table_header *head)
1258 {
1259         if (!head)
1260                 return;
1261         spin_lock(&sysctl_lock);
1262         unuse_table(head);
1263         spin_unlock(&sysctl_lock);
1264 }
1265
1266 struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1267 {
1268         struct ctl_table_header *head;
1269         struct list_head *tmp;
1270         spin_lock(&sysctl_lock);
1271         if (prev) {
1272                 tmp = &prev->ctl_entry;
1273                 unuse_table(prev);
1274                 goto next;
1275         }
1276         tmp = &root_table_header.ctl_entry;
1277         for (;;) {
1278                 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1279
1280                 if (!use_table(head))
1281                         goto next;
1282                 spin_unlock(&sysctl_lock);
1283                 return head;
1284         next:
1285                 tmp = tmp->next;
1286                 if (tmp == &root_table_header.ctl_entry)
1287                         break;
1288         }
1289         spin_unlock(&sysctl_lock);
1290         return NULL;
1291 }
1292
1293 #ifdef CONFIG_SYSCTL_SYSCALL
1294 int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
1295                void __user *newval, size_t newlen)
1296 {
1297         struct ctl_table_header *head;
1298         int error = -ENOTDIR;
1299
1300         if (nlen <= 0 || nlen >= CTL_MAXNAME)
1301                 return -ENOTDIR;
1302         if (oldval) {
1303                 int old_len;
1304                 if (!oldlenp || get_user(old_len, oldlenp))
1305                         return -EFAULT;
1306         }
1307
1308         for (head = sysctl_head_next(NULL); head;
1309                         head = sysctl_head_next(head)) {
1310                 error = parse_table(name, nlen, oldval, oldlenp, 
1311                                         newval, newlen, head->ctl_table);
1312                 if (error != -ENOTDIR) {
1313                         sysctl_head_finish(head);
1314                         break;
1315                 }
1316         }
1317         return error;
1318 }
1319
1320 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
1321 {
1322         struct __sysctl_args tmp;
1323         int error;
1324
1325         if (copy_from_user(&tmp, args, sizeof(tmp)))
1326                 return -EFAULT;
1327
1328         lock_kernel();
1329         error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
1330                           tmp.newval, tmp.newlen);
1331         unlock_kernel();
1332         return error;
1333 }
1334 #endif /* CONFIG_SYSCTL_SYSCALL */
1335
1336 /*
1337  * sysctl_perm does NOT grant the superuser all rights automatically, because
1338  * some sysctl variables are readonly even to root.
1339  */
1340
1341 static int test_perm(int mode, int op)
1342 {
1343         if (!current->euid)
1344                 mode >>= 6;
1345         else if (in_egroup_p(0))
1346                 mode >>= 3;
1347         if ((mode & op & 0007) == op)
1348                 return 0;
1349         return -EACCES;
1350 }
1351
1352 int sysctl_perm(ctl_table *table, int op)
1353 {
1354         int error;
1355         error = security_sysctl(table, op);
1356         if (error)
1357                 return error;
1358         return test_perm(table->mode, op);
1359 }
1360
1361 #ifdef CONFIG_SYSCTL_SYSCALL
1362 static int parse_table(int __user *name, int nlen,
1363                        void __user *oldval, size_t __user *oldlenp,
1364                        void __user *newval, size_t newlen,
1365                        ctl_table *table)
1366 {
1367         int n;
1368 repeat:
1369         if (!nlen)
1370                 return -ENOTDIR;
1371         if (get_user(n, name))
1372                 return -EFAULT;
1373         for ( ; table->ctl_name || table->procname; table++) {
1374                 if (!table->ctl_name)
1375                         continue;
1376                 if (n == table->ctl_name) {
1377                         int error;
1378                         if (table->child) {
1379                                 if (sysctl_perm(table, 001))
1380                                         return -EPERM;
1381                                 name++;
1382                                 nlen--;
1383                                 table = table->child;
1384                                 goto repeat;
1385                         }
1386                         error = do_sysctl_strategy(table, name, nlen,
1387                                                    oldval, oldlenp,
1388                                                    newval, newlen);
1389                         return error;
1390                 }
1391         }
1392         return -ENOTDIR;
1393 }
1394
1395 /* Perform the actual read/write of a sysctl table entry. */
1396 int do_sysctl_strategy (ctl_table *table, 
1397                         int __user *name, int nlen,
1398                         void __user *oldval, size_t __user *oldlenp,
1399                         void __user *newval, size_t newlen)
1400 {
1401         int op = 0, rc;
1402         size_t len;
1403
1404         if (oldval)
1405                 op |= 004;
1406         if (newval) 
1407                 op |= 002;
1408         if (sysctl_perm(table, op))
1409                 return -EPERM;
1410
1411         if (table->strategy) {
1412                 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1413                                      newval, newlen);
1414                 if (rc < 0)
1415                         return rc;
1416                 if (rc > 0)
1417                         return 0;
1418         }
1419
1420         /* If there is no strategy routine, or if the strategy returns
1421          * zero, proceed with automatic r/w */
1422         if (table->data && table->maxlen) {
1423                 if (oldval && oldlenp) {
1424                         if (get_user(len, oldlenp))
1425                                 return -EFAULT;
1426                         if (len) {
1427                                 if (len > table->maxlen)
1428                                         len = table->maxlen;
1429                                 if(copy_to_user(oldval, table->data, len))
1430                                         return -EFAULT;
1431                                 if(put_user(len, oldlenp))
1432                                         return -EFAULT;
1433                         }
1434                 }
1435                 if (newval && newlen) {
1436                         len = newlen;
1437                         if (len > table->maxlen)
1438                                 len = table->maxlen;
1439                         if(copy_from_user(table->data, newval, len))
1440                                 return -EFAULT;
1441                 }
1442         }
1443         return 0;
1444 }
1445 #endif /* CONFIG_SYSCTL_SYSCALL */
1446
1447 static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
1448 {
1449         for (; table->ctl_name || table->procname; table++) {
1450                 table->parent = parent;
1451                 if (table->child)
1452                         sysctl_set_parent(table, table->child);
1453         }
1454 }
1455
1456 static __init int sysctl_init(void)
1457 {
1458         sysctl_set_parent(NULL, root_table);
1459         return 0;
1460 }
1461
1462 core_initcall(sysctl_init);
1463
1464 /**
1465  * register_sysctl_table - register a sysctl hierarchy
1466  * @table: the top-level table structure
1467  *
1468  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1469  * array. An entry with a ctl_name of 0 terminates the table. 
1470  *
1471  * The members of the &ctl_table structure are used as follows:
1472  *
1473  * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1474  *            must be unique within that level of sysctl
1475  *
1476  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1477  *            enter a sysctl file
1478  *
1479  * data - a pointer to data for use by proc_handler
1480  *
1481  * maxlen - the maximum size in bytes of the data
1482  *
1483  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1484  *
1485  * child - a pointer to the child sysctl table if this entry is a directory, or
1486  *         %NULL.
1487  *
1488  * proc_handler - the text handler routine (described below)
1489  *
1490  * strategy - the strategy routine (described below)
1491  *
1492  * de - for internal use by the sysctl routines
1493  *
1494  * extra1, extra2 - extra pointers usable by the proc handler routines
1495  *
1496  * Leaf nodes in the sysctl tree will be represented by a single file
1497  * under /proc; non-leaf nodes will be represented by directories.
1498  *
1499  * sysctl(2) can automatically manage read and write requests through
1500  * the sysctl table.  The data and maxlen fields of the ctl_table
1501  * struct enable minimal validation of the values being written to be
1502  * performed, and the mode field allows minimal authentication.
1503  *
1504  * More sophisticated management can be enabled by the provision of a
1505  * strategy routine with the table entry.  This will be called before
1506  * any automatic read or write of the data is performed.
1507  *
1508  * The strategy routine may return
1509  *
1510  * < 0 - Error occurred (error is passed to user process)
1511  *
1512  * 0   - OK - proceed with automatic read or write.
1513  *
1514  * > 0 - OK - read or write has been done by the strategy routine, so
1515  *       return immediately.
1516  *
1517  * There must be a proc_handler routine for any terminal nodes
1518  * mirrored under /proc/sys (non-terminals are handled by a built-in
1519  * directory handler).  Several default handlers are available to
1520  * cover common cases -
1521  *
1522  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1523  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 
1524  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1525  *
1526  * It is the handler's job to read the input buffer from user memory
1527  * and process it. The handler should return 0 on success.
1528  *
1529  * This routine returns %NULL on a failure to register, and a pointer
1530  * to the table header on success.
1531  */
1532 struct ctl_table_header *register_sysctl_table(ctl_table * table)
1533 {
1534         struct ctl_table_header *tmp;
1535         tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1536         if (!tmp)
1537                 return NULL;
1538         tmp->ctl_table = table;
1539         INIT_LIST_HEAD(&tmp->ctl_entry);
1540         tmp->used = 0;
1541         tmp->unregistering = NULL;
1542         sysctl_set_parent(NULL, table);
1543         spin_lock(&sysctl_lock);
1544         list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1545         spin_unlock(&sysctl_lock);
1546         return tmp;
1547 }
1548
1549 /**
1550  * unregister_sysctl_table - unregister a sysctl table hierarchy
1551  * @header: the header returned from register_sysctl_table
1552  *
1553  * Unregisters the sysctl table and all children. proc entries may not
1554  * actually be removed until they are no longer used by anyone.
1555  */
1556 void unregister_sysctl_table(struct ctl_table_header * header)
1557 {
1558         might_sleep();
1559         spin_lock(&sysctl_lock);
1560         start_unregistering(header);
1561         spin_unlock(&sysctl_lock);
1562         kfree(header);
1563 }
1564
1565 #else /* !CONFIG_SYSCTL */
1566 struct ctl_table_header *register_sysctl_table(ctl_table * table)
1567 {
1568         return NULL;
1569 }
1570
1571 void unregister_sysctl_table(struct ctl_table_header * table)
1572 {
1573 }
1574
1575 #endif /* CONFIG_SYSCTL */
1576
1577 /*
1578  * /proc/sys support
1579  */
1580
1581 #ifdef CONFIG_PROC_SYSCTL
1582
1583 static int _proc_do_string(void* data, int maxlen, int write,
1584                            struct file *filp, void __user *buffer,
1585                            size_t *lenp, loff_t *ppos)
1586 {
1587         size_t len;
1588         char __user *p;
1589         char c;
1590
1591         if (!data || !maxlen || !*lenp) {
1592                 *lenp = 0;
1593                 return 0;
1594         }
1595
1596         if (write) {
1597                 len = 0;
1598                 p = buffer;
1599                 while (len < *lenp) {
1600                         if (get_user(c, p++))
1601                                 return -EFAULT;
1602                         if (c == 0 || c == '\n')
1603                                 break;
1604                         len++;
1605                 }
1606                 if (len >= maxlen)
1607                         len = maxlen-1;
1608                 if(copy_from_user(data, buffer, len))
1609                         return -EFAULT;
1610                 ((char *) data)[len] = 0;
1611                 *ppos += *lenp;
1612         } else {
1613                 len = strlen(data);
1614                 if (len > maxlen)
1615                         len = maxlen;
1616
1617                 if (*ppos > len) {
1618                         *lenp = 0;
1619                         return 0;
1620                 }
1621
1622                 data += *ppos;
1623                 len  -= *ppos;
1624
1625                 if (len > *lenp)
1626                         len = *lenp;
1627                 if (len)
1628                         if(copy_to_user(buffer, data, len))
1629                                 return -EFAULT;
1630                 if (len < *lenp) {
1631                         if(put_user('\n', ((char __user *) buffer) + len))
1632                                 return -EFAULT;
1633                         len++;
1634                 }
1635                 *lenp = len;
1636                 *ppos += len;
1637         }
1638         return 0;
1639 }
1640
1641 /**
1642  * proc_dostring - read a string sysctl
1643  * @table: the sysctl table
1644  * @write: %TRUE if this is a write to the sysctl file
1645  * @filp: the file structure
1646  * @buffer: the user buffer
1647  * @lenp: the size of the user buffer
1648  * @ppos: file position
1649  *
1650  * Reads/writes a string from/to the user buffer. If the kernel
1651  * buffer provided is not large enough to hold the string, the
1652  * string is truncated. The copied string is %NULL-terminated.
1653  * If the string is being read by the user process, it is copied
1654  * and a newline '\n' is added. It is truncated if the buffer is
1655  * not large enough.
1656  *
1657  * Returns 0 on success.
1658  */
1659 int proc_dostring(ctl_table *table, int write, struct file *filp,
1660                   void __user *buffer, size_t *lenp, loff_t *ppos)
1661 {
1662         return _proc_do_string(table->data, table->maxlen, write, filp,
1663                                buffer, lenp, ppos);
1664 }
1665
1666
1667 static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
1668                                  int *valp,
1669                                  int write, void *data)
1670 {
1671         if (write) {
1672                 *valp = *negp ? -*lvalp : *lvalp;
1673         } else {
1674                 int val = *valp;
1675                 if (val < 0) {
1676                         *negp = -1;
1677                         *lvalp = (unsigned long)-val;
1678                 } else {
1679                         *negp = 0;
1680                         *lvalp = (unsigned long)val;
1681                 }
1682         }
1683         return 0;
1684 }
1685
1686 static int __do_proc_dointvec(void *tbl_data, ctl_table *table,
1687                   int write, struct file *filp, void __user *buffer,
1688                   size_t *lenp, loff_t *ppos,
1689                   int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1690                               int write, void *data),
1691                   void *data)
1692 {
1693 #define TMPBUFLEN 21
1694         int *i, vleft, first=1, neg, val;
1695         unsigned long lval;
1696         size_t left, len;
1697         
1698         char buf[TMPBUFLEN], *p;
1699         char __user *s = buffer;
1700         
1701         if (!tbl_data || !table->maxlen || !*lenp ||
1702             (*ppos && !write)) {
1703                 *lenp = 0;
1704                 return 0;
1705         }
1706         
1707         i = (int *) tbl_data;
1708         vleft = table->maxlen / sizeof(*i);
1709         left = *lenp;
1710
1711         if (!conv)
1712                 conv = do_proc_dointvec_conv;
1713
1714         for (; left && vleft--; i++, first=0) {
1715                 if (write) {
1716                         while (left) {
1717                                 char c;
1718                                 if (get_user(c, s))
1719                                         return -EFAULT;
1720                                 if (!isspace(c))
1721                                         break;
1722                                 left--;
1723                                 s++;
1724                         }
1725                         if (!left)
1726                                 break;
1727                         neg = 0;
1728                         len = left;
1729                         if (len > sizeof(buf) - 1)
1730                                 len = sizeof(buf) - 1;
1731                         if (copy_from_user(buf, s, len))
1732                                 return -EFAULT;
1733                         buf[len] = 0;
1734                         p = buf;
1735                         if (*p == '-' && left > 1) {
1736                                 neg = 1;
1737                                 p++;
1738                         }
1739                         if (*p < '0' || *p > '9')
1740                                 break;
1741
1742                         lval = simple_strtoul(p, &p, 0);
1743
1744                         len = p-buf;
1745                         if ((len < left) && *p && !isspace(*p))
1746                                 break;
1747                         if (neg)
1748                                 val = -val;
1749                         s += len;
1750                         left -= len;
1751
1752                         if (conv(&neg, &lval, i, 1, data))
1753                                 break;
1754                 } else {
1755                         p = buf;
1756                         if (!first)
1757                                 *p++ = '\t';
1758         
1759                         if (conv(&neg, &lval, i, 0, data))
1760                                 break;
1761
1762                         sprintf(p, "%s%lu", neg ? "-" : "", lval);
1763                         len = strlen(buf);
1764                         if (len > left)
1765                                 len = left;
1766                         if(copy_to_user(s, buf, len))
1767                                 return -EFAULT;
1768                         left -= len;
1769                         s += len;
1770                 }
1771         }
1772
1773         if (!write && !first && left) {
1774                 if(put_user('\n', s))
1775                         return -EFAULT;
1776                 left--, s++;
1777         }
1778         if (write) {
1779                 while (left) {
1780                         char c;
1781                         if (get_user(c, s++))
1782                                 return -EFAULT;
1783                         if (!isspace(c))
1784                                 break;
1785                         left--;
1786                 }
1787         }
1788         if (write && first)
1789                 return -EINVAL;
1790         *lenp -= left;
1791         *ppos += *lenp;
1792         return 0;
1793 #undef TMPBUFLEN
1794 }
1795
1796 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
1797                   void __user *buffer, size_t *lenp, loff_t *ppos,
1798                   int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1799                               int write, void *data),
1800                   void *data)
1801 {
1802         return __do_proc_dointvec(table->data, table, write, filp,
1803                         buffer, lenp, ppos, conv, data);
1804 }
1805
1806 /**
1807  * proc_dointvec - read a vector of integers
1808  * @table: the sysctl table
1809  * @write: %TRUE if this is a write to the sysctl file
1810  * @filp: the file structure
1811  * @buffer: the user buffer
1812  * @lenp: the size of the user buffer
1813  * @ppos: file position
1814  *
1815  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1816  * values from/to the user buffer, treated as an ASCII string. 
1817  *
1818  * Returns 0 on success.
1819  */
1820 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1821                      void __user *buffer, size_t *lenp, loff_t *ppos)
1822 {
1823     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1824                             NULL,NULL);
1825 }
1826
1827 #define OP_SET  0
1828 #define OP_AND  1
1829 #define OP_OR   2
1830
1831 static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
1832                                       int *valp,
1833                                       int write, void *data)
1834 {
1835         int op = *(int *)data;
1836         if (write) {
1837                 int val = *negp ? -*lvalp : *lvalp;
1838                 switch(op) {
1839                 case OP_SET:    *valp = val; break;
1840                 case OP_AND:    *valp &= val; break;
1841                 case OP_OR:     *valp |= val; break;
1842                 }
1843         } else {
1844                 int val = *valp;
1845                 if (val < 0) {
1846                         *negp = -1;
1847                         *lvalp = (unsigned long)-val;
1848                 } else {
1849                         *negp = 0;
1850                         *lvalp = (unsigned long)val;
1851                 }
1852         }
1853         return 0;
1854 }
1855
1856 /*
1857  *      init may raise the set.
1858  */
1859  
1860 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1861                         void __user *buffer, size_t *lenp, loff_t *ppos)
1862 {
1863         int op;
1864
1865         if (write && !capable(CAP_SYS_MODULE)) {
1866                 return -EPERM;
1867         }
1868
1869         op = is_init(current) ? OP_SET : OP_AND;
1870         return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1871                                 do_proc_dointvec_bset_conv,&op);
1872 }
1873
1874 /*
1875  *      Taint values can only be increased
1876  */
1877 static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
1878                                void __user *buffer, size_t *lenp, loff_t *ppos)
1879 {
1880         int op;
1881
1882         if (write && !capable(CAP_SYS_ADMIN))
1883                 return -EPERM;
1884
1885         op = OP_OR;
1886         return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1887                                 do_proc_dointvec_bset_conv,&op);
1888 }
1889
1890 struct do_proc_dointvec_minmax_conv_param {
1891         int *min;
1892         int *max;
1893 };
1894
1895 static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, 
1896                                         int *valp, 
1897                                         int write, void *data)
1898 {
1899         struct do_proc_dointvec_minmax_conv_param *param = data;
1900         if (write) {
1901                 int val = *negp ? -*lvalp : *lvalp;
1902                 if ((param->min && *param->min > val) ||
1903                     (param->max && *param->max < val))
1904                         return -EINVAL;
1905                 *valp = val;
1906         } else {
1907                 int val = *valp;
1908                 if (val < 0) {
1909                         *negp = -1;
1910                         *lvalp = (unsigned long)-val;
1911                 } else {
1912                         *negp = 0;
1913                         *lvalp = (unsigned long)val;
1914                 }
1915         }
1916         return 0;
1917 }
1918
1919 /**
1920  * proc_dointvec_minmax - read a vector of integers with min/max values
1921  * @table: the sysctl table
1922  * @write: %TRUE if this is a write to the sysctl file
1923  * @filp: the file structure
1924  * @buffer: the user buffer
1925  * @lenp: the size of the user buffer
1926  * @ppos: file position
1927  *
1928  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1929  * values from/to the user buffer, treated as an ASCII string.
1930  *
1931  * This routine will ensure the values are within the range specified by
1932  * table->extra1 (min) and table->extra2 (max).
1933  *
1934  * Returns 0 on success.
1935  */
1936 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1937                   void __user *buffer, size_t *lenp, loff_t *ppos)
1938 {
1939         struct do_proc_dointvec_minmax_conv_param param = {
1940                 .min = (int *) table->extra1,
1941                 .max = (int *) table->extra2,
1942         };
1943         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
1944                                 do_proc_dointvec_minmax_conv, &param);
1945 }
1946
1947 static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write,
1948                                      struct file *filp,
1949                                      void __user *buffer,
1950                                      size_t *lenp, loff_t *ppos,
1951                                      unsigned long convmul,
1952                                      unsigned long convdiv)
1953 {
1954 #define TMPBUFLEN 21
1955         unsigned long *i, *min, *max, val;
1956         int vleft, first=1, neg;
1957         size_t len, left;
1958         char buf[TMPBUFLEN], *p;
1959         char __user *s = buffer;
1960         
1961         if (!data || !table->maxlen || !*lenp ||
1962             (*ppos && !write)) {
1963                 *lenp = 0;
1964                 return 0;
1965         }
1966         
1967         i = (unsigned long *) data;
1968         min = (unsigned long *) table->extra1;
1969         max = (unsigned long *) table->extra2;
1970         vleft = table->maxlen / sizeof(unsigned long);
1971         left = *lenp;
1972         
1973         for (; left && vleft--; i++, min++, max++, first=0) {
1974                 if (write) {
1975                         while (left) {
1976                                 char c;
1977                                 if (get_user(c, s))
1978                                         return -EFAULT;
1979                                 if (!isspace(c))
1980                                         break;
1981                                 left--;
1982                                 s++;
1983                         }
1984                         if (!left)
1985                                 break;
1986                         neg = 0;
1987                         len = left;
1988                         if (len > TMPBUFLEN-1)
1989                                 len = TMPBUFLEN-1;
1990                         if (copy_from_user(buf, s, len))
1991                                 return -EFAULT;
1992                         buf[len] = 0;
1993                         p = buf;
1994                         if (*p == '-' && left > 1) {
1995                                 neg = 1;
1996                                 p++;
1997                         }
1998                         if (*p < '0' || *p > '9')
1999                                 break;
2000                         val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
2001                         len = p-buf;
2002                         if ((len < left) && *p && !isspace(*p))
2003                                 break;
2004                         if (neg)
2005                                 val = -val;
2006                         s += len;
2007                         left -= len;
2008
2009                         if(neg)
2010                                 continue;
2011                         if ((min && val < *min) || (max && val > *max))
2012                                 continue;
2013                         *i = val;
2014                 } else {
2015                         p = buf;
2016                         if (!first)
2017                                 *p++ = '\t';
2018                         sprintf(p, "%lu", convdiv * (*i) / convmul);
2019                         len = strlen(buf);
2020                         if (len > left)
2021                                 len = left;
2022                         if(copy_to_user(s, buf, len))
2023                                 return -EFAULT;
2024                         left -= len;
2025                         s += len;
2026                 }
2027         }
2028
2029         if (!write && !first && left) {
2030                 if(put_user('\n', s))
2031                         return -EFAULT;
2032                 left--, s++;
2033         }
2034         if (write) {
2035                 while (left) {
2036                         char c;
2037                         if (get_user(c, s++))
2038                                 return -EFAULT;
2039                         if (!isspace(c))
2040                                 break;
2041                         left--;
2042                 }
2043         }
2044         if (write && first)
2045                 return -EINVAL;
2046         *lenp -= left;
2047         *ppos += *lenp;
2048         return 0;
2049 #undef TMPBUFLEN
2050 }
2051
2052 static int do_proc_doulongvec_minmax(ctl_table *table, int write,
2053                                      struct file *filp,
2054                                      void __user *buffer,
2055                                      size_t *lenp, loff_t *ppos,
2056                                      unsigned long convmul,
2057                                      unsigned long convdiv)
2058 {
2059         return __do_proc_doulongvec_minmax(table->data, table, write,
2060                         filp, buffer, lenp, ppos, convmul, convdiv);
2061 }
2062
2063 /**
2064  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2065  * @table: the sysctl table
2066  * @write: %TRUE if this is a write to the sysctl file
2067  * @filp: the file structure
2068  * @buffer: the user buffer
2069  * @lenp: the size of the user buffer
2070  * @ppos: file position
2071  *
2072  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2073  * values from/to the user buffer, treated as an ASCII string.
2074  *
2075  * This routine will ensure the values are within the range specified by
2076  * table->extra1 (min) and table->extra2 (max).
2077  *
2078  * Returns 0 on success.
2079  */
2080 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2081                            void __user *buffer, size_t *lenp, loff_t *ppos)
2082 {
2083     return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
2084 }
2085
2086 /**
2087  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2088  * @table: the sysctl table
2089  * @write: %TRUE if this is a write to the sysctl file
2090  * @filp: the file structure
2091  * @buffer: the user buffer
2092  * @lenp: the size of the user buffer
2093  * @ppos: file position
2094  *
2095  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2096  * values from/to the user buffer, treated as an ASCII string. The values
2097  * are treated as milliseconds, and converted to jiffies when they are stored.
2098  *
2099  * This routine will ensure the values are within the range specified by
2100  * table->extra1 (min) and table->extra2 (max).
2101  *
2102  * Returns 0 on success.
2103  */
2104 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2105                                       struct file *filp,
2106                                       void __user *buffer,
2107                                       size_t *lenp, loff_t *ppos)
2108 {
2109     return do_proc_doulongvec_minmax(table, write, filp, buffer,
2110                                      lenp, ppos, HZ, 1000l);
2111 }
2112
2113
2114 static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
2115                                          int *valp,
2116                                          int write, void *data)
2117 {
2118         if (write) {
2119                 if (*lvalp > LONG_MAX / HZ)
2120                         return 1;
2121                 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2122         } else {
2123                 int val = *valp;
2124                 unsigned long lval;
2125                 if (val < 0) {
2126                         *negp = -1;
2127                         lval = (unsigned long)-val;
2128                 } else {
2129                         *negp = 0;
2130                         lval = (unsigned long)val;
2131                 }
2132                 *lvalp = lval / HZ;
2133         }
2134         return 0;
2135 }
2136
2137 static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
2138                                                 int *valp,
2139                                                 int write, void *data)
2140 {
2141         if (write) {
2142                 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2143                         return 1;
2144                 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2145         } else {
2146                 int val = *valp;
2147                 unsigned long lval;
2148                 if (val < 0) {
2149                         *negp = -1;
2150                         lval = (unsigned long)-val;
2151                 } else {
2152                         *negp = 0;
2153                         lval = (unsigned long)val;
2154                 }
2155                 *lvalp = jiffies_to_clock_t(lval);
2156         }
2157         return 0;
2158 }
2159
2160 static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
2161                                             int *valp,
2162                                             int write, void *data)
2163 {
2164         if (write) {
2165                 *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2166         } else {
2167                 int val = *valp;
2168                 unsigned long lval;
2169                 if (val < 0) {
2170                         *negp = -1;
2171                         lval = (unsigned long)-val;
2172                 } else {
2173                         *negp = 0;
2174                         lval = (unsigned long)val;
2175                 }
2176                 *lvalp = jiffies_to_msecs(lval);
2177         }
2178         return 0;
2179 }
2180
2181 /**
2182  * proc_dointvec_jiffies - read a vector of integers as seconds
2183  * @table: the sysctl table
2184  * @write: %TRUE if this is a write to the sysctl file
2185  * @filp: the file structure
2186  * @buffer: the user buffer
2187  * @lenp: the size of the user buffer
2188  * @ppos: file position
2189  *
2190  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2191  * values from/to the user buffer, treated as an ASCII string. 
2192  * The values read are assumed to be in seconds, and are converted into
2193  * jiffies.
2194  *
2195  * Returns 0 on success.
2196  */
2197 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2198                           void __user *buffer, size_t *lenp, loff_t *ppos)
2199 {
2200     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2201                             do_proc_dointvec_jiffies_conv,NULL);
2202 }
2203
2204 /**
2205  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2206  * @table: the sysctl table
2207  * @write: %TRUE if this is a write to the sysctl file
2208  * @filp: the file structure
2209  * @buffer: the user buffer
2210  * @lenp: the size of the user buffer
2211  * @ppos: pointer to the file position
2212  *
2213  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2214  * values from/to the user buffer, treated as an ASCII string. 
2215  * The values read are assumed to be in 1/USER_HZ seconds, and 
2216  * are converted into jiffies.
2217  *
2218  * Returns 0 on success.
2219  */
2220 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2221                                  void __user *buffer, size_t *lenp, loff_t *ppos)
2222 {
2223     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2224                             do_proc_dointvec_userhz_jiffies_conv,NULL);
2225 }
2226
2227 /**
2228  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2229  * @table: the sysctl table
2230  * @write: %TRUE if this is a write to the sysctl file
2231  * @filp: the file structure
2232  * @buffer: the user buffer
2233  * @lenp: the size of the user buffer
2234  * @ppos: file position
2235  * @ppos: the current position in the file
2236  *
2237  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2238  * values from/to the user buffer, treated as an ASCII string. 
2239  * The values read are assumed to be in 1/1000 seconds, and 
2240  * are converted into jiffies.
2241  *
2242  * Returns 0 on success.
2243  */
2244 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2245                              void __user *buffer, size_t *lenp, loff_t *ppos)
2246 {
2247         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2248                                 do_proc_dointvec_ms_jiffies_conv, NULL);
2249 }
2250
2251 static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
2252                            void __user *buffer, size_t *lenp, loff_t *ppos)
2253 {
2254         struct pid *new_pid;
2255         pid_t tmp;
2256         int r;
2257
2258         tmp = pid_nr(cad_pid);
2259
2260         r = __do_proc_dointvec(&tmp, table, write, filp, buffer,
2261                                lenp, ppos, NULL, NULL);
2262         if (r || !write)
2263                 return r;
2264
2265         new_pid = find_get_pid(tmp);
2266         if (!new_pid)
2267                 return -ESRCH;
2268
2269         put_pid(xchg(&cad_pid, new_pid));
2270         return 0;
2271 }
2272
2273 #else /* CONFIG_PROC_FS */
2274
2275 int proc_dostring(ctl_table *table, int write, struct file *filp,
2276                   void __user *buffer, size_t *lenp, loff_t *ppos)
2277 {
2278         return -ENOSYS;
2279 }
2280
2281 int proc_dointvec(ctl_table *table, int write, struct file *filp,
2282                   void __user *buffer, size_t *lenp, loff_t *ppos)
2283 {
2284         return -ENOSYS;
2285 }
2286
2287 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2288                         void __user *buffer, size_t *lenp, loff_t *ppos)
2289 {
2290         return -ENOSYS;
2291 }
2292
2293 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2294                     void __user *buffer, size_t *lenp, loff_t *ppos)
2295 {
2296         return -ENOSYS;
2297 }
2298
2299 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2300                     void __user *buffer, size_t *lenp, loff_t *ppos)
2301 {
2302         return -ENOSYS;
2303 }
2304
2305 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2306                     void __user *buffer, size_t *lenp, loff_t *ppos)
2307 {
2308         return -ENOSYS;
2309 }
2310
2311 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2312                              void __user *buffer, size_t *lenp, loff_t *ppos)
2313 {
2314         return -ENOSYS;
2315 }
2316
2317 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2318                     void __user *buffer, size_t *lenp, loff_t *ppos)
2319 {
2320         return -ENOSYS;
2321 }
2322
2323 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2324                                       struct file *filp,
2325                                       void __user *buffer,
2326                                       size_t *lenp, loff_t *ppos)
2327 {
2328     return -ENOSYS;
2329 }
2330
2331
2332 #endif /* CONFIG_PROC_FS */
2333
2334
2335 #ifdef CONFIG_SYSCTL_SYSCALL
2336 /*
2337  * General sysctl support routines 
2338  */
2339
2340 /* The generic string strategy routine: */
2341 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2342                   void __user *oldval, size_t __user *oldlenp,
2343                   void __user *newval, size_t newlen)
2344 {
2345         if (!table->data || !table->maxlen) 
2346                 return -ENOTDIR;
2347         
2348         if (oldval && oldlenp) {
2349                 size_t bufsize;
2350                 if (get_user(bufsize, oldlenp))
2351                         return -EFAULT;
2352                 if (bufsize) {
2353                         size_t len = strlen(table->data), copied;
2354
2355                         /* This shouldn't trigger for a well-formed sysctl */
2356                         if (len > table->maxlen)
2357                                 len = table->maxlen;
2358
2359                         /* Copy up to a max of bufsize-1 bytes of the string */
2360                         copied = (len >= bufsize) ? bufsize - 1 : len;
2361
2362                         if (copy_to_user(oldval, table->data, copied) ||
2363                             put_user(0, (char __user *)(oldval + copied)))
2364                                 return -EFAULT;
2365                         if (put_user(len, oldlenp))
2366                                 return -EFAULT;
2367                 }
2368         }
2369         if (newval && newlen) {
2370                 size_t len = newlen;
2371                 if (len > table->maxlen)
2372                         len = table->maxlen;
2373                 if(copy_from_user(table->data, newval, len))
2374                         return -EFAULT;
2375                 if (len == table->maxlen)
2376                         len--;
2377                 ((char *) table->data)[len] = 0;
2378         }
2379         return 1;
2380 }
2381
2382 /*
2383  * This function makes sure that all of the integers in the vector
2384  * are between the minimum and maximum values given in the arrays
2385  * table->extra1 and table->extra2, respectively.
2386  */
2387 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2388                 void __user *oldval, size_t __user *oldlenp,
2389                 void __user *newval, size_t newlen)
2390 {
2391
2392         if (newval && newlen) {
2393                 int __user *vec = (int __user *) newval;
2394                 int *min = (int *) table->extra1;
2395                 int *max = (int *) table->extra2;
2396                 size_t length;
2397                 int i;
2398
2399                 if (newlen % sizeof(int) != 0)
2400                         return -EINVAL;
2401
2402                 if (!table->extra1 && !table->extra2)
2403                         return 0;
2404
2405                 if (newlen > table->maxlen)
2406                         newlen = table->maxlen;
2407                 length = newlen / sizeof(int);
2408
2409                 for (i = 0; i < length; i++) {
2410                         int value;
2411                         if (get_user(value, vec + i))
2412                                 return -EFAULT;
2413                         if (min && value < min[i])
2414                                 return -EINVAL;
2415                         if (max && value > max[i])
2416                                 return -EINVAL;
2417                 }
2418         }
2419         return 0;
2420 }
2421
2422 /* Strategy function to convert jiffies to seconds */ 
2423 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2424                 void __user *oldval, size_t __user *oldlenp,
2425                 void __user *newval, size_t newlen)
2426 {
2427         if (oldval && oldlenp) {
2428                 size_t olen;
2429
2430                 if (get_user(olen, oldlenp))
2431                         return -EFAULT;
2432                 if (olen) {
2433                         int val;
2434
2435                         if (olen < sizeof(int))
2436                                 return -EINVAL;
2437
2438                         val = *(int *)(table->data) / HZ;
2439                         if (put_user(val, (int __user *)oldval))
2440                                 return -EFAULT;
2441                         if (put_user(sizeof(int), oldlenp))
2442                                 return -EFAULT;
2443                 }
2444         }
2445         if (newval && newlen) { 
2446                 int new;
2447                 if (newlen != sizeof(int))
2448                         return -EINVAL; 
2449                 if (get_user(new, (int __user *)newval))
2450                         return -EFAULT;
2451                 *(int *)(table->data) = new*HZ; 
2452         }
2453         return 1;
2454 }
2455
2456 /* Strategy function to convert jiffies to seconds */ 
2457 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2458                 void __user *oldval, size_t __user *oldlenp,
2459                 void __user *newval, size_t newlen)
2460 {
2461         if (oldval && oldlenp) {
2462                 size_t olen;
2463
2464                 if (get_user(olen, oldlenp))
2465                         return -EFAULT;
2466                 if (olen) {
2467                         int val;
2468
2469                         if (olen < sizeof(int))
2470                                 return -EINVAL;
2471
2472                         val = jiffies_to_msecs(*(int *)(table->data));
2473                         if (put_user(val, (int __user *)oldval))
2474                                 return -EFAULT;
2475                         if (put_user(sizeof(int), oldlenp))
2476                                 return -EFAULT;
2477                 }
2478         }
2479         if (newval && newlen) { 
2480                 int new;
2481                 if (newlen != sizeof(int))
2482                         return -EINVAL; 
2483                 if (get_user(new, (int __user *)newval))
2484                         return -EFAULT;
2485                 *(int *)(table->data) = msecs_to_jiffies(new);
2486         }
2487         return 1;
2488 }
2489
2490
2491
2492 #else /* CONFIG_SYSCTL_SYSCALL */
2493
2494
2495 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
2496 {
2497         static int msg_count;
2498         struct __sysctl_args tmp;
2499         int name[CTL_MAXNAME];
2500         int i;
2501
2502         /* Read in the sysctl name for better debug message logging */
2503         if (copy_from_user(&tmp, args, sizeof(tmp)))
2504                 return -EFAULT;
2505         if (tmp.nlen <= 0 || tmp.nlen >= CTL_MAXNAME)
2506                 return -ENOTDIR;
2507         for (i = 0; i < tmp.nlen; i++)
2508                 if (get_user(name[i], tmp.name + i))
2509                         return -EFAULT;
2510
2511         /* Ignore accesses to kernel.version */
2512         if ((tmp.nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION))
2513                 goto out;
2514
2515         if (msg_count < 5) {
2516                 msg_count++;
2517                 printk(KERN_INFO
2518                         "warning: process `%s' used the removed sysctl "
2519                         "system call with ", current->comm);
2520                 for (i = 0; i < tmp.nlen; i++)
2521                         printk("%d.", name[i]);
2522                 printk("\n");
2523         }
2524 out:
2525         return -ENOSYS;
2526 }
2527
2528 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2529                   void __user *oldval, size_t __user *oldlenp,
2530                   void __user *newval, size_t newlen)
2531 {
2532         return -ENOSYS;
2533 }
2534
2535 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2536                 void __user *oldval, size_t __user *oldlenp,
2537                 void __user *newval, size_t newlen)
2538 {
2539         return -ENOSYS;
2540 }
2541
2542 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2543                 void __user *oldval, size_t __user *oldlenp,
2544                 void __user *newval, size_t newlen)
2545 {
2546         return -ENOSYS;
2547 }
2548
2549 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2550                 void __user *oldval, size_t __user *oldlenp,
2551                 void __user *newval, size_t newlen)
2552 {
2553         return -ENOSYS;
2554 }
2555
2556 #endif /* CONFIG_SYSCTL_SYSCALL */
2557
2558 /*
2559  * No sense putting this after each symbol definition, twice,
2560  * exception granted :-)
2561  */
2562 EXPORT_SYMBOL(proc_dointvec);
2563 EXPORT_SYMBOL(proc_dointvec_jiffies);
2564 EXPORT_SYMBOL(proc_dointvec_minmax);
2565 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2566 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2567 EXPORT_SYMBOL(proc_dostring);
2568 EXPORT_SYMBOL(proc_doulongvec_minmax);
2569 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2570 EXPORT_SYMBOL(register_sysctl_table);
2571 EXPORT_SYMBOL(sysctl_intvec);
2572 EXPORT_SYMBOL(sysctl_jiffies);
2573 EXPORT_SYMBOL(sysctl_ms_jiffies);
2574 EXPORT_SYMBOL(sysctl_string);
2575 EXPORT_SYMBOL(unregister_sysctl_table);