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