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