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