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