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