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