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