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