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