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