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