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