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