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