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