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