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