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