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