[SCSI] iscsi bugfixes: send correct error values to userspace
[linux-2.6] / 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_MIN_UNMAPPED,
937                 .procname       = "min_unmapped_ratio",
938                 .data           = &sysctl_min_unmapped_ratio,
939                 .maxlen         = sizeof(sysctl_min_unmapped_ratio),
940                 .mode           = 0644,
941                 .proc_handler   = &sysctl_min_unmapped_ratio_sysctl_handler,
942                 .strategy       = &sysctl_intvec,
943                 .extra1         = &zero,
944                 .extra2         = &one_hundred,
945         },
946 #endif
947 #ifdef CONFIG_X86_32
948         {
949                 .ctl_name       = VM_VDSO_ENABLED,
950                 .procname       = "vdso_enabled",
951                 .data           = &vdso_enabled,
952                 .maxlen         = sizeof(vdso_enabled),
953                 .mode           = 0644,
954                 .proc_handler   = &proc_dointvec,
955                 .strategy       = &sysctl_intvec,
956                 .extra1         = &zero,
957         },
958 #endif
959         { .ctl_name = 0 }
960 };
961
962 static ctl_table fs_table[] = {
963         {
964                 .ctl_name       = FS_NRINODE,
965                 .procname       = "inode-nr",
966                 .data           = &inodes_stat,
967                 .maxlen         = 2*sizeof(int),
968                 .mode           = 0444,
969                 .proc_handler   = &proc_dointvec,
970         },
971         {
972                 .ctl_name       = FS_STATINODE,
973                 .procname       = "inode-state",
974                 .data           = &inodes_stat,
975                 .maxlen         = 7*sizeof(int),
976                 .mode           = 0444,
977                 .proc_handler   = &proc_dointvec,
978         },
979         {
980                 .ctl_name       = FS_NRFILE,
981                 .procname       = "file-nr",
982                 .data           = &files_stat,
983                 .maxlen         = 3*sizeof(int),
984                 .mode           = 0444,
985                 .proc_handler   = &proc_nr_files,
986         },
987         {
988                 .ctl_name       = FS_MAXFILE,
989                 .procname       = "file-max",
990                 .data           = &files_stat.max_files,
991                 .maxlen         = sizeof(int),
992                 .mode           = 0644,
993                 .proc_handler   = &proc_dointvec,
994         },
995         {
996                 .ctl_name       = FS_DENTRY,
997                 .procname       = "dentry-state",
998                 .data           = &dentry_stat,
999                 .maxlen         = 6*sizeof(int),
1000                 .mode           = 0444,
1001                 .proc_handler   = &proc_dointvec,
1002         },
1003         {
1004                 .ctl_name       = FS_OVERFLOWUID,
1005                 .procname       = "overflowuid",
1006                 .data           = &fs_overflowuid,
1007                 .maxlen         = sizeof(int),
1008                 .mode           = 0644,
1009                 .proc_handler   = &proc_dointvec_minmax,
1010                 .strategy       = &sysctl_intvec,
1011                 .extra1         = &minolduid,
1012                 .extra2         = &maxolduid,
1013         },
1014         {
1015                 .ctl_name       = FS_OVERFLOWGID,
1016                 .procname       = "overflowgid",
1017                 .data           = &fs_overflowgid,
1018                 .maxlen         = sizeof(int),
1019                 .mode           = 0644,
1020                 .proc_handler   = &proc_dointvec_minmax,
1021                 .strategy       = &sysctl_intvec,
1022                 .extra1         = &minolduid,
1023                 .extra2         = &maxolduid,
1024         },
1025         {
1026                 .ctl_name       = FS_LEASES,
1027                 .procname       = "leases-enable",
1028                 .data           = &leases_enable,
1029                 .maxlen         = sizeof(int),
1030                 .mode           = 0644,
1031                 .proc_handler   = &proc_dointvec,
1032         },
1033 #ifdef CONFIG_DNOTIFY
1034         {
1035                 .ctl_name       = FS_DIR_NOTIFY,
1036                 .procname       = "dir-notify-enable",
1037                 .data           = &dir_notify_enable,
1038                 .maxlen         = sizeof(int),
1039                 .mode           = 0644,
1040                 .proc_handler   = &proc_dointvec,
1041         },
1042 #endif
1043 #ifdef CONFIG_MMU
1044         {
1045                 .ctl_name       = FS_LEASE_TIME,
1046                 .procname       = "lease-break-time",
1047                 .data           = &lease_break_time,
1048                 .maxlen         = sizeof(int),
1049                 .mode           = 0644,
1050                 .proc_handler   = &proc_dointvec,
1051         },
1052         {
1053                 .ctl_name       = FS_AIO_NR,
1054                 .procname       = "aio-nr",
1055                 .data           = &aio_nr,
1056                 .maxlen         = sizeof(aio_nr),
1057                 .mode           = 0444,
1058                 .proc_handler   = &proc_doulongvec_minmax,
1059         },
1060         {
1061                 .ctl_name       = FS_AIO_MAX_NR,
1062                 .procname       = "aio-max-nr",
1063                 .data           = &aio_max_nr,
1064                 .maxlen         = sizeof(aio_max_nr),
1065                 .mode           = 0644,
1066                 .proc_handler   = &proc_doulongvec_minmax,
1067         },
1068 #ifdef CONFIG_INOTIFY_USER
1069         {
1070                 .ctl_name       = FS_INOTIFY,
1071                 .procname       = "inotify",
1072                 .mode           = 0555,
1073                 .child          = inotify_table,
1074         },
1075 #endif  
1076 #endif
1077         {
1078                 .ctl_name       = KERN_SETUID_DUMPABLE,
1079                 .procname       = "suid_dumpable",
1080                 .data           = &suid_dumpable,
1081                 .maxlen         = sizeof(int),
1082                 .mode           = 0644,
1083                 .proc_handler   = &proc_dointvec,
1084         },
1085         { .ctl_name = 0 }
1086 };
1087
1088 static ctl_table debug_table[] = {
1089         { .ctl_name = 0 }
1090 };
1091
1092 static ctl_table dev_table[] = {
1093         { .ctl_name = 0 }
1094 };
1095
1096 extern void init_irq_proc (void);
1097
1098 static DEFINE_SPINLOCK(sysctl_lock);
1099
1100 /* called under sysctl_lock */
1101 static int use_table(struct ctl_table_header *p)
1102 {
1103         if (unlikely(p->unregistering))
1104                 return 0;
1105         p->used++;
1106         return 1;
1107 }
1108
1109 /* called under sysctl_lock */
1110 static void unuse_table(struct ctl_table_header *p)
1111 {
1112         if (!--p->used)
1113                 if (unlikely(p->unregistering))
1114                         complete(p->unregistering);
1115 }
1116
1117 /* called under sysctl_lock, will reacquire if has to wait */
1118 static void start_unregistering(struct ctl_table_header *p)
1119 {
1120         /*
1121          * if p->used is 0, nobody will ever touch that entry again;
1122          * we'll eliminate all paths to it before dropping sysctl_lock
1123          */
1124         if (unlikely(p->used)) {
1125                 struct completion wait;
1126                 init_completion(&wait);
1127                 p->unregistering = &wait;
1128                 spin_unlock(&sysctl_lock);
1129                 wait_for_completion(&wait);
1130                 spin_lock(&sysctl_lock);
1131         }
1132         /*
1133          * do not remove from the list until nobody holds it; walking the
1134          * list in do_sysctl() relies on that.
1135          */
1136         list_del_init(&p->ctl_entry);
1137 }
1138
1139 void __init sysctl_init(void)
1140 {
1141 #ifdef CONFIG_PROC_FS
1142         register_proc_table(root_table, proc_sys_root, &root_table_header);
1143         init_irq_proc();
1144 #endif
1145 }
1146
1147 int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
1148                void __user *newval, size_t newlen)
1149 {
1150         struct list_head *tmp;
1151         int error = -ENOTDIR;
1152
1153         if (nlen <= 0 || nlen >= CTL_MAXNAME)
1154                 return -ENOTDIR;
1155         if (oldval) {
1156                 int old_len;
1157                 if (!oldlenp || get_user(old_len, oldlenp))
1158                         return -EFAULT;
1159         }
1160         spin_lock(&sysctl_lock);
1161         tmp = &root_table_header.ctl_entry;
1162         do {
1163                 struct ctl_table_header *head =
1164                         list_entry(tmp, struct ctl_table_header, ctl_entry);
1165                 void *context = NULL;
1166
1167                 if (!use_table(head))
1168                         continue;
1169
1170                 spin_unlock(&sysctl_lock);
1171
1172                 error = parse_table(name, nlen, oldval, oldlenp, 
1173                                         newval, newlen, head->ctl_table,
1174                                         &context);
1175                 kfree(context);
1176
1177                 spin_lock(&sysctl_lock);
1178                 unuse_table(head);
1179                 if (error != -ENOTDIR)
1180                         break;
1181         } while ((tmp = tmp->next) != &root_table_header.ctl_entry);
1182         spin_unlock(&sysctl_lock);
1183         return error;
1184 }
1185
1186 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
1187 {
1188         struct __sysctl_args tmp;
1189         int error;
1190
1191         if (copy_from_user(&tmp, args, sizeof(tmp)))
1192                 return -EFAULT;
1193
1194         lock_kernel();
1195         error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
1196                           tmp.newval, tmp.newlen);
1197         unlock_kernel();
1198         return error;
1199 }
1200
1201 /*
1202  * ctl_perm does NOT grant the superuser all rights automatically, because
1203  * some sysctl variables are readonly even to root.
1204  */
1205
1206 static int test_perm(int mode, int op)
1207 {
1208         if (!current->euid)
1209                 mode >>= 6;
1210         else if (in_egroup_p(0))
1211                 mode >>= 3;
1212         if ((mode & op & 0007) == op)
1213                 return 0;
1214         return -EACCES;
1215 }
1216
1217 static inline int ctl_perm(ctl_table *table, int op)
1218 {
1219         int error;
1220         error = security_sysctl(table, op);
1221         if (error)
1222                 return error;
1223         return test_perm(table->mode, op);
1224 }
1225
1226 static int parse_table(int __user *name, int nlen,
1227                        void __user *oldval, size_t __user *oldlenp,
1228                        void __user *newval, size_t newlen,
1229                        ctl_table *table, void **context)
1230 {
1231         int n;
1232 repeat:
1233         if (!nlen)
1234                 return -ENOTDIR;
1235         if (get_user(n, name))
1236                 return -EFAULT;
1237         for ( ; table->ctl_name; table++) {
1238                 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
1239                         int error;
1240                         if (table->child) {
1241                                 if (ctl_perm(table, 001))
1242                                         return -EPERM;
1243                                 if (table->strategy) {
1244                                         error = table->strategy(
1245                                                 table, name, nlen,
1246                                                 oldval, oldlenp,
1247                                                 newval, newlen, context);
1248                                         if (error)
1249                                                 return error;
1250                                 }
1251                                 name++;
1252                                 nlen--;
1253                                 table = table->child;
1254                                 goto repeat;
1255                         }
1256                         error = do_sysctl_strategy(table, name, nlen,
1257                                                    oldval, oldlenp,
1258                                                    newval, newlen, context);
1259                         return error;
1260                 }
1261         }
1262         return -ENOTDIR;
1263 }
1264
1265 /* Perform the actual read/write of a sysctl table entry. */
1266 int do_sysctl_strategy (ctl_table *table, 
1267                         int __user *name, int nlen,
1268                         void __user *oldval, size_t __user *oldlenp,
1269                         void __user *newval, size_t newlen, void **context)
1270 {
1271         int op = 0, rc;
1272         size_t len;
1273
1274         if (oldval)
1275                 op |= 004;
1276         if (newval) 
1277                 op |= 002;
1278         if (ctl_perm(table, op))
1279                 return -EPERM;
1280
1281         if (table->strategy) {
1282                 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1283                                      newval, newlen, context);
1284                 if (rc < 0)
1285                         return rc;
1286                 if (rc > 0)
1287                         return 0;
1288         }
1289
1290         /* If there is no strategy routine, or if the strategy returns
1291          * zero, proceed with automatic r/w */
1292         if (table->data && table->maxlen) {
1293                 if (oldval && oldlenp) {
1294                         if (get_user(len, oldlenp))
1295                                 return -EFAULT;
1296                         if (len) {
1297                                 if (len > table->maxlen)
1298                                         len = table->maxlen;
1299                                 if(copy_to_user(oldval, table->data, len))
1300                                         return -EFAULT;
1301                                 if(put_user(len, oldlenp))
1302                                         return -EFAULT;
1303                         }
1304                 }
1305                 if (newval && newlen) {
1306                         len = newlen;
1307                         if (len > table->maxlen)
1308                                 len = table->maxlen;
1309                         if(copy_from_user(table->data, newval, len))
1310                                 return -EFAULT;
1311                 }
1312         }
1313         return 0;
1314 }
1315
1316 /**
1317  * register_sysctl_table - register a sysctl hierarchy
1318  * @table: the top-level table structure
1319  * @insert_at_head: whether the entry should be inserted in front or at the end
1320  *
1321  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1322  * array. An entry with a ctl_name of 0 terminates the table. 
1323  *
1324  * The members of the &ctl_table structure are used as follows:
1325  *
1326  * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1327  *            must be unique within that level of sysctl
1328  *
1329  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1330  *            enter a sysctl file
1331  *
1332  * data - a pointer to data for use by proc_handler
1333  *
1334  * maxlen - the maximum size in bytes of the data
1335  *
1336  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1337  *
1338  * child - a pointer to the child sysctl table if this entry is a directory, or
1339  *         %NULL.
1340  *
1341  * proc_handler - the text handler routine (described below)
1342  *
1343  * strategy - the strategy routine (described below)
1344  *
1345  * de - for internal use by the sysctl routines
1346  *
1347  * extra1, extra2 - extra pointers usable by the proc handler routines
1348  *
1349  * Leaf nodes in the sysctl tree will be represented by a single file
1350  * under /proc; non-leaf nodes will be represented by directories.
1351  *
1352  * sysctl(2) can automatically manage read and write requests through
1353  * the sysctl table.  The data and maxlen fields of the ctl_table
1354  * struct enable minimal validation of the values being written to be
1355  * performed, and the mode field allows minimal authentication.
1356  *
1357  * More sophisticated management can be enabled by the provision of a
1358  * strategy routine with the table entry.  This will be called before
1359  * any automatic read or write of the data is performed.
1360  *
1361  * The strategy routine may return
1362  *
1363  * < 0 - Error occurred (error is passed to user process)
1364  *
1365  * 0   - OK - proceed with automatic read or write.
1366  *
1367  * > 0 - OK - read or write has been done by the strategy routine, so
1368  *       return immediately.
1369  *
1370  * There must be a proc_handler routine for any terminal nodes
1371  * mirrored under /proc/sys (non-terminals are handled by a built-in
1372  * directory handler).  Several default handlers are available to
1373  * cover common cases -
1374  *
1375  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1376  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 
1377  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1378  *
1379  * It is the handler's job to read the input buffer from user memory
1380  * and process it. The handler should return 0 on success.
1381  *
1382  * This routine returns %NULL on a failure to register, and a pointer
1383  * to the table header on success.
1384  */
1385 struct ctl_table_header *register_sysctl_table(ctl_table * table, 
1386                                                int insert_at_head)
1387 {
1388         struct ctl_table_header *tmp;
1389         tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1390         if (!tmp)
1391                 return NULL;
1392         tmp->ctl_table = table;
1393         INIT_LIST_HEAD(&tmp->ctl_entry);
1394         tmp->used = 0;
1395         tmp->unregistering = NULL;
1396         spin_lock(&sysctl_lock);
1397         if (insert_at_head)
1398                 list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
1399         else
1400                 list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1401         spin_unlock(&sysctl_lock);
1402 #ifdef CONFIG_PROC_FS
1403         register_proc_table(table, proc_sys_root, tmp);
1404 #endif
1405         return tmp;
1406 }
1407
1408 /**
1409  * unregister_sysctl_table - unregister a sysctl table hierarchy
1410  * @header: the header returned from register_sysctl_table
1411  *
1412  * Unregisters the sysctl table and all children. proc entries may not
1413  * actually be removed until they are no longer used by anyone.
1414  */
1415 void unregister_sysctl_table(struct ctl_table_header * header)
1416 {
1417         might_sleep();
1418         spin_lock(&sysctl_lock);
1419         start_unregistering(header);
1420 #ifdef CONFIG_PROC_FS
1421         unregister_proc_table(header->ctl_table, proc_sys_root);
1422 #endif
1423         spin_unlock(&sysctl_lock);
1424         kfree(header);
1425 }
1426
1427 /*
1428  * /proc/sys support
1429  */
1430
1431 #ifdef CONFIG_PROC_FS
1432
1433 /* Scan the sysctl entries in table and add them all into /proc */
1434 static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
1435 {
1436         struct proc_dir_entry *de;
1437         int len;
1438         mode_t mode;
1439         
1440         for (; table->ctl_name; table++) {
1441                 /* Can't do anything without a proc name. */
1442                 if (!table->procname)
1443                         continue;
1444                 /* Maybe we can't do anything with it... */
1445                 if (!table->proc_handler && !table->child) {
1446                         printk(KERN_WARNING "SYSCTL: Can't register %s\n",
1447                                 table->procname);
1448                         continue;
1449                 }
1450
1451                 len = strlen(table->procname);
1452                 mode = table->mode;
1453
1454                 de = NULL;
1455                 if (table->proc_handler)
1456                         mode |= S_IFREG;
1457                 else {
1458                         mode |= S_IFDIR;
1459                         for (de = root->subdir; de; de = de->next) {
1460                                 if (proc_match(len, table->procname, de))
1461                                         break;
1462                         }
1463                         /* If the subdir exists already, de is non-NULL */
1464                 }
1465
1466                 if (!de) {
1467                         de = create_proc_entry(table->procname, mode, root);
1468                         if (!de)
1469                                 continue;
1470                         de->set = set;
1471                         de->data = (void *) table;
1472                         if (table->proc_handler)
1473                                 de->proc_fops = &proc_sys_file_operations;
1474                 }
1475                 table->de = de;
1476                 if (de->mode & S_IFDIR)
1477                         register_proc_table(table->child, de, set);
1478         }
1479 }
1480
1481 /*
1482  * Unregister a /proc sysctl table and any subdirectories.
1483  */
1484 static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
1485 {
1486         struct proc_dir_entry *de;
1487         for (; table->ctl_name; table++) {
1488                 if (!(de = table->de))
1489                         continue;
1490                 if (de->mode & S_IFDIR) {
1491                         if (!table->child) {
1492                                 printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
1493                                 continue;
1494                         }
1495                         unregister_proc_table(table->child, de);
1496
1497                         /* Don't unregister directories which still have entries.. */
1498                         if (de->subdir)
1499                                 continue;
1500                 }
1501
1502                 /*
1503                  * In any case, mark the entry as goner; we'll keep it
1504                  * around if it's busy, but we'll know to do nothing with
1505                  * its fields.  We are under sysctl_lock here.
1506                  */
1507                 de->data = NULL;
1508
1509                 /* Don't unregister proc entries that are still being used.. */
1510                 if (atomic_read(&de->count))
1511                         continue;
1512
1513                 table->de = NULL;
1514                 remove_proc_entry(table->procname, root);
1515         }
1516 }
1517
1518 static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
1519                           size_t count, loff_t *ppos)
1520 {
1521         int op;
1522         struct proc_dir_entry *de = PDE(file->f_dentry->d_inode);
1523         struct ctl_table *table;
1524         size_t res;
1525         ssize_t error = -ENOTDIR;
1526         
1527         spin_lock(&sysctl_lock);
1528         if (de && de->data && use_table(de->set)) {
1529                 /*
1530                  * at that point we know that sysctl was not unregistered
1531                  * and won't be until we finish
1532                  */
1533                 spin_unlock(&sysctl_lock);
1534                 table = (struct ctl_table *) de->data;
1535                 if (!table || !table->proc_handler)
1536                         goto out;
1537                 error = -EPERM;
1538                 op = (write ? 002 : 004);
1539                 if (ctl_perm(table, op))
1540                         goto out;
1541                 
1542                 /* careful: calling conventions are nasty here */
1543                 res = count;
1544                 error = (*table->proc_handler)(table, write, file,
1545                                                 buf, &res, ppos);
1546                 if (!error)
1547                         error = res;
1548         out:
1549                 spin_lock(&sysctl_lock);
1550                 unuse_table(de->set);
1551         }
1552         spin_unlock(&sysctl_lock);
1553         return error;
1554 }
1555
1556 static int proc_opensys(struct inode *inode, struct file *file)
1557 {
1558         if (file->f_mode & FMODE_WRITE) {
1559                 /*
1560                  * sysctl entries that are not writable,
1561                  * are _NOT_ writable, capabilities or not.
1562                  */
1563                 if (!(inode->i_mode & S_IWUSR))
1564                         return -EPERM;
1565         }
1566
1567         return 0;
1568 }
1569
1570 static ssize_t proc_readsys(struct file * file, char __user * buf,
1571                             size_t count, loff_t *ppos)
1572 {
1573         return do_rw_proc(0, file, buf, count, ppos);
1574 }
1575
1576 static ssize_t proc_writesys(struct file * file, const char __user * buf,
1577                              size_t count, loff_t *ppos)
1578 {
1579         return do_rw_proc(1, file, (char __user *) buf, count, ppos);
1580 }
1581
1582 /**
1583  * proc_dostring - read a string sysctl
1584  * @table: the sysctl table
1585  * @write: %TRUE if this is a write to the sysctl file
1586  * @filp: the file structure
1587  * @buffer: the user buffer
1588  * @lenp: the size of the user buffer
1589  * @ppos: file position
1590  *
1591  * Reads/writes a string from/to the user buffer. If the kernel
1592  * buffer provided is not large enough to hold the string, the
1593  * string is truncated. The copied string is %NULL-terminated.
1594  * If the string is being read by the user process, it is copied
1595  * and a newline '\n' is added. It is truncated if the buffer is
1596  * not large enough.
1597  *
1598  * Returns 0 on success.
1599  */
1600 int proc_dostring(ctl_table *table, int write, struct file *filp,
1601                   void __user *buffer, size_t *lenp, loff_t *ppos)
1602 {
1603         size_t len;
1604         char __user *p;
1605         char c;
1606         
1607         if (!table->data || !table->maxlen || !*lenp ||
1608             (*ppos && !write)) {
1609                 *lenp = 0;
1610                 return 0;
1611         }
1612         
1613         if (write) {
1614                 len = 0;
1615                 p = buffer;
1616                 while (len < *lenp) {
1617                         if (get_user(c, p++))
1618                                 return -EFAULT;
1619                         if (c == 0 || c == '\n')
1620                                 break;
1621                         len++;
1622                 }
1623                 if (len >= table->maxlen)
1624                         len = table->maxlen-1;
1625                 if(copy_from_user(table->data, buffer, len))
1626                         return -EFAULT;
1627                 ((char *) table->data)[len] = 0;
1628                 *ppos += *lenp;
1629         } else {
1630                 len = strlen(table->data);
1631                 if (len > table->maxlen)
1632                         len = table->maxlen;
1633                 if (len > *lenp)
1634                         len = *lenp;
1635                 if (len)
1636                         if(copy_to_user(buffer, table->data, len))
1637                                 return -EFAULT;
1638                 if (len < *lenp) {
1639                         if(put_user('\n', ((char __user *) buffer) + len))
1640                                 return -EFAULT;
1641                         len++;
1642                 }
1643                 *lenp = len;
1644                 *ppos += len;
1645         }
1646         return 0;
1647 }
1648
1649 /*
1650  *      Special case of dostring for the UTS structure. This has locks
1651  *      to observe. Should this be in kernel/sys.c ????
1652  */
1653  
1654 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
1655                   void __user *buffer, size_t *lenp, loff_t *ppos)
1656 {
1657         int r;
1658
1659         if (!write) {
1660                 down_read(&uts_sem);
1661                 r=proc_dostring(table,0,filp,buffer,lenp, ppos);
1662                 up_read(&uts_sem);
1663         } else {
1664                 down_write(&uts_sem);
1665                 r=proc_dostring(table,1,filp,buffer,lenp, ppos);
1666                 up_write(&uts_sem);
1667         }
1668         return r;
1669 }
1670
1671 static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
1672                                  int *valp,
1673                                  int write, void *data)
1674 {
1675         if (write) {
1676                 *valp = *negp ? -*lvalp : *lvalp;
1677         } else {
1678                 int val = *valp;
1679                 if (val < 0) {
1680                         *negp = -1;
1681                         *lvalp = (unsigned long)-val;
1682                 } else {
1683                         *negp = 0;
1684                         *lvalp = (unsigned long)val;
1685                 }
1686         }
1687         return 0;
1688 }
1689
1690 static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
1691                   void __user *buffer, size_t *lenp, loff_t *ppos,
1692                   int (*conv)(int *negp, unsigned long *lvalp, int *valp,
1693                               int write, void *data),
1694                   void *data)
1695 {
1696 #define TMPBUFLEN 21
1697         int *i, vleft, first=1, neg, val;
1698         unsigned long lval;
1699         size_t left, len;
1700         
1701         char buf[TMPBUFLEN], *p;
1702         char __user *s = buffer;
1703         
1704         if (!table->data || !table->maxlen || !*lenp ||
1705             (*ppos && !write)) {
1706                 *lenp = 0;
1707                 return 0;
1708         }
1709         
1710         i = (int *) table->data;
1711         vleft = table->maxlen / sizeof(*i);
1712         left = *lenp;
1713
1714         if (!conv)
1715                 conv = do_proc_dointvec_conv;
1716
1717         for (; left && vleft--; i++, first=0) {
1718                 if (write) {
1719                         while (left) {
1720                                 char c;
1721                                 if (get_user(c, s))
1722                                         return -EFAULT;
1723                                 if (!isspace(c))
1724                                         break;
1725                                 left--;
1726                                 s++;
1727                         }
1728                         if (!left)
1729                                 break;
1730                         neg = 0;
1731                         len = left;
1732                         if (len > sizeof(buf) - 1)
1733                                 len = sizeof(buf) - 1;
1734                         if (copy_from_user(buf, s, len))
1735                                 return -EFAULT;
1736                         buf[len] = 0;
1737                         p = buf;
1738                         if (*p == '-' && left > 1) {
1739                                 neg = 1;
1740                                 left--, p++;
1741                         }
1742                         if (*p < '0' || *p > '9')
1743                                 break;
1744
1745                         lval = simple_strtoul(p, &p, 0);
1746
1747                         len = p-buf;
1748                         if ((len < left) && *p && !isspace(*p))
1749                                 break;
1750                         if (neg)
1751                                 val = -val;
1752                         s += len;
1753                         left -= len;
1754
1755                         if (conv(&neg, &lval, i, 1, data))
1756                                 break;
1757                 } else {
1758                         p = buf;
1759                         if (!first)
1760                                 *p++ = '\t';
1761         
1762                         if (conv(&neg, &lval, i, 0, data))
1763                                 break;
1764
1765                         sprintf(p, "%s%lu", neg ? "-" : "", lval);
1766                         len = strlen(buf);
1767                         if (len > left)
1768                                 len = left;
1769                         if(copy_to_user(s, buf, len))
1770                                 return -EFAULT;
1771                         left -= len;
1772                         s += len;
1773                 }
1774         }
1775
1776         if (!write && !first && left) {
1777                 if(put_user('\n', s))
1778                         return -EFAULT;
1779                 left--, s++;
1780         }
1781         if (write) {
1782                 while (left) {
1783                         char c;
1784                         if (get_user(c, s++))
1785                                 return -EFAULT;
1786                         if (!isspace(c))
1787                                 break;
1788                         left--;
1789                 }
1790         }
1791         if (write && first)
1792                 return -EINVAL;
1793         *lenp -= left;
1794         *ppos += *lenp;
1795         return 0;
1796 #undef TMPBUFLEN
1797 }
1798
1799 /**
1800  * proc_dointvec - read a vector of integers
1801  * @table: the sysctl table
1802  * @write: %TRUE if this is a write to the sysctl file
1803  * @filp: the file structure
1804  * @buffer: the user buffer
1805  * @lenp: the size of the user buffer
1806  * @ppos: file position
1807  *
1808  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1809  * values from/to the user buffer, treated as an ASCII string. 
1810  *
1811  * Returns 0 on success.
1812  */
1813 int proc_dointvec(ctl_table *table, int write, struct file *filp,
1814                      void __user *buffer, size_t *lenp, loff_t *ppos)
1815 {
1816     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1817                             NULL,NULL);
1818 }
1819
1820 #define OP_SET  0
1821 #define OP_AND  1
1822 #define OP_OR   2
1823 #define OP_MAX  3
1824 #define OP_MIN  4
1825
1826 static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
1827                                       int *valp,
1828                                       int write, void *data)
1829 {
1830         int op = *(int *)data;
1831         if (write) {
1832                 int val = *negp ? -*lvalp : *lvalp;
1833                 switch(op) {
1834                 case OP_SET:    *valp = val; break;
1835                 case OP_AND:    *valp &= val; break;
1836                 case OP_OR:     *valp |= val; break;
1837                 case OP_MAX:    if(*valp < val)
1838                                         *valp = val;
1839                                 break;
1840                 case OP_MIN:    if(*valp > val)
1841                                 *valp = val;
1842                                 break;
1843                 }
1844         } else {
1845                 int val = *valp;
1846                 if (val < 0) {
1847                         *negp = -1;
1848                         *lvalp = (unsigned long)-val;
1849                 } else {
1850                         *negp = 0;
1851                         *lvalp = (unsigned long)val;
1852                 }
1853         }
1854         return 0;
1855 }
1856
1857 /*
1858  *      init may raise the set.
1859  */
1860  
1861 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
1862                         void __user *buffer, size_t *lenp, loff_t *ppos)
1863 {
1864         int op;
1865
1866         if (!capable(CAP_SYS_MODULE)) {
1867                 return -EPERM;
1868         }
1869
1870         op = (current->pid == 1) ? OP_SET : OP_AND;
1871         return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1872                                 do_proc_dointvec_bset_conv,&op);
1873 }
1874
1875 struct do_proc_dointvec_minmax_conv_param {
1876         int *min;
1877         int *max;
1878 };
1879
1880 static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, 
1881                                         int *valp, 
1882                                         int write, void *data)
1883 {
1884         struct do_proc_dointvec_minmax_conv_param *param = data;
1885         if (write) {
1886                 int val = *negp ? -*lvalp : *lvalp;
1887                 if ((param->min && *param->min > val) ||
1888                     (param->max && *param->max < val))
1889                         return -EINVAL;
1890                 *valp = val;
1891         } else {
1892                 int val = *valp;
1893                 if (val < 0) {
1894                         *negp = -1;
1895                         *lvalp = (unsigned long)-val;
1896                 } else {
1897                         *negp = 0;
1898                         *lvalp = (unsigned long)val;
1899                 }
1900         }
1901         return 0;
1902 }
1903
1904 /**
1905  * proc_dointvec_minmax - read a vector of integers with min/max values
1906  * @table: the sysctl table
1907  * @write: %TRUE if this is a write to the sysctl file
1908  * @filp: the file structure
1909  * @buffer: the user buffer
1910  * @lenp: the size of the user buffer
1911  * @ppos: file position
1912  *
1913  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1914  * values from/to the user buffer, treated as an ASCII string.
1915  *
1916  * This routine will ensure the values are within the range specified by
1917  * table->extra1 (min) and table->extra2 (max).
1918  *
1919  * Returns 0 on success.
1920  */
1921 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
1922                   void __user *buffer, size_t *lenp, loff_t *ppos)
1923 {
1924         struct do_proc_dointvec_minmax_conv_param param = {
1925                 .min = (int *) table->extra1,
1926                 .max = (int *) table->extra2,
1927         };
1928         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
1929                                 do_proc_dointvec_minmax_conv, &param);
1930 }
1931
1932 static int do_proc_doulongvec_minmax(ctl_table *table, int write,
1933                                      struct file *filp,
1934                                      void __user *buffer,
1935                                      size_t *lenp, loff_t *ppos,
1936                                      unsigned long convmul,
1937                                      unsigned long convdiv)
1938 {
1939 #define TMPBUFLEN 21
1940         unsigned long *i, *min, *max, val;
1941         int vleft, first=1, neg;
1942         size_t len, left;
1943         char buf[TMPBUFLEN], *p;
1944         char __user *s = buffer;
1945         
1946         if (!table->data || !table->maxlen || !*lenp ||
1947             (*ppos && !write)) {
1948                 *lenp = 0;
1949                 return 0;
1950         }
1951         
1952         i = (unsigned long *) table->data;
1953         min = (unsigned long *) table->extra1;
1954         max = (unsigned long *) table->extra2;
1955         vleft = table->maxlen / sizeof(unsigned long);
1956         left = *lenp;
1957         
1958         for (; left && vleft--; i++, min++, max++, first=0) {
1959                 if (write) {
1960                         while (left) {
1961                                 char c;
1962                                 if (get_user(c, s))
1963                                         return -EFAULT;
1964                                 if (!isspace(c))
1965                                         break;
1966                                 left--;
1967                                 s++;
1968                         }
1969                         if (!left)
1970                                 break;
1971                         neg = 0;
1972                         len = left;
1973                         if (len > TMPBUFLEN-1)
1974                                 len = TMPBUFLEN-1;
1975                         if (copy_from_user(buf, s, len))
1976                                 return -EFAULT;
1977                         buf[len] = 0;
1978                         p = buf;
1979                         if (*p == '-' && left > 1) {
1980                                 neg = 1;
1981                                 left--, p++;
1982                         }
1983                         if (*p < '0' || *p > '9')
1984                                 break;
1985                         val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
1986                         len = p-buf;
1987                         if ((len < left) && *p && !isspace(*p))
1988                                 break;
1989                         if (neg)
1990                                 val = -val;
1991                         s += len;
1992                         left -= len;
1993
1994                         if(neg)
1995                                 continue;
1996                         if ((min && val < *min) || (max && val > *max))
1997                                 continue;
1998                         *i = val;
1999                 } else {
2000                         p = buf;
2001                         if (!first)
2002                                 *p++ = '\t';
2003                         sprintf(p, "%lu", convdiv * (*i) / convmul);
2004                         len = strlen(buf);
2005                         if (len > left)
2006                                 len = left;
2007                         if(copy_to_user(s, buf, len))
2008                                 return -EFAULT;
2009                         left -= len;
2010                         s += len;
2011                 }
2012         }
2013
2014         if (!write && !first && left) {
2015                 if(put_user('\n', s))
2016                         return -EFAULT;
2017                 left--, s++;
2018         }
2019         if (write) {
2020                 while (left) {
2021                         char c;
2022                         if (get_user(c, s++))
2023                                 return -EFAULT;
2024                         if (!isspace(c))
2025                                 break;
2026                         left--;
2027                 }
2028         }
2029         if (write && first)
2030                 return -EINVAL;
2031         *lenp -= left;
2032         *ppos += *lenp;
2033         return 0;
2034 #undef TMPBUFLEN
2035 }
2036
2037 /**
2038  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2039  * @table: the sysctl table
2040  * @write: %TRUE if this is a write to the sysctl file
2041  * @filp: the file structure
2042  * @buffer: the user buffer
2043  * @lenp: the size of the user buffer
2044  * @ppos: file position
2045  *
2046  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2047  * values from/to the user buffer, treated as an ASCII string.
2048  *
2049  * This routine will ensure the values are within the range specified by
2050  * table->extra1 (min) and table->extra2 (max).
2051  *
2052  * Returns 0 on success.
2053  */
2054 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2055                            void __user *buffer, size_t *lenp, loff_t *ppos)
2056 {
2057     return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
2058 }
2059
2060 /**
2061  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2062  * @table: the sysctl table
2063  * @write: %TRUE if this is a write to the sysctl file
2064  * @filp: the file structure
2065  * @buffer: the user buffer
2066  * @lenp: the size of the user buffer
2067  * @ppos: file position
2068  *
2069  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2070  * values from/to the user buffer, treated as an ASCII string. The values
2071  * are treated as milliseconds, and converted to jiffies when they are stored.
2072  *
2073  * This routine will ensure the values are within the range specified by
2074  * table->extra1 (min) and table->extra2 (max).
2075  *
2076  * Returns 0 on success.
2077  */
2078 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2079                                       struct file *filp,
2080                                       void __user *buffer,
2081                                       size_t *lenp, loff_t *ppos)
2082 {
2083     return do_proc_doulongvec_minmax(table, write, filp, buffer,
2084                                      lenp, ppos, HZ, 1000l);
2085 }
2086
2087
2088 static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
2089                                          int *valp,
2090                                          int write, void *data)
2091 {
2092         if (write) {
2093                 if (*lvalp > LONG_MAX / HZ)
2094                         return 1;
2095                 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2096         } else {
2097                 int val = *valp;
2098                 unsigned long lval;
2099                 if (val < 0) {
2100                         *negp = -1;
2101                         lval = (unsigned long)-val;
2102                 } else {
2103                         *negp = 0;
2104                         lval = (unsigned long)val;
2105                 }
2106                 *lvalp = lval / HZ;
2107         }
2108         return 0;
2109 }
2110
2111 static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
2112                                                 int *valp,
2113                                                 int write, void *data)
2114 {
2115         if (write) {
2116                 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2117                         return 1;
2118                 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2119         } else {
2120                 int val = *valp;
2121                 unsigned long lval;
2122                 if (val < 0) {
2123                         *negp = -1;
2124                         lval = (unsigned long)-val;
2125                 } else {
2126                         *negp = 0;
2127                         lval = (unsigned long)val;
2128                 }
2129                 *lvalp = jiffies_to_clock_t(lval);
2130         }
2131         return 0;
2132 }
2133
2134 static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
2135                                             int *valp,
2136                                             int write, void *data)
2137 {
2138         if (write) {
2139                 *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2140         } else {
2141                 int val = *valp;
2142                 unsigned long lval;
2143                 if (val < 0) {
2144                         *negp = -1;
2145                         lval = (unsigned long)-val;
2146                 } else {
2147                         *negp = 0;
2148                         lval = (unsigned long)val;
2149                 }
2150                 *lvalp = jiffies_to_msecs(lval);
2151         }
2152         return 0;
2153 }
2154
2155 /**
2156  * proc_dointvec_jiffies - read a vector of integers as seconds
2157  * @table: the sysctl table
2158  * @write: %TRUE if this is a write to the sysctl file
2159  * @filp: the file structure
2160  * @buffer: the user buffer
2161  * @lenp: the size of the user buffer
2162  * @ppos: file position
2163  *
2164  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2165  * values from/to the user buffer, treated as an ASCII string. 
2166  * The values read are assumed to be in seconds, and are converted into
2167  * jiffies.
2168  *
2169  * Returns 0 on success.
2170  */
2171 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2172                           void __user *buffer, size_t *lenp, loff_t *ppos)
2173 {
2174     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2175                             do_proc_dointvec_jiffies_conv,NULL);
2176 }
2177
2178 /**
2179  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2180  * @table: the sysctl table
2181  * @write: %TRUE if this is a write to the sysctl file
2182  * @filp: the file structure
2183  * @buffer: the user buffer
2184  * @lenp: the size of the user buffer
2185  * @ppos: pointer to the file position
2186  *
2187  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2188  * values from/to the user buffer, treated as an ASCII string. 
2189  * The values read are assumed to be in 1/USER_HZ seconds, and 
2190  * are converted into jiffies.
2191  *
2192  * Returns 0 on success.
2193  */
2194 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2195                                  void __user *buffer, size_t *lenp, loff_t *ppos)
2196 {
2197     return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2198                             do_proc_dointvec_userhz_jiffies_conv,NULL);
2199 }
2200
2201 /**
2202  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2203  * @table: the sysctl table
2204  * @write: %TRUE if this is a write to the sysctl file
2205  * @filp: the file structure
2206  * @buffer: the user buffer
2207  * @lenp: the size of the user buffer
2208  * @ppos: file position
2209  * @ppos: the current position in the file
2210  *
2211  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2212  * values from/to the user buffer, treated as an ASCII string. 
2213  * The values read are assumed to be in 1/1000 seconds, and 
2214  * are converted into jiffies.
2215  *
2216  * Returns 0 on success.
2217  */
2218 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2219                              void __user *buffer, size_t *lenp, loff_t *ppos)
2220 {
2221         return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
2222                                 do_proc_dointvec_ms_jiffies_conv, NULL);
2223 }
2224
2225 #else /* CONFIG_PROC_FS */
2226
2227 int proc_dostring(ctl_table *table, int write, struct file *filp,
2228                   void __user *buffer, size_t *lenp, loff_t *ppos)
2229 {
2230         return -ENOSYS;
2231 }
2232
2233 static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
2234                             void __user *buffer, size_t *lenp, loff_t *ppos)
2235 {
2236         return -ENOSYS;
2237 }
2238
2239 int proc_dointvec(ctl_table *table, int write, struct file *filp,
2240                   void __user *buffer, size_t *lenp, loff_t *ppos)
2241 {
2242         return -ENOSYS;
2243 }
2244
2245 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2246                         void __user *buffer, size_t *lenp, loff_t *ppos)
2247 {
2248         return -ENOSYS;
2249 }
2250
2251 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2252                     void __user *buffer, size_t *lenp, loff_t *ppos)
2253 {
2254         return -ENOSYS;
2255 }
2256
2257 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2258                     void __user *buffer, size_t *lenp, loff_t *ppos)
2259 {
2260         return -ENOSYS;
2261 }
2262
2263 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2264                     void __user *buffer, size_t *lenp, loff_t *ppos)
2265 {
2266         return -ENOSYS;
2267 }
2268
2269 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2270                              void __user *buffer, size_t *lenp, loff_t *ppos)
2271 {
2272         return -ENOSYS;
2273 }
2274
2275 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2276                     void __user *buffer, size_t *lenp, loff_t *ppos)
2277 {
2278         return -ENOSYS;
2279 }
2280
2281 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2282                                       struct file *filp,
2283                                       void __user *buffer,
2284                                       size_t *lenp, loff_t *ppos)
2285 {
2286     return -ENOSYS;
2287 }
2288
2289
2290 #endif /* CONFIG_PROC_FS */
2291
2292
2293 /*
2294  * General sysctl support routines 
2295  */
2296
2297 /* The generic string strategy routine: */
2298 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2299                   void __user *oldval, size_t __user *oldlenp,
2300                   void __user *newval, size_t newlen, void **context)
2301 {
2302         if (!table->data || !table->maxlen) 
2303                 return -ENOTDIR;
2304         
2305         if (oldval && oldlenp) {
2306                 size_t bufsize;
2307                 if (get_user(bufsize, oldlenp))
2308                         return -EFAULT;
2309                 if (bufsize) {
2310                         size_t len = strlen(table->data), copied;
2311
2312                         /* This shouldn't trigger for a well-formed sysctl */
2313                         if (len > table->maxlen)
2314                                 len = table->maxlen;
2315
2316                         /* Copy up to a max of bufsize-1 bytes of the string */
2317                         copied = (len >= bufsize) ? bufsize - 1 : len;
2318
2319                         if (copy_to_user(oldval, table->data, copied) ||
2320                             put_user(0, (char __user *)(oldval + copied)))
2321                                 return -EFAULT;
2322                         if (put_user(len, oldlenp))
2323                                 return -EFAULT;
2324                 }
2325         }
2326         if (newval && newlen) {
2327                 size_t len = newlen;
2328                 if (len > table->maxlen)
2329                         len = table->maxlen;
2330                 if(copy_from_user(table->data, newval, len))
2331                         return -EFAULT;
2332                 if (len == table->maxlen)
2333                         len--;
2334                 ((char *) table->data)[len] = 0;
2335         }
2336         return 1;
2337 }
2338
2339 /*
2340  * This function makes sure that all of the integers in the vector
2341  * are between the minimum and maximum values given in the arrays
2342  * table->extra1 and table->extra2, respectively.
2343  */
2344 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2345                 void __user *oldval, size_t __user *oldlenp,
2346                 void __user *newval, size_t newlen, void **context)
2347 {
2348
2349         if (newval && newlen) {
2350                 int __user *vec = (int __user *) newval;
2351                 int *min = (int *) table->extra1;
2352                 int *max = (int *) table->extra2;
2353                 size_t length;
2354                 int i;
2355
2356                 if (newlen % sizeof(int) != 0)
2357                         return -EINVAL;
2358
2359                 if (!table->extra1 && !table->extra2)
2360                         return 0;
2361
2362                 if (newlen > table->maxlen)
2363                         newlen = table->maxlen;
2364                 length = newlen / sizeof(int);
2365
2366                 for (i = 0; i < length; i++) {
2367                         int value;
2368                         if (get_user(value, vec + i))
2369                                 return -EFAULT;
2370                         if (min && value < min[i])
2371                                 return -EINVAL;
2372                         if (max && value > max[i])
2373                                 return -EINVAL;
2374                 }
2375         }
2376         return 0;
2377 }
2378
2379 /* Strategy function to convert jiffies to seconds */ 
2380 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2381                 void __user *oldval, size_t __user *oldlenp,
2382                 void __user *newval, size_t newlen, void **context)
2383 {
2384         if (oldval) {
2385                 size_t olen;
2386                 if (oldlenp) { 
2387                         if (get_user(olen, oldlenp))
2388                                 return -EFAULT;
2389                         if (olen!=sizeof(int))
2390                                 return -EINVAL; 
2391                 }
2392                 if (put_user(*(int *)(table->data)/HZ, (int __user *)oldval) ||
2393                     (oldlenp && put_user(sizeof(int),oldlenp)))
2394                         return -EFAULT;
2395         }
2396         if (newval && newlen) { 
2397                 int new;
2398                 if (newlen != sizeof(int))
2399                         return -EINVAL; 
2400                 if (get_user(new, (int __user *)newval))
2401                         return -EFAULT;
2402                 *(int *)(table->data) = new*HZ; 
2403         }
2404         return 1;
2405 }
2406
2407 /* Strategy function to convert jiffies to seconds */ 
2408 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2409                 void __user *oldval, size_t __user *oldlenp,
2410                 void __user *newval, size_t newlen, void **context)
2411 {
2412         if (oldval) {
2413                 size_t olen;
2414                 if (oldlenp) { 
2415                         if (get_user(olen, oldlenp))
2416                                 return -EFAULT;
2417                         if (olen!=sizeof(int))
2418                                 return -EINVAL; 
2419                 }
2420                 if (put_user(jiffies_to_msecs(*(int *)(table->data)), (int __user *)oldval) ||
2421                     (oldlenp && put_user(sizeof(int),oldlenp)))
2422                         return -EFAULT;
2423         }
2424         if (newval && newlen) { 
2425                 int new;
2426                 if (newlen != sizeof(int))
2427                         return -EINVAL; 
2428                 if (get_user(new, (int __user *)newval))
2429                         return -EFAULT;
2430                 *(int *)(table->data) = msecs_to_jiffies(new);
2431         }
2432         return 1;
2433 }
2434
2435 #else /* CONFIG_SYSCTL */
2436
2437
2438 asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
2439 {
2440         return -ENOSYS;
2441 }
2442
2443 int sysctl_string(ctl_table *table, int __user *name, int nlen,
2444                   void __user *oldval, size_t __user *oldlenp,
2445                   void __user *newval, size_t newlen, void **context)
2446 {
2447         return -ENOSYS;
2448 }
2449
2450 int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
2451                 void __user *oldval, size_t __user *oldlenp,
2452                 void __user *newval, size_t newlen, void **context)
2453 {
2454         return -ENOSYS;
2455 }
2456
2457 int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
2458                 void __user *oldval, size_t __user *oldlenp,
2459                 void __user *newval, size_t newlen, void **context)
2460 {
2461         return -ENOSYS;
2462 }
2463
2464 int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
2465                 void __user *oldval, size_t __user *oldlenp,
2466                 void __user *newval, size_t newlen, void **context)
2467 {
2468         return -ENOSYS;
2469 }
2470
2471 int proc_dostring(ctl_table *table, int write, struct file *filp,
2472                   void __user *buffer, size_t *lenp, loff_t *ppos)
2473 {
2474         return -ENOSYS;
2475 }
2476
2477 int proc_dointvec(ctl_table *table, int write, struct file *filp,
2478                   void __user *buffer, size_t *lenp, loff_t *ppos)
2479 {
2480         return -ENOSYS;
2481 }
2482
2483 int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
2484                         void __user *buffer, size_t *lenp, loff_t *ppos)
2485 {
2486         return -ENOSYS;
2487 }
2488
2489 int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
2490                     void __user *buffer, size_t *lenp, loff_t *ppos)
2491 {
2492         return -ENOSYS;
2493 }
2494
2495 int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
2496                           void __user *buffer, size_t *lenp, loff_t *ppos)
2497 {
2498         return -ENOSYS;
2499 }
2500
2501 int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
2502                           void __user *buffer, size_t *lenp, loff_t *ppos)
2503 {
2504         return -ENOSYS;
2505 }
2506
2507 int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
2508                              void __user *buffer, size_t *lenp, loff_t *ppos)
2509 {
2510         return -ENOSYS;
2511 }
2512
2513 int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
2514                     void __user *buffer, size_t *lenp, loff_t *ppos)
2515 {
2516         return -ENOSYS;
2517 }
2518
2519 int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
2520                                       struct file *filp,
2521                                       void __user *buffer,
2522                                       size_t *lenp, loff_t *ppos)
2523 {
2524     return -ENOSYS;
2525 }
2526
2527 struct ctl_table_header * register_sysctl_table(ctl_table * table, 
2528                                                 int insert_at_head)
2529 {
2530         return NULL;
2531 }
2532
2533 void unregister_sysctl_table(struct ctl_table_header * table)
2534 {
2535 }
2536
2537 #endif /* CONFIG_SYSCTL */
2538
2539 /*
2540  * No sense putting this after each symbol definition, twice,
2541  * exception granted :-)
2542  */
2543 EXPORT_SYMBOL(proc_dointvec);
2544 EXPORT_SYMBOL(proc_dointvec_jiffies);
2545 EXPORT_SYMBOL(proc_dointvec_minmax);
2546 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2547 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2548 EXPORT_SYMBOL(proc_dostring);
2549 EXPORT_SYMBOL(proc_doulongvec_minmax);
2550 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2551 EXPORT_SYMBOL(register_sysctl_table);
2552 EXPORT_SYMBOL(sysctl_intvec);
2553 EXPORT_SYMBOL(sysctl_jiffies);
2554 EXPORT_SYMBOL(sysctl_ms_jiffies);
2555 EXPORT_SYMBOL(sysctl_string);
2556 EXPORT_SYMBOL(unregister_sysctl_table);