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