[S390] sparse: fix sparse warnings about missing prototypes
[cascardo/linux.git] / kernel / sysctl.c
1 /*
2  * sysctl.c: General linux system control interface
3  *
4  * Begun 24 March 1995, Stephen Tweedie
5  * Added /proc support, Dec 1995
6  * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
7  * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
8  * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
9  * Dynamic registration fixes, Stephen Tweedie.
10  * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
11  * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
12  *  Horn.
13  * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
14  * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
15  * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
16  *  Wendling.
17  * The list_for_each() macro wasn't appropriate for the sysctl loop.
18  *  Removed it and replaced it with older style, 03/23/00, Bill Wendling
19  */
20
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/swap.h>
24 #include <linux/slab.h>
25 #include <linux/sysctl.h>
26 #include <linux/signal.h>
27 #include <linux/printk.h>
28 #include <linux/proc_fs.h>
29 #include <linux/security.h>
30 #include <linux/ctype.h>
31 #include <linux/kmemcheck.h>
32 #include <linux/fs.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/ratelimit.h>
41 #include <linux/compaction.h>
42 #include <linux/hugetlb.h>
43 #include <linux/initrd.h>
44 #include <linux/key.h>
45 #include <linux/times.h>
46 #include <linux/limits.h>
47 #include <linux/dcache.h>
48 #include <linux/dnotify.h>
49 #include <linux/syscalls.h>
50 #include <linux/vmstat.h>
51 #include <linux/nfs_fs.h>
52 #include <linux/acpi.h>
53 #include <linux/reboot.h>
54 #include <linux/ftrace.h>
55 #include <linux/perf_event.h>
56 #include <linux/kprobes.h>
57 #include <linux/pipe_fs_i.h>
58 #include <linux/oom.h>
59 #include <linux/kmod.h>
60
61 #include <asm/uaccess.h>
62 #include <asm/processor.h>
63
64 #ifdef CONFIG_X86
65 #include <asm/nmi.h>
66 #include <asm/stacktrace.h>
67 #include <asm/io.h>
68 #endif
69 #ifdef CONFIG_BSD_PROCESS_ACCT
70 #include <linux/acct.h>
71 #endif
72 #ifdef CONFIG_RT_MUTEXES
73 #include <linux/rtmutex.h>
74 #endif
75 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_LOCK_STAT)
76 #include <linux/lockdep.h>
77 #endif
78 #ifdef CONFIG_CHR_DEV_SG
79 #include <scsi/sg.h>
80 #endif
81
82 #ifdef CONFIG_LOCKUP_DETECTOR
83 #include <linux/nmi.h>
84 #endif
85
86
87 #if defined(CONFIG_SYSCTL)
88
89 /* External variables not in a header file. */
90 extern int sysctl_overcommit_memory;
91 extern int sysctl_overcommit_ratio;
92 extern int max_threads;
93 extern int core_uses_pid;
94 extern int suid_dumpable;
95 extern char core_pattern[];
96 extern unsigned int core_pipe_limit;
97 extern int pid_max;
98 extern int min_free_kbytes;
99 extern int pid_max_min, pid_max_max;
100 extern int sysctl_drop_caches;
101 extern int percpu_pagelist_fraction;
102 extern int compat_log;
103 extern int latencytop_enabled;
104 extern int sysctl_nr_open_min, sysctl_nr_open_max;
105 #ifndef CONFIG_MMU
106 extern int sysctl_nr_trim_pages;
107 #endif
108 #ifdef CONFIG_BLOCK
109 extern int blk_iopoll_enabled;
110 #endif
111
112 /* Constants used for minimum and  maximum */
113 #ifdef CONFIG_LOCKUP_DETECTOR
114 static int sixty = 60;
115 static int neg_one = -1;
116 #endif
117
118 static int zero;
119 static int __maybe_unused one = 1;
120 static int __maybe_unused two = 2;
121 static int __maybe_unused three = 3;
122 static unsigned long one_ul = 1;
123 static int one_hundred = 100;
124 #ifdef CONFIG_PRINTK
125 static int ten_thousand = 10000;
126 #endif
127
128 /* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
129 static unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
130
131 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
132 static int maxolduid = 65535;
133 static int minolduid;
134 static int min_percpu_pagelist_fract = 8;
135
136 static int ngroups_max = NGROUPS_MAX;
137
138 #ifdef CONFIG_INOTIFY_USER
139 #include <linux/inotify.h>
140 #endif
141 #ifdef CONFIG_SPARC
142 #include <asm/system.h>
143 #endif
144
145 #ifdef CONFIG_SPARC64
146 extern int sysctl_tsb_ratio;
147 #endif
148
149 #ifdef __hppa__
150 extern int pwrsw_enabled;
151 extern int unaligned_enabled;
152 #endif
153
154 #ifdef CONFIG_IA64
155 extern int no_unaligned_warning;
156 extern int unaligned_dump_stack;
157 #endif
158
159 #ifdef CONFIG_PROC_SYSCTL
160 static int proc_do_cad_pid(struct ctl_table *table, int write,
161                   void __user *buffer, size_t *lenp, loff_t *ppos);
162 static int proc_taint(struct ctl_table *table, int write,
163                                void __user *buffer, size_t *lenp, loff_t *ppos);
164 #endif
165
166 #ifdef CONFIG_PRINTK
167 static int proc_dmesg_restrict(struct ctl_table *table, int write,
168                                 void __user *buffer, size_t *lenp, loff_t *ppos);
169 #endif
170
171 #ifdef CONFIG_MAGIC_SYSRQ
172 /* Note: sysrq code uses it's own private copy */
173 static int __sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
174
175 static int sysrq_sysctl_handler(ctl_table *table, int write,
176                                 void __user *buffer, size_t *lenp,
177                                 loff_t *ppos)
178 {
179         int error;
180
181         error = proc_dointvec(table, write, buffer, lenp, ppos);
182         if (error)
183                 return error;
184
185         if (write)
186                 sysrq_toggle_support(__sysrq_enabled);
187
188         return 0;
189 }
190
191 #endif
192
193 static struct ctl_table root_table[];
194 static struct ctl_table_root sysctl_table_root;
195 static struct ctl_table_header root_table_header = {
196         {{.count = 1,
197         .ctl_table = root_table,
198         .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
199         .root = &sysctl_table_root,
200         .set = &sysctl_table_root.default_set,
201 };
202 static struct ctl_table_root sysctl_table_root = {
203         .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
204         .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
205 };
206
207 static struct ctl_table kern_table[];
208 static struct ctl_table vm_table[];
209 static struct ctl_table fs_table[];
210 static struct ctl_table debug_table[];
211 static struct ctl_table dev_table[];
212 extern struct ctl_table random_table[];
213 #ifdef CONFIG_EPOLL
214 extern struct ctl_table epoll_table[];
215 #endif
216
217 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
218 int sysctl_legacy_va_layout;
219 #endif
220
221 /* The default sysctl tables: */
222
223 static struct ctl_table root_table[] = {
224         {
225                 .procname       = "kernel",
226                 .mode           = 0555,
227                 .child          = kern_table,
228         },
229         {
230                 .procname       = "vm",
231                 .mode           = 0555,
232                 .child          = vm_table,
233         },
234         {
235                 .procname       = "fs",
236                 .mode           = 0555,
237                 .child          = fs_table,
238         },
239         {
240                 .procname       = "debug",
241                 .mode           = 0555,
242                 .child          = debug_table,
243         },
244         {
245                 .procname       = "dev",
246                 .mode           = 0555,
247                 .child          = dev_table,
248         },
249         { }
250 };
251
252 #ifdef CONFIG_SCHED_DEBUG
253 static int min_sched_granularity_ns = 100000;           /* 100 usecs */
254 static int max_sched_granularity_ns = NSEC_PER_SEC;     /* 1 second */
255 static int min_wakeup_granularity_ns;                   /* 0 usecs */
256 static int max_wakeup_granularity_ns = NSEC_PER_SEC;    /* 1 second */
257 static int min_sched_tunable_scaling = SCHED_TUNABLESCALING_NONE;
258 static int max_sched_tunable_scaling = SCHED_TUNABLESCALING_END-1;
259 #endif
260
261 #ifdef CONFIG_COMPACTION
262 static int min_extfrag_threshold;
263 static int max_extfrag_threshold = 1000;
264 #endif
265
266 static struct ctl_table kern_table[] = {
267         {
268                 .procname       = "sched_child_runs_first",
269                 .data           = &sysctl_sched_child_runs_first,
270                 .maxlen         = sizeof(unsigned int),
271                 .mode           = 0644,
272                 .proc_handler   = proc_dointvec,
273         },
274 #ifdef CONFIG_SCHED_DEBUG
275         {
276                 .procname       = "sched_min_granularity_ns",
277                 .data           = &sysctl_sched_min_granularity,
278                 .maxlen         = sizeof(unsigned int),
279                 .mode           = 0644,
280                 .proc_handler   = sched_proc_update_handler,
281                 .extra1         = &min_sched_granularity_ns,
282                 .extra2         = &max_sched_granularity_ns,
283         },
284         {
285                 .procname       = "sched_latency_ns",
286                 .data           = &sysctl_sched_latency,
287                 .maxlen         = sizeof(unsigned int),
288                 .mode           = 0644,
289                 .proc_handler   = sched_proc_update_handler,
290                 .extra1         = &min_sched_granularity_ns,
291                 .extra2         = &max_sched_granularity_ns,
292         },
293         {
294                 .procname       = "sched_wakeup_granularity_ns",
295                 .data           = &sysctl_sched_wakeup_granularity,
296                 .maxlen         = sizeof(unsigned int),
297                 .mode           = 0644,
298                 .proc_handler   = sched_proc_update_handler,
299                 .extra1         = &min_wakeup_granularity_ns,
300                 .extra2         = &max_wakeup_granularity_ns,
301         },
302         {
303                 .procname       = "sched_tunable_scaling",
304                 .data           = &sysctl_sched_tunable_scaling,
305                 .maxlen         = sizeof(enum sched_tunable_scaling),
306                 .mode           = 0644,
307                 .proc_handler   = sched_proc_update_handler,
308                 .extra1         = &min_sched_tunable_scaling,
309                 .extra2         = &max_sched_tunable_scaling,
310         },
311         {
312                 .procname       = "sched_migration_cost",
313                 .data           = &sysctl_sched_migration_cost,
314                 .maxlen         = sizeof(unsigned int),
315                 .mode           = 0644,
316                 .proc_handler   = proc_dointvec,
317         },
318         {
319                 .procname       = "sched_nr_migrate",
320                 .data           = &sysctl_sched_nr_migrate,
321                 .maxlen         = sizeof(unsigned int),
322                 .mode           = 0644,
323                 .proc_handler   = proc_dointvec,
324         },
325         {
326                 .procname       = "sched_time_avg",
327                 .data           = &sysctl_sched_time_avg,
328                 .maxlen         = sizeof(unsigned int),
329                 .mode           = 0644,
330                 .proc_handler   = proc_dointvec,
331         },
332         {
333                 .procname       = "sched_shares_window",
334                 .data           = &sysctl_sched_shares_window,
335                 .maxlen         = sizeof(unsigned int),
336                 .mode           = 0644,
337                 .proc_handler   = proc_dointvec,
338         },
339         {
340                 .procname       = "timer_migration",
341                 .data           = &sysctl_timer_migration,
342                 .maxlen         = sizeof(unsigned int),
343                 .mode           = 0644,
344                 .proc_handler   = proc_dointvec_minmax,
345                 .extra1         = &zero,
346                 .extra2         = &one,
347         },
348 #endif
349         {
350                 .procname       = "sched_rt_period_us",
351                 .data           = &sysctl_sched_rt_period,
352                 .maxlen         = sizeof(unsigned int),
353                 .mode           = 0644,
354                 .proc_handler   = sched_rt_handler,
355         },
356         {
357                 .procname       = "sched_rt_runtime_us",
358                 .data           = &sysctl_sched_rt_runtime,
359                 .maxlen         = sizeof(int),
360                 .mode           = 0644,
361                 .proc_handler   = sched_rt_handler,
362         },
363 #ifdef CONFIG_SCHED_AUTOGROUP
364         {
365                 .procname       = "sched_autogroup_enabled",
366                 .data           = &sysctl_sched_autogroup_enabled,
367                 .maxlen         = sizeof(unsigned int),
368                 .mode           = 0644,
369                 .proc_handler   = proc_dointvec_minmax,
370                 .extra1         = &zero,
371                 .extra2         = &one,
372         },
373 #endif
374 #ifdef CONFIG_CFS_BANDWIDTH
375         {
376                 .procname       = "sched_cfs_bandwidth_slice_us",
377                 .data           = &sysctl_sched_cfs_bandwidth_slice,
378                 .maxlen         = sizeof(unsigned int),
379                 .mode           = 0644,
380                 .proc_handler   = proc_dointvec_minmax,
381                 .extra1         = &one,
382         },
383 #endif
384 #ifdef CONFIG_PROVE_LOCKING
385         {
386                 .procname       = "prove_locking",
387                 .data           = &prove_locking,
388                 .maxlen         = sizeof(int),
389                 .mode           = 0644,
390                 .proc_handler   = proc_dointvec,
391         },
392 #endif
393 #ifdef CONFIG_LOCK_STAT
394         {
395                 .procname       = "lock_stat",
396                 .data           = &lock_stat,
397                 .maxlen         = sizeof(int),
398                 .mode           = 0644,
399                 .proc_handler   = proc_dointvec,
400         },
401 #endif
402         {
403                 .procname       = "panic",
404                 .data           = &panic_timeout,
405                 .maxlen         = sizeof(int),
406                 .mode           = 0644,
407                 .proc_handler   = proc_dointvec,
408         },
409         {
410                 .procname       = "core_uses_pid",
411                 .data           = &core_uses_pid,
412                 .maxlen         = sizeof(int),
413                 .mode           = 0644,
414                 .proc_handler   = proc_dointvec,
415         },
416         {
417                 .procname       = "core_pattern",
418                 .data           = core_pattern,
419                 .maxlen         = CORENAME_MAX_SIZE,
420                 .mode           = 0644,
421                 .proc_handler   = proc_dostring,
422         },
423         {
424                 .procname       = "core_pipe_limit",
425                 .data           = &core_pipe_limit,
426                 .maxlen         = sizeof(unsigned int),
427                 .mode           = 0644,
428                 .proc_handler   = proc_dointvec,
429         },
430 #ifdef CONFIG_PROC_SYSCTL
431         {
432                 .procname       = "tainted",
433                 .maxlen         = sizeof(long),
434                 .mode           = 0644,
435                 .proc_handler   = proc_taint,
436         },
437 #endif
438 #ifdef CONFIG_LATENCYTOP
439         {
440                 .procname       = "latencytop",
441                 .data           = &latencytop_enabled,
442                 .maxlen         = sizeof(int),
443                 .mode           = 0644,
444                 .proc_handler   = proc_dointvec,
445         },
446 #endif
447 #ifdef CONFIG_BLK_DEV_INITRD
448         {
449                 .procname       = "real-root-dev",
450                 .data           = &real_root_dev,
451                 .maxlen         = sizeof(int),
452                 .mode           = 0644,
453                 .proc_handler   = proc_dointvec,
454         },
455 #endif
456         {
457                 .procname       = "print-fatal-signals",
458                 .data           = &print_fatal_signals,
459                 .maxlen         = sizeof(int),
460                 .mode           = 0644,
461                 .proc_handler   = proc_dointvec,
462         },
463 #ifdef CONFIG_SPARC
464         {
465                 .procname       = "reboot-cmd",
466                 .data           = reboot_command,
467                 .maxlen         = 256,
468                 .mode           = 0644,
469                 .proc_handler   = proc_dostring,
470         },
471         {
472                 .procname       = "stop-a",
473                 .data           = &stop_a_enabled,
474                 .maxlen         = sizeof (int),
475                 .mode           = 0644,
476                 .proc_handler   = proc_dointvec,
477         },
478         {
479                 .procname       = "scons-poweroff",
480                 .data           = &scons_pwroff,
481                 .maxlen         = sizeof (int),
482                 .mode           = 0644,
483                 .proc_handler   = proc_dointvec,
484         },
485 #endif
486 #ifdef CONFIG_SPARC64
487         {
488                 .procname       = "tsb-ratio",
489                 .data           = &sysctl_tsb_ratio,
490                 .maxlen         = sizeof (int),
491                 .mode           = 0644,
492                 .proc_handler   = proc_dointvec,
493         },
494 #endif
495 #ifdef __hppa__
496         {
497                 .procname       = "soft-power",
498                 .data           = &pwrsw_enabled,
499                 .maxlen         = sizeof (int),
500                 .mode           = 0644,
501                 .proc_handler   = proc_dointvec,
502         },
503         {
504                 .procname       = "unaligned-trap",
505                 .data           = &unaligned_enabled,
506                 .maxlen         = sizeof (int),
507                 .mode           = 0644,
508                 .proc_handler   = proc_dointvec,
509         },
510 #endif
511         {
512                 .procname       = "ctrl-alt-del",
513                 .data           = &C_A_D,
514                 .maxlen         = sizeof(int),
515                 .mode           = 0644,
516                 .proc_handler   = proc_dointvec,
517         },
518 #ifdef CONFIG_FUNCTION_TRACER
519         {
520                 .procname       = "ftrace_enabled",
521                 .data           = &ftrace_enabled,
522                 .maxlen         = sizeof(int),
523                 .mode           = 0644,
524                 .proc_handler   = ftrace_enable_sysctl,
525         },
526 #endif
527 #ifdef CONFIG_STACK_TRACER
528         {
529                 .procname       = "stack_tracer_enabled",
530                 .data           = &stack_tracer_enabled,
531                 .maxlen         = sizeof(int),
532                 .mode           = 0644,
533                 .proc_handler   = stack_trace_sysctl,
534         },
535 #endif
536 #ifdef CONFIG_TRACING
537         {
538                 .procname       = "ftrace_dump_on_oops",
539                 .data           = &ftrace_dump_on_oops,
540                 .maxlen         = sizeof(int),
541                 .mode           = 0644,
542                 .proc_handler   = proc_dointvec,
543         },
544 #endif
545 #ifdef CONFIG_MODULES
546         {
547                 .procname       = "modprobe",
548                 .data           = &modprobe_path,
549                 .maxlen         = KMOD_PATH_LEN,
550                 .mode           = 0644,
551                 .proc_handler   = proc_dostring,
552         },
553         {
554                 .procname       = "modules_disabled",
555                 .data           = &modules_disabled,
556                 .maxlen         = sizeof(int),
557                 .mode           = 0644,
558                 /* only handle a transition from default "0" to "1" */
559                 .proc_handler   = proc_dointvec_minmax,
560                 .extra1         = &one,
561                 .extra2         = &one,
562         },
563 #endif
564 #ifdef CONFIG_HOTPLUG
565         {
566                 .procname       = "hotplug",
567                 .data           = &uevent_helper,
568                 .maxlen         = UEVENT_HELPER_PATH_LEN,
569                 .mode           = 0644,
570                 .proc_handler   = proc_dostring,
571         },
572 #endif
573 #ifdef CONFIG_CHR_DEV_SG
574         {
575                 .procname       = "sg-big-buff",
576                 .data           = &sg_big_buff,
577                 .maxlen         = sizeof (int),
578                 .mode           = 0444,
579                 .proc_handler   = proc_dointvec,
580         },
581 #endif
582 #ifdef CONFIG_BSD_PROCESS_ACCT
583         {
584                 .procname       = "acct",
585                 .data           = &acct_parm,
586                 .maxlen         = 3*sizeof(int),
587                 .mode           = 0644,
588                 .proc_handler   = proc_dointvec,
589         },
590 #endif
591 #ifdef CONFIG_MAGIC_SYSRQ
592         {
593                 .procname       = "sysrq",
594                 .data           = &__sysrq_enabled,
595                 .maxlen         = sizeof (int),
596                 .mode           = 0644,
597                 .proc_handler   = sysrq_sysctl_handler,
598         },
599 #endif
600 #ifdef CONFIG_PROC_SYSCTL
601         {
602                 .procname       = "cad_pid",
603                 .data           = NULL,
604                 .maxlen         = sizeof (int),
605                 .mode           = 0600,
606                 .proc_handler   = proc_do_cad_pid,
607         },
608 #endif
609         {
610                 .procname       = "threads-max",
611                 .data           = &max_threads,
612                 .maxlen         = sizeof(int),
613                 .mode           = 0644,
614                 .proc_handler   = proc_dointvec,
615         },
616         {
617                 .procname       = "random",
618                 .mode           = 0555,
619                 .child          = random_table,
620         },
621         {
622                 .procname       = "usermodehelper",
623                 .mode           = 0555,
624                 .child          = usermodehelper_table,
625         },
626         {
627                 .procname       = "overflowuid",
628                 .data           = &overflowuid,
629                 .maxlen         = sizeof(int),
630                 .mode           = 0644,
631                 .proc_handler   = proc_dointvec_minmax,
632                 .extra1         = &minolduid,
633                 .extra2         = &maxolduid,
634         },
635         {
636                 .procname       = "overflowgid",
637                 .data           = &overflowgid,
638                 .maxlen         = sizeof(int),
639                 .mode           = 0644,
640                 .proc_handler   = proc_dointvec_minmax,
641                 .extra1         = &minolduid,
642                 .extra2         = &maxolduid,
643         },
644 #ifdef CONFIG_S390
645 #ifdef CONFIG_MATHEMU
646         {
647                 .procname       = "ieee_emulation_warnings",
648                 .data           = &sysctl_ieee_emulation_warnings,
649                 .maxlen         = sizeof(int),
650                 .mode           = 0644,
651                 .proc_handler   = proc_dointvec,
652         },
653 #endif
654         {
655                 .procname       = "userprocess_debug",
656                 .data           = &show_unhandled_signals,
657                 .maxlen         = sizeof(int),
658                 .mode           = 0644,
659                 .proc_handler   = proc_dointvec,
660         },
661 #endif
662         {
663                 .procname       = "pid_max",
664                 .data           = &pid_max,
665                 .maxlen         = sizeof (int),
666                 .mode           = 0644,
667                 .proc_handler   = proc_dointvec_minmax,
668                 .extra1         = &pid_max_min,
669                 .extra2         = &pid_max_max,
670         },
671         {
672                 .procname       = "panic_on_oops",
673                 .data           = &panic_on_oops,
674                 .maxlen         = sizeof(int),
675                 .mode           = 0644,
676                 .proc_handler   = proc_dointvec,
677         },
678 #if defined CONFIG_PRINTK
679         {
680                 .procname       = "printk",
681                 .data           = &console_loglevel,
682                 .maxlen         = 4*sizeof(int),
683                 .mode           = 0644,
684                 .proc_handler   = proc_dointvec,
685         },
686         {
687                 .procname       = "printk_ratelimit",
688                 .data           = &printk_ratelimit_state.interval,
689                 .maxlen         = sizeof(int),
690                 .mode           = 0644,
691                 .proc_handler   = proc_dointvec_jiffies,
692         },
693         {
694                 .procname       = "printk_ratelimit_burst",
695                 .data           = &printk_ratelimit_state.burst,
696                 .maxlen         = sizeof(int),
697                 .mode           = 0644,
698                 .proc_handler   = proc_dointvec,
699         },
700         {
701                 .procname       = "printk_delay",
702                 .data           = &printk_delay_msec,
703                 .maxlen         = sizeof(int),
704                 .mode           = 0644,
705                 .proc_handler   = proc_dointvec_minmax,
706                 .extra1         = &zero,
707                 .extra2         = &ten_thousand,
708         },
709         {
710                 .procname       = "dmesg_restrict",
711                 .data           = &dmesg_restrict,
712                 .maxlen         = sizeof(int),
713                 .mode           = 0644,
714                 .proc_handler   = proc_dointvec_minmax,
715                 .extra1         = &zero,
716                 .extra2         = &one,
717         },
718         {
719                 .procname       = "kptr_restrict",
720                 .data           = &kptr_restrict,
721                 .maxlen         = sizeof(int),
722                 .mode           = 0644,
723                 .proc_handler   = proc_dmesg_restrict,
724                 .extra1         = &zero,
725                 .extra2         = &two,
726         },
727 #endif
728         {
729                 .procname       = "ngroups_max",
730                 .data           = &ngroups_max,
731                 .maxlen         = sizeof (int),
732                 .mode           = 0444,
733                 .proc_handler   = proc_dointvec,
734         },
735 #if defined(CONFIG_LOCKUP_DETECTOR)
736         {
737                 .procname       = "watchdog",
738                 .data           = &watchdog_enabled,
739                 .maxlen         = sizeof (int),
740                 .mode           = 0644,
741                 .proc_handler   = proc_dowatchdog,
742                 .extra1         = &zero,
743                 .extra2         = &one,
744         },
745         {
746                 .procname       = "watchdog_thresh",
747                 .data           = &watchdog_thresh,
748                 .maxlen         = sizeof(int),
749                 .mode           = 0644,
750                 .proc_handler   = proc_dowatchdog,
751                 .extra1         = &neg_one,
752                 .extra2         = &sixty,
753         },
754         {
755                 .procname       = "softlockup_panic",
756                 .data           = &softlockup_panic,
757                 .maxlen         = sizeof(int),
758                 .mode           = 0644,
759                 .proc_handler   = proc_dointvec_minmax,
760                 .extra1         = &zero,
761                 .extra2         = &one,
762         },
763         {
764                 .procname       = "nmi_watchdog",
765                 .data           = &watchdog_enabled,
766                 .maxlen         = sizeof (int),
767                 .mode           = 0644,
768                 .proc_handler   = proc_dowatchdog,
769                 .extra1         = &zero,
770                 .extra2         = &one,
771         },
772 #endif
773 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
774         {
775                 .procname       = "unknown_nmi_panic",
776                 .data           = &unknown_nmi_panic,
777                 .maxlen         = sizeof (int),
778                 .mode           = 0644,
779                 .proc_handler   = proc_dointvec,
780         },
781 #endif
782 #if defined(CONFIG_X86)
783         {
784                 .procname       = "panic_on_unrecovered_nmi",
785                 .data           = &panic_on_unrecovered_nmi,
786                 .maxlen         = sizeof(int),
787                 .mode           = 0644,
788                 .proc_handler   = proc_dointvec,
789         },
790         {
791                 .procname       = "panic_on_io_nmi",
792                 .data           = &panic_on_io_nmi,
793                 .maxlen         = sizeof(int),
794                 .mode           = 0644,
795                 .proc_handler   = proc_dointvec,
796         },
797         {
798                 .procname       = "bootloader_type",
799                 .data           = &bootloader_type,
800                 .maxlen         = sizeof (int),
801                 .mode           = 0444,
802                 .proc_handler   = proc_dointvec,
803         },
804         {
805                 .procname       = "bootloader_version",
806                 .data           = &bootloader_version,
807                 .maxlen         = sizeof (int),
808                 .mode           = 0444,
809                 .proc_handler   = proc_dointvec,
810         },
811         {
812                 .procname       = "kstack_depth_to_print",
813                 .data           = &kstack_depth_to_print,
814                 .maxlen         = sizeof(int),
815                 .mode           = 0644,
816                 .proc_handler   = proc_dointvec,
817         },
818         {
819                 .procname       = "io_delay_type",
820                 .data           = &io_delay_type,
821                 .maxlen         = sizeof(int),
822                 .mode           = 0644,
823                 .proc_handler   = proc_dointvec,
824         },
825 #endif
826 #if defined(CONFIG_MMU)
827         {
828                 .procname       = "randomize_va_space",
829                 .data           = &randomize_va_space,
830                 .maxlen         = sizeof(int),
831                 .mode           = 0644,
832                 .proc_handler   = proc_dointvec,
833         },
834 #endif
835 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
836         {
837                 .procname       = "spin_retry",
838                 .data           = &spin_retry,
839                 .maxlen         = sizeof (int),
840                 .mode           = 0644,
841                 .proc_handler   = proc_dointvec,
842         },
843 #endif
844 #if     defined(CONFIG_ACPI_SLEEP) && defined(CONFIG_X86)
845         {
846                 .procname       = "acpi_video_flags",
847                 .data           = &acpi_realmode_flags,
848                 .maxlen         = sizeof (unsigned long),
849                 .mode           = 0644,
850                 .proc_handler   = proc_doulongvec_minmax,
851         },
852 #endif
853 #ifdef CONFIG_IA64
854         {
855                 .procname       = "ignore-unaligned-usertrap",
856                 .data           = &no_unaligned_warning,
857                 .maxlen         = sizeof (int),
858                 .mode           = 0644,
859                 .proc_handler   = proc_dointvec,
860         },
861         {
862                 .procname       = "unaligned-dump-stack",
863                 .data           = &unaligned_dump_stack,
864                 .maxlen         = sizeof (int),
865                 .mode           = 0644,
866                 .proc_handler   = proc_dointvec,
867         },
868 #endif
869 #ifdef CONFIG_DETECT_HUNG_TASK
870         {
871                 .procname       = "hung_task_panic",
872                 .data           = &sysctl_hung_task_panic,
873                 .maxlen         = sizeof(int),
874                 .mode           = 0644,
875                 .proc_handler   = proc_dointvec_minmax,
876                 .extra1         = &zero,
877                 .extra2         = &one,
878         },
879         {
880                 .procname       = "hung_task_check_count",
881                 .data           = &sysctl_hung_task_check_count,
882                 .maxlen         = sizeof(unsigned long),
883                 .mode           = 0644,
884                 .proc_handler   = proc_doulongvec_minmax,
885         },
886         {
887                 .procname       = "hung_task_timeout_secs",
888                 .data           = &sysctl_hung_task_timeout_secs,
889                 .maxlen         = sizeof(unsigned long),
890                 .mode           = 0644,
891                 .proc_handler   = proc_dohung_task_timeout_secs,
892         },
893         {
894                 .procname       = "hung_task_warnings",
895                 .data           = &sysctl_hung_task_warnings,
896                 .maxlen         = sizeof(unsigned long),
897                 .mode           = 0644,
898                 .proc_handler   = proc_doulongvec_minmax,
899         },
900 #endif
901 #ifdef CONFIG_COMPAT
902         {
903                 .procname       = "compat-log",
904                 .data           = &compat_log,
905                 .maxlen         = sizeof (int),
906                 .mode           = 0644,
907                 .proc_handler   = proc_dointvec,
908         },
909 #endif
910 #ifdef CONFIG_RT_MUTEXES
911         {
912                 .procname       = "max_lock_depth",
913                 .data           = &max_lock_depth,
914                 .maxlen         = sizeof(int),
915                 .mode           = 0644,
916                 .proc_handler   = proc_dointvec,
917         },
918 #endif
919         {
920                 .procname       = "poweroff_cmd",
921                 .data           = &poweroff_cmd,
922                 .maxlen         = POWEROFF_CMD_PATH_LEN,
923                 .mode           = 0644,
924                 .proc_handler   = proc_dostring,
925         },
926 #ifdef CONFIG_KEYS
927         {
928                 .procname       = "keys",
929                 .mode           = 0555,
930                 .child          = key_sysctls,
931         },
932 #endif
933 #ifdef CONFIG_RCU_TORTURE_TEST
934         {
935                 .procname       = "rcutorture_runnable",
936                 .data           = &rcutorture_runnable,
937                 .maxlen         = sizeof(int),
938                 .mode           = 0644,
939                 .proc_handler   = proc_dointvec,
940         },
941 #endif
942 #ifdef CONFIG_PERF_EVENTS
943         /*
944          * User-space scripts rely on the existence of this file
945          * as a feature check for perf_events being enabled.
946          *
947          * So it's an ABI, do not remove!
948          */
949         {
950                 .procname       = "perf_event_paranoid",
951                 .data           = &sysctl_perf_event_paranoid,
952                 .maxlen         = sizeof(sysctl_perf_event_paranoid),
953                 .mode           = 0644,
954                 .proc_handler   = proc_dointvec,
955         },
956         {
957                 .procname       = "perf_event_mlock_kb",
958                 .data           = &sysctl_perf_event_mlock,
959                 .maxlen         = sizeof(sysctl_perf_event_mlock),
960                 .mode           = 0644,
961                 .proc_handler   = proc_dointvec,
962         },
963         {
964                 .procname       = "perf_event_max_sample_rate",
965                 .data           = &sysctl_perf_event_sample_rate,
966                 .maxlen         = sizeof(sysctl_perf_event_sample_rate),
967                 .mode           = 0644,
968                 .proc_handler   = perf_proc_update_handler,
969         },
970 #endif
971 #ifdef CONFIG_KMEMCHECK
972         {
973                 .procname       = "kmemcheck",
974                 .data           = &kmemcheck_enabled,
975                 .maxlen         = sizeof(int),
976                 .mode           = 0644,
977                 .proc_handler   = proc_dointvec,
978         },
979 #endif
980 #ifdef CONFIG_BLOCK
981         {
982                 .procname       = "blk_iopoll",
983                 .data           = &blk_iopoll_enabled,
984                 .maxlen         = sizeof(int),
985                 .mode           = 0644,
986                 .proc_handler   = proc_dointvec,
987         },
988 #endif
989         { }
990 };
991
992 static struct ctl_table vm_table[] = {
993         {
994                 .procname       = "overcommit_memory",
995                 .data           = &sysctl_overcommit_memory,
996                 .maxlen         = sizeof(sysctl_overcommit_memory),
997                 .mode           = 0644,
998                 .proc_handler   = proc_dointvec_minmax,
999                 .extra1         = &zero,
1000                 .extra2         = &two,
1001         },
1002         {
1003                 .procname       = "panic_on_oom",
1004                 .data           = &sysctl_panic_on_oom,
1005                 .maxlen         = sizeof(sysctl_panic_on_oom),
1006                 .mode           = 0644,
1007                 .proc_handler   = proc_dointvec_minmax,
1008                 .extra1         = &zero,
1009                 .extra2         = &two,
1010         },
1011         {
1012                 .procname       = "oom_kill_allocating_task",
1013                 .data           = &sysctl_oom_kill_allocating_task,
1014                 .maxlen         = sizeof(sysctl_oom_kill_allocating_task),
1015                 .mode           = 0644,
1016                 .proc_handler   = proc_dointvec,
1017         },
1018         {
1019                 .procname       = "oom_dump_tasks",
1020                 .data           = &sysctl_oom_dump_tasks,
1021                 .maxlen         = sizeof(sysctl_oom_dump_tasks),
1022                 .mode           = 0644,
1023                 .proc_handler   = proc_dointvec,
1024         },
1025         {
1026                 .procname       = "overcommit_ratio",
1027                 .data           = &sysctl_overcommit_ratio,
1028                 .maxlen         = sizeof(sysctl_overcommit_ratio),
1029                 .mode           = 0644,
1030                 .proc_handler   = proc_dointvec,
1031         },
1032         {
1033                 .procname       = "page-cluster", 
1034                 .data           = &page_cluster,
1035                 .maxlen         = sizeof(int),
1036                 .mode           = 0644,
1037                 .proc_handler   = proc_dointvec_minmax,
1038                 .extra1         = &zero,
1039         },
1040         {
1041                 .procname       = "dirty_background_ratio",
1042                 .data           = &dirty_background_ratio,
1043                 .maxlen         = sizeof(dirty_background_ratio),
1044                 .mode           = 0644,
1045                 .proc_handler   = dirty_background_ratio_handler,
1046                 .extra1         = &zero,
1047                 .extra2         = &one_hundred,
1048         },
1049         {
1050                 .procname       = "dirty_background_bytes",
1051                 .data           = &dirty_background_bytes,
1052                 .maxlen         = sizeof(dirty_background_bytes),
1053                 .mode           = 0644,
1054                 .proc_handler   = dirty_background_bytes_handler,
1055                 .extra1         = &one_ul,
1056         },
1057         {
1058                 .procname       = "dirty_ratio",
1059                 .data           = &vm_dirty_ratio,
1060                 .maxlen         = sizeof(vm_dirty_ratio),
1061                 .mode           = 0644,
1062                 .proc_handler   = dirty_ratio_handler,
1063                 .extra1         = &zero,
1064                 .extra2         = &one_hundred,
1065         },
1066         {
1067                 .procname       = "dirty_bytes",
1068                 .data           = &vm_dirty_bytes,
1069                 .maxlen         = sizeof(vm_dirty_bytes),
1070                 .mode           = 0644,
1071                 .proc_handler   = dirty_bytes_handler,
1072                 .extra1         = &dirty_bytes_min,
1073         },
1074         {
1075                 .procname       = "dirty_writeback_centisecs",
1076                 .data           = &dirty_writeback_interval,
1077                 .maxlen         = sizeof(dirty_writeback_interval),
1078                 .mode           = 0644,
1079                 .proc_handler   = dirty_writeback_centisecs_handler,
1080         },
1081         {
1082                 .procname       = "dirty_expire_centisecs",
1083                 .data           = &dirty_expire_interval,
1084                 .maxlen         = sizeof(dirty_expire_interval),
1085                 .mode           = 0644,
1086                 .proc_handler   = proc_dointvec_minmax,
1087                 .extra1         = &zero,
1088         },
1089         {
1090                 .procname       = "nr_pdflush_threads",
1091                 .data           = &nr_pdflush_threads,
1092                 .maxlen         = sizeof nr_pdflush_threads,
1093                 .mode           = 0444 /* read-only*/,
1094                 .proc_handler   = proc_dointvec,
1095         },
1096         {
1097                 .procname       = "swappiness",
1098                 .data           = &vm_swappiness,
1099                 .maxlen         = sizeof(vm_swappiness),
1100                 .mode           = 0644,
1101                 .proc_handler   = proc_dointvec_minmax,
1102                 .extra1         = &zero,
1103                 .extra2         = &one_hundred,
1104         },
1105 #ifdef CONFIG_HUGETLB_PAGE
1106         {
1107                 .procname       = "nr_hugepages",
1108                 .data           = NULL,
1109                 .maxlen         = sizeof(unsigned long),
1110                 .mode           = 0644,
1111                 .proc_handler   = hugetlb_sysctl_handler,
1112                 .extra1         = (void *)&hugetlb_zero,
1113                 .extra2         = (void *)&hugetlb_infinity,
1114         },
1115 #ifdef CONFIG_NUMA
1116         {
1117                 .procname       = "nr_hugepages_mempolicy",
1118                 .data           = NULL,
1119                 .maxlen         = sizeof(unsigned long),
1120                 .mode           = 0644,
1121                 .proc_handler   = &hugetlb_mempolicy_sysctl_handler,
1122                 .extra1         = (void *)&hugetlb_zero,
1123                 .extra2         = (void *)&hugetlb_infinity,
1124         },
1125 #endif
1126          {
1127                 .procname       = "hugetlb_shm_group",
1128                 .data           = &sysctl_hugetlb_shm_group,
1129                 .maxlen         = sizeof(gid_t),
1130                 .mode           = 0644,
1131                 .proc_handler   = proc_dointvec,
1132          },
1133          {
1134                 .procname       = "hugepages_treat_as_movable",
1135                 .data           = &hugepages_treat_as_movable,
1136                 .maxlen         = sizeof(int),
1137                 .mode           = 0644,
1138                 .proc_handler   = hugetlb_treat_movable_handler,
1139         },
1140         {
1141                 .procname       = "nr_overcommit_hugepages",
1142                 .data           = NULL,
1143                 .maxlen         = sizeof(unsigned long),
1144                 .mode           = 0644,
1145                 .proc_handler   = hugetlb_overcommit_handler,
1146                 .extra1         = (void *)&hugetlb_zero,
1147                 .extra2         = (void *)&hugetlb_infinity,
1148         },
1149 #endif
1150         {
1151                 .procname       = "lowmem_reserve_ratio",
1152                 .data           = &sysctl_lowmem_reserve_ratio,
1153                 .maxlen         = sizeof(sysctl_lowmem_reserve_ratio),
1154                 .mode           = 0644,
1155                 .proc_handler   = lowmem_reserve_ratio_sysctl_handler,
1156         },
1157         {
1158                 .procname       = "drop_caches",
1159                 .data           = &sysctl_drop_caches,
1160                 .maxlen         = sizeof(int),
1161                 .mode           = 0644,
1162                 .proc_handler   = drop_caches_sysctl_handler,
1163                 .extra1         = &one,
1164                 .extra2         = &three,
1165         },
1166 #ifdef CONFIG_COMPACTION
1167         {
1168                 .procname       = "compact_memory",
1169                 .data           = &sysctl_compact_memory,
1170                 .maxlen         = sizeof(int),
1171                 .mode           = 0200,
1172                 .proc_handler   = sysctl_compaction_handler,
1173         },
1174         {
1175                 .procname       = "extfrag_threshold",
1176                 .data           = &sysctl_extfrag_threshold,
1177                 .maxlen         = sizeof(int),
1178                 .mode           = 0644,
1179                 .proc_handler   = sysctl_extfrag_handler,
1180                 .extra1         = &min_extfrag_threshold,
1181                 .extra2         = &max_extfrag_threshold,
1182         },
1183
1184 #endif /* CONFIG_COMPACTION */
1185         {
1186                 .procname       = "min_free_kbytes",
1187                 .data           = &min_free_kbytes,
1188                 .maxlen         = sizeof(min_free_kbytes),
1189                 .mode           = 0644,
1190                 .proc_handler   = min_free_kbytes_sysctl_handler,
1191                 .extra1         = &zero,
1192         },
1193         {
1194                 .procname       = "percpu_pagelist_fraction",
1195                 .data           = &percpu_pagelist_fraction,
1196                 .maxlen         = sizeof(percpu_pagelist_fraction),
1197                 .mode           = 0644,
1198                 .proc_handler   = percpu_pagelist_fraction_sysctl_handler,
1199                 .extra1         = &min_percpu_pagelist_fract,
1200         },
1201 #ifdef CONFIG_MMU
1202         {
1203                 .procname       = "max_map_count",
1204                 .data           = &sysctl_max_map_count,
1205                 .maxlen         = sizeof(sysctl_max_map_count),
1206                 .mode           = 0644,
1207                 .proc_handler   = proc_dointvec_minmax,
1208                 .extra1         = &zero,
1209         },
1210 #else
1211         {
1212                 .procname       = "nr_trim_pages",
1213                 .data           = &sysctl_nr_trim_pages,
1214                 .maxlen         = sizeof(sysctl_nr_trim_pages),
1215                 .mode           = 0644,
1216                 .proc_handler   = proc_dointvec_minmax,
1217                 .extra1         = &zero,
1218         },
1219 #endif
1220         {
1221                 .procname       = "laptop_mode",
1222                 .data           = &laptop_mode,
1223                 .maxlen         = sizeof(laptop_mode),
1224                 .mode           = 0644,
1225                 .proc_handler   = proc_dointvec_jiffies,
1226         },
1227         {
1228                 .procname       = "block_dump",
1229                 .data           = &block_dump,
1230                 .maxlen         = sizeof(block_dump),
1231                 .mode           = 0644,
1232                 .proc_handler   = proc_dointvec,
1233                 .extra1         = &zero,
1234         },
1235         {
1236                 .procname       = "vfs_cache_pressure",
1237                 .data           = &sysctl_vfs_cache_pressure,
1238                 .maxlen         = sizeof(sysctl_vfs_cache_pressure),
1239                 .mode           = 0644,
1240                 .proc_handler   = proc_dointvec,
1241                 .extra1         = &zero,
1242         },
1243 #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
1244         {
1245                 .procname       = "legacy_va_layout",
1246                 .data           = &sysctl_legacy_va_layout,
1247                 .maxlen         = sizeof(sysctl_legacy_va_layout),
1248                 .mode           = 0644,
1249                 .proc_handler   = proc_dointvec,
1250                 .extra1         = &zero,
1251         },
1252 #endif
1253 #ifdef CONFIG_NUMA
1254         {
1255                 .procname       = "zone_reclaim_mode",
1256                 .data           = &zone_reclaim_mode,
1257                 .maxlen         = sizeof(zone_reclaim_mode),
1258                 .mode           = 0644,
1259                 .proc_handler   = proc_dointvec,
1260                 .extra1         = &zero,
1261         },
1262         {
1263                 .procname       = "min_unmapped_ratio",
1264                 .data           = &sysctl_min_unmapped_ratio,
1265                 .maxlen         = sizeof(sysctl_min_unmapped_ratio),
1266                 .mode           = 0644,
1267                 .proc_handler   = sysctl_min_unmapped_ratio_sysctl_handler,
1268                 .extra1         = &zero,
1269                 .extra2         = &one_hundred,
1270         },
1271         {
1272                 .procname       = "min_slab_ratio",
1273                 .data           = &sysctl_min_slab_ratio,
1274                 .maxlen         = sizeof(sysctl_min_slab_ratio),
1275                 .mode           = 0644,
1276                 .proc_handler   = sysctl_min_slab_ratio_sysctl_handler,
1277                 .extra1         = &zero,
1278                 .extra2         = &one_hundred,
1279         },
1280 #endif
1281 #ifdef CONFIG_SMP
1282         {
1283                 .procname       = "stat_interval",
1284                 .data           = &sysctl_stat_interval,
1285                 .maxlen         = sizeof(sysctl_stat_interval),
1286                 .mode           = 0644,
1287                 .proc_handler   = proc_dointvec_jiffies,
1288         },
1289 #endif
1290 #ifdef CONFIG_MMU
1291         {
1292                 .procname       = "mmap_min_addr",
1293                 .data           = &dac_mmap_min_addr,
1294                 .maxlen         = sizeof(unsigned long),
1295                 .mode           = 0644,
1296                 .proc_handler   = mmap_min_addr_handler,
1297         },
1298 #endif
1299 #ifdef CONFIG_NUMA
1300         {
1301                 .procname       = "numa_zonelist_order",
1302                 .data           = &numa_zonelist_order,
1303                 .maxlen         = NUMA_ZONELIST_ORDER_LEN,
1304                 .mode           = 0644,
1305                 .proc_handler   = numa_zonelist_order_handler,
1306         },
1307 #endif
1308 #if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
1309    (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
1310         {
1311                 .procname       = "vdso_enabled",
1312                 .data           = &vdso_enabled,
1313                 .maxlen         = sizeof(vdso_enabled),
1314                 .mode           = 0644,
1315                 .proc_handler   = proc_dointvec,
1316                 .extra1         = &zero,
1317         },
1318 #endif
1319 #ifdef CONFIG_HIGHMEM
1320         {
1321                 .procname       = "highmem_is_dirtyable",
1322                 .data           = &vm_highmem_is_dirtyable,
1323                 .maxlen         = sizeof(vm_highmem_is_dirtyable),
1324                 .mode           = 0644,
1325                 .proc_handler   = proc_dointvec_minmax,
1326                 .extra1         = &zero,
1327                 .extra2         = &one,
1328         },
1329 #endif
1330         {
1331                 .procname       = "scan_unevictable_pages",
1332                 .data           = &scan_unevictable_pages,
1333                 .maxlen         = sizeof(scan_unevictable_pages),
1334                 .mode           = 0644,
1335                 .proc_handler   = scan_unevictable_handler,
1336         },
1337 #ifdef CONFIG_MEMORY_FAILURE
1338         {
1339                 .procname       = "memory_failure_early_kill",
1340                 .data           = &sysctl_memory_failure_early_kill,
1341                 .maxlen         = sizeof(sysctl_memory_failure_early_kill),
1342                 .mode           = 0644,
1343                 .proc_handler   = proc_dointvec_minmax,
1344                 .extra1         = &zero,
1345                 .extra2         = &one,
1346         },
1347         {
1348                 .procname       = "memory_failure_recovery",
1349                 .data           = &sysctl_memory_failure_recovery,
1350                 .maxlen         = sizeof(sysctl_memory_failure_recovery),
1351                 .mode           = 0644,
1352                 .proc_handler   = proc_dointvec_minmax,
1353                 .extra1         = &zero,
1354                 .extra2         = &one,
1355         },
1356 #endif
1357         { }
1358 };
1359
1360 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1361 static struct ctl_table binfmt_misc_table[] = {
1362         { }
1363 };
1364 #endif
1365
1366 static struct ctl_table fs_table[] = {
1367         {
1368                 .procname       = "inode-nr",
1369                 .data           = &inodes_stat,
1370                 .maxlen         = 2*sizeof(int),
1371                 .mode           = 0444,
1372                 .proc_handler   = proc_nr_inodes,
1373         },
1374         {
1375                 .procname       = "inode-state",
1376                 .data           = &inodes_stat,
1377                 .maxlen         = 7*sizeof(int),
1378                 .mode           = 0444,
1379                 .proc_handler   = proc_nr_inodes,
1380         },
1381         {
1382                 .procname       = "file-nr",
1383                 .data           = &files_stat,
1384                 .maxlen         = sizeof(files_stat),
1385                 .mode           = 0444,
1386                 .proc_handler   = proc_nr_files,
1387         },
1388         {
1389                 .procname       = "file-max",
1390                 .data           = &files_stat.max_files,
1391                 .maxlen         = sizeof(files_stat.max_files),
1392                 .mode           = 0644,
1393                 .proc_handler   = proc_doulongvec_minmax,
1394         },
1395         {
1396                 .procname       = "nr_open",
1397                 .data           = &sysctl_nr_open,
1398                 .maxlen         = sizeof(int),
1399                 .mode           = 0644,
1400                 .proc_handler   = proc_dointvec_minmax,
1401                 .extra1         = &sysctl_nr_open_min,
1402                 .extra2         = &sysctl_nr_open_max,
1403         },
1404         {
1405                 .procname       = "dentry-state",
1406                 .data           = &dentry_stat,
1407                 .maxlen         = 6*sizeof(int),
1408                 .mode           = 0444,
1409                 .proc_handler   = proc_nr_dentry,
1410         },
1411         {
1412                 .procname       = "overflowuid",
1413                 .data           = &fs_overflowuid,
1414                 .maxlen         = sizeof(int),
1415                 .mode           = 0644,
1416                 .proc_handler   = proc_dointvec_minmax,
1417                 .extra1         = &minolduid,
1418                 .extra2         = &maxolduid,
1419         },
1420         {
1421                 .procname       = "overflowgid",
1422                 .data           = &fs_overflowgid,
1423                 .maxlen         = sizeof(int),
1424                 .mode           = 0644,
1425                 .proc_handler   = proc_dointvec_minmax,
1426                 .extra1         = &minolduid,
1427                 .extra2         = &maxolduid,
1428         },
1429 #ifdef CONFIG_FILE_LOCKING
1430         {
1431                 .procname       = "leases-enable",
1432                 .data           = &leases_enable,
1433                 .maxlen         = sizeof(int),
1434                 .mode           = 0644,
1435                 .proc_handler   = proc_dointvec,
1436         },
1437 #endif
1438 #ifdef CONFIG_DNOTIFY
1439         {
1440                 .procname       = "dir-notify-enable",
1441                 .data           = &dir_notify_enable,
1442                 .maxlen         = sizeof(int),
1443                 .mode           = 0644,
1444                 .proc_handler   = proc_dointvec,
1445         },
1446 #endif
1447 #ifdef CONFIG_MMU
1448 #ifdef CONFIG_FILE_LOCKING
1449         {
1450                 .procname       = "lease-break-time",
1451                 .data           = &lease_break_time,
1452                 .maxlen         = sizeof(int),
1453                 .mode           = 0644,
1454                 .proc_handler   = proc_dointvec,
1455         },
1456 #endif
1457 #ifdef CONFIG_AIO
1458         {
1459                 .procname       = "aio-nr",
1460                 .data           = &aio_nr,
1461                 .maxlen         = sizeof(aio_nr),
1462                 .mode           = 0444,
1463                 .proc_handler   = proc_doulongvec_minmax,
1464         },
1465         {
1466                 .procname       = "aio-max-nr",
1467                 .data           = &aio_max_nr,
1468                 .maxlen         = sizeof(aio_max_nr),
1469                 .mode           = 0644,
1470                 .proc_handler   = proc_doulongvec_minmax,
1471         },
1472 #endif /* CONFIG_AIO */
1473 #ifdef CONFIG_INOTIFY_USER
1474         {
1475                 .procname       = "inotify",
1476                 .mode           = 0555,
1477                 .child          = inotify_table,
1478         },
1479 #endif  
1480 #ifdef CONFIG_EPOLL
1481         {
1482                 .procname       = "epoll",
1483                 .mode           = 0555,
1484                 .child          = epoll_table,
1485         },
1486 #endif
1487 #endif
1488         {
1489                 .procname       = "suid_dumpable",
1490                 .data           = &suid_dumpable,
1491                 .maxlen         = sizeof(int),
1492                 .mode           = 0644,
1493                 .proc_handler   = proc_dointvec_minmax,
1494                 .extra1         = &zero,
1495                 .extra2         = &two,
1496         },
1497 #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
1498         {
1499                 .procname       = "binfmt_misc",
1500                 .mode           = 0555,
1501                 .child          = binfmt_misc_table,
1502         },
1503 #endif
1504         {
1505                 .procname       = "pipe-max-size",
1506                 .data           = &pipe_max_size,
1507                 .maxlen         = sizeof(int),
1508                 .mode           = 0644,
1509                 .proc_handler   = &pipe_proc_fn,
1510                 .extra1         = &pipe_min_size,
1511         },
1512         { }
1513 };
1514
1515 static struct ctl_table debug_table[] = {
1516 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) || \
1517     defined(CONFIG_S390) || defined(CONFIG_TILE)
1518         {
1519                 .procname       = "exception-trace",
1520                 .data           = &show_unhandled_signals,
1521                 .maxlen         = sizeof(int),
1522                 .mode           = 0644,
1523                 .proc_handler   = proc_dointvec
1524         },
1525 #endif
1526 #if defined(CONFIG_OPTPROBES)
1527         {
1528                 .procname       = "kprobes-optimization",
1529                 .data           = &sysctl_kprobes_optimization,
1530                 .maxlen         = sizeof(int),
1531                 .mode           = 0644,
1532                 .proc_handler   = proc_kprobes_optimization_handler,
1533                 .extra1         = &zero,
1534                 .extra2         = &one,
1535         },
1536 #endif
1537         { }
1538 };
1539
1540 static struct ctl_table dev_table[] = {
1541         { }
1542 };
1543
1544 static DEFINE_SPINLOCK(sysctl_lock);
1545
1546 /* called under sysctl_lock */
1547 static int use_table(struct ctl_table_header *p)
1548 {
1549         if (unlikely(p->unregistering))
1550                 return 0;
1551         p->used++;
1552         return 1;
1553 }
1554
1555 /* called under sysctl_lock */
1556 static void unuse_table(struct ctl_table_header *p)
1557 {
1558         if (!--p->used)
1559                 if (unlikely(p->unregistering))
1560                         complete(p->unregistering);
1561 }
1562
1563 /* called under sysctl_lock, will reacquire if has to wait */
1564 static void start_unregistering(struct ctl_table_header *p)
1565 {
1566         /*
1567          * if p->used is 0, nobody will ever touch that entry again;
1568          * we'll eliminate all paths to it before dropping sysctl_lock
1569          */
1570         if (unlikely(p->used)) {
1571                 struct completion wait;
1572                 init_completion(&wait);
1573                 p->unregistering = &wait;
1574                 spin_unlock(&sysctl_lock);
1575                 wait_for_completion(&wait);
1576                 spin_lock(&sysctl_lock);
1577         } else {
1578                 /* anything non-NULL; we'll never dereference it */
1579                 p->unregistering = ERR_PTR(-EINVAL);
1580         }
1581         /*
1582          * do not remove from the list until nobody holds it; walking the
1583          * list in do_sysctl() relies on that.
1584          */
1585         list_del_init(&p->ctl_entry);
1586 }
1587
1588 void sysctl_head_get(struct ctl_table_header *head)
1589 {
1590         spin_lock(&sysctl_lock);
1591         head->count++;
1592         spin_unlock(&sysctl_lock);
1593 }
1594
1595 void sysctl_head_put(struct ctl_table_header *head)
1596 {
1597         spin_lock(&sysctl_lock);
1598         if (!--head->count)
1599                 kfree_rcu(head, rcu);
1600         spin_unlock(&sysctl_lock);
1601 }
1602
1603 struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1604 {
1605         if (!head)
1606                 BUG();
1607         spin_lock(&sysctl_lock);
1608         if (!use_table(head))
1609                 head = ERR_PTR(-ENOENT);
1610         spin_unlock(&sysctl_lock);
1611         return head;
1612 }
1613
1614 void sysctl_head_finish(struct ctl_table_header *head)
1615 {
1616         if (!head)
1617                 return;
1618         spin_lock(&sysctl_lock);
1619         unuse_table(head);
1620         spin_unlock(&sysctl_lock);
1621 }
1622
1623 static struct ctl_table_set *
1624 lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1625 {
1626         struct ctl_table_set *set = &root->default_set;
1627         if (root->lookup)
1628                 set = root->lookup(root, namespaces);
1629         return set;
1630 }
1631
1632 static struct list_head *
1633 lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1634 {
1635         struct ctl_table_set *set = lookup_header_set(root, namespaces);
1636         return &set->list;
1637 }
1638
1639 struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1640                                             struct ctl_table_header *prev)
1641 {
1642         struct ctl_table_root *root;
1643         struct list_head *header_list;
1644         struct ctl_table_header *head;
1645         struct list_head *tmp;
1646
1647         spin_lock(&sysctl_lock);
1648         if (prev) {
1649                 head = prev;
1650                 tmp = &prev->ctl_entry;
1651                 unuse_table(prev);
1652                 goto next;
1653         }
1654         tmp = &root_table_header.ctl_entry;
1655         for (;;) {
1656                 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1657
1658                 if (!use_table(head))
1659                         goto next;
1660                 spin_unlock(&sysctl_lock);
1661                 return head;
1662         next:
1663                 root = head->root;
1664                 tmp = tmp->next;
1665                 header_list = lookup_header_list(root, namespaces);
1666                 if (tmp != header_list)
1667                         continue;
1668
1669                 do {
1670                         root = list_entry(root->root_list.next,
1671                                         struct ctl_table_root, root_list);
1672                         if (root == &sysctl_table_root)
1673                                 goto out;
1674                         header_list = lookup_header_list(root, namespaces);
1675                 } while (list_empty(header_list));
1676                 tmp = header_list->next;
1677         }
1678 out:
1679         spin_unlock(&sysctl_lock);
1680         return NULL;
1681 }
1682
1683 struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1684 {
1685         return __sysctl_head_next(current->nsproxy, prev);
1686 }
1687
1688 void register_sysctl_root(struct ctl_table_root *root)
1689 {
1690         spin_lock(&sysctl_lock);
1691         list_add_tail(&root->root_list, &sysctl_table_root.root_list);
1692         spin_unlock(&sysctl_lock);
1693 }
1694
1695 /*
1696  * sysctl_perm does NOT grant the superuser all rights automatically, because
1697  * some sysctl variables are readonly even to root.
1698  */
1699
1700 static int test_perm(int mode, int op)
1701 {
1702         if (!current_euid())
1703                 mode >>= 6;
1704         else if (in_egroup_p(0))
1705                 mode >>= 3;
1706         if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
1707                 return 0;
1708         return -EACCES;
1709 }
1710
1711 int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
1712 {
1713         int mode;
1714
1715         if (root->permissions)
1716                 mode = root->permissions(root, current->nsproxy, table);
1717         else
1718                 mode = table->mode;
1719
1720         return test_perm(mode, op);
1721 }
1722
1723 static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
1724 {
1725         for (; table->procname; table++) {
1726                 table->parent = parent;
1727                 if (table->child)
1728                         sysctl_set_parent(table, table->child);
1729         }
1730 }
1731
1732 static __init int sysctl_init(void)
1733 {
1734         sysctl_set_parent(NULL, root_table);
1735 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1736         sysctl_check_table(current->nsproxy, root_table);
1737 #endif
1738         return 0;
1739 }
1740
1741 core_initcall(sysctl_init);
1742
1743 static struct ctl_table *is_branch_in(struct ctl_table *branch,
1744                                       struct ctl_table *table)
1745 {
1746         struct ctl_table *p;
1747         const char *s = branch->procname;
1748
1749         /* branch should have named subdirectory as its first element */
1750         if (!s || !branch->child)
1751                 return NULL;
1752
1753         /* ... and nothing else */
1754         if (branch[1].procname)
1755                 return NULL;
1756
1757         /* table should contain subdirectory with the same name */
1758         for (p = table; p->procname; p++) {
1759                 if (!p->child)
1760                         continue;
1761                 if (p->procname && strcmp(p->procname, s) == 0)
1762                         return p;
1763         }
1764         return NULL;
1765 }
1766
1767 /* see if attaching q to p would be an improvement */
1768 static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
1769 {
1770         struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
1771         struct ctl_table *next;
1772         int is_better = 0;
1773         int not_in_parent = !p->attached_by;
1774
1775         while ((next = is_branch_in(by, to)) != NULL) {
1776                 if (by == q->attached_by)
1777                         is_better = 1;
1778                 if (to == p->attached_by)
1779                         not_in_parent = 1;
1780                 by = by->child;
1781                 to = next->child;
1782         }
1783
1784         if (is_better && not_in_parent) {
1785                 q->attached_by = by;
1786                 q->attached_to = to;
1787                 q->parent = p;
1788         }
1789 }
1790
1791 /**
1792  * __register_sysctl_paths - register a sysctl hierarchy
1793  * @root: List of sysctl headers to register on
1794  * @namespaces: Data to compute which lists of sysctl entries are visible
1795  * @path: The path to the directory the sysctl table is in.
1796  * @table: the top-level table structure
1797  *
1798  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1799  * array. A completely 0 filled entry terminates the table.
1800  *
1801  * The members of the &struct ctl_table structure are used as follows:
1802  *
1803  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1804  *            enter a sysctl file
1805  *
1806  * data - a pointer to data for use by proc_handler
1807  *
1808  * maxlen - the maximum size in bytes of the data
1809  *
1810  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1811  *
1812  * child - a pointer to the child sysctl table if this entry is a directory, or
1813  *         %NULL.
1814  *
1815  * proc_handler - the text handler routine (described below)
1816  *
1817  * de - for internal use by the sysctl routines
1818  *
1819  * extra1, extra2 - extra pointers usable by the proc handler routines
1820  *
1821  * Leaf nodes in the sysctl tree will be represented by a single file
1822  * under /proc; non-leaf nodes will be represented by directories.
1823  *
1824  * sysctl(2) can automatically manage read and write requests through
1825  * the sysctl table.  The data and maxlen fields of the ctl_table
1826  * struct enable minimal validation of the values being written to be
1827  * performed, and the mode field allows minimal authentication.
1828  *
1829  * There must be a proc_handler routine for any terminal nodes
1830  * mirrored under /proc/sys (non-terminals are handled by a built-in
1831  * directory handler).  Several default handlers are available to
1832  * cover common cases -
1833  *
1834  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1835  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 
1836  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1837  *
1838  * It is the handler's job to read the input buffer from user memory
1839  * and process it. The handler should return 0 on success.
1840  *
1841  * This routine returns %NULL on a failure to register, and a pointer
1842  * to the table header on success.
1843  */
1844 struct ctl_table_header *__register_sysctl_paths(
1845         struct ctl_table_root *root,
1846         struct nsproxy *namespaces,
1847         const struct ctl_path *path, struct ctl_table *table)
1848 {
1849         struct ctl_table_header *header;
1850         struct ctl_table *new, **prevp;
1851         unsigned int n, npath;
1852         struct ctl_table_set *set;
1853
1854         /* Count the path components */
1855         for (npath = 0; path[npath].procname; ++npath)
1856                 ;
1857
1858         /*
1859          * For each path component, allocate a 2-element ctl_table array.
1860          * The first array element will be filled with the sysctl entry
1861          * for this, the second will be the sentinel (procname == 0).
1862          *
1863          * We allocate everything in one go so that we don't have to
1864          * worry about freeing additional memory in unregister_sysctl_table.
1865          */
1866         header = kzalloc(sizeof(struct ctl_table_header) +
1867                          (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
1868         if (!header)
1869                 return NULL;
1870
1871         new = (struct ctl_table *) (header + 1);
1872
1873         /* Now connect the dots */
1874         prevp = &header->ctl_table;
1875         for (n = 0; n < npath; ++n, ++path) {
1876                 /* Copy the procname */
1877                 new->procname = path->procname;
1878                 new->mode     = 0555;
1879
1880                 *prevp = new;
1881                 prevp = &new->child;
1882
1883                 new += 2;
1884         }
1885         *prevp = table;
1886         header->ctl_table_arg = table;
1887
1888         INIT_LIST_HEAD(&header->ctl_entry);
1889         header->used = 0;
1890         header->unregistering = NULL;
1891         header->root = root;
1892         sysctl_set_parent(NULL, header->ctl_table);
1893         header->count = 1;
1894 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1895         if (sysctl_check_table(namespaces, header->ctl_table)) {
1896                 kfree(header);
1897                 return NULL;
1898         }
1899 #endif
1900         spin_lock(&sysctl_lock);
1901         header->set = lookup_header_set(root, namespaces);
1902         header->attached_by = header->ctl_table;
1903         header->attached_to = root_table;
1904         header->parent = &root_table_header;
1905         for (set = header->set; set; set = set->parent) {
1906                 struct ctl_table_header *p;
1907                 list_for_each_entry(p, &set->list, ctl_entry) {
1908                         if (p->unregistering)
1909                                 continue;
1910                         try_attach(p, header);
1911                 }
1912         }
1913         header->parent->count++;
1914         list_add_tail(&header->ctl_entry, &header->set->list);
1915         spin_unlock(&sysctl_lock);
1916
1917         return header;
1918 }
1919
1920 /**
1921  * register_sysctl_table_path - register a sysctl table hierarchy
1922  * @path: The path to the directory the sysctl table is in.
1923  * @table: the top-level table structure
1924  *
1925  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1926  * array. A completely 0 filled entry terminates the table.
1927  *
1928  * See __register_sysctl_paths for more details.
1929  */
1930 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1931                                                 struct ctl_table *table)
1932 {
1933         return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1934                                         path, table);
1935 }
1936
1937 /**
1938  * register_sysctl_table - register a sysctl table hierarchy
1939  * @table: the top-level table structure
1940  *
1941  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1942  * array. A completely 0 filled entry terminates the table.
1943  *
1944  * See register_sysctl_paths for more details.
1945  */
1946 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1947 {
1948         static const struct ctl_path null_path[] = { {} };
1949
1950         return register_sysctl_paths(null_path, table);
1951 }
1952
1953 /**
1954  * unregister_sysctl_table - unregister a sysctl table hierarchy
1955  * @header: the header returned from register_sysctl_table
1956  *
1957  * Unregisters the sysctl table and all children. proc entries may not
1958  * actually be removed until they are no longer used by anyone.
1959  */
1960 void unregister_sysctl_table(struct ctl_table_header * header)
1961 {
1962         might_sleep();
1963
1964         if (header == NULL)
1965                 return;
1966
1967         spin_lock(&sysctl_lock);
1968         start_unregistering(header);
1969         if (!--header->parent->count) {
1970                 WARN_ON(1);
1971                 kfree_rcu(header->parent, rcu);
1972         }
1973         if (!--header->count)
1974                 kfree_rcu(header, rcu);
1975         spin_unlock(&sysctl_lock);
1976 }
1977
1978 int sysctl_is_seen(struct ctl_table_header *p)
1979 {
1980         struct ctl_table_set *set = p->set;
1981         int res;
1982         spin_lock(&sysctl_lock);
1983         if (p->unregistering)
1984                 res = 0;
1985         else if (!set->is_seen)
1986                 res = 1;
1987         else
1988                 res = set->is_seen(set);
1989         spin_unlock(&sysctl_lock);
1990         return res;
1991 }
1992
1993 void setup_sysctl_set(struct ctl_table_set *p,
1994         struct ctl_table_set *parent,
1995         int (*is_seen)(struct ctl_table_set *))
1996 {
1997         INIT_LIST_HEAD(&p->list);
1998         p->parent = parent ? parent : &sysctl_table_root.default_set;
1999         p->is_seen = is_seen;
2000 }
2001
2002 #else /* !CONFIG_SYSCTL */
2003 struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
2004 {
2005         return NULL;
2006 }
2007
2008 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
2009                                                     struct ctl_table *table)
2010 {
2011         return NULL;
2012 }
2013
2014 void unregister_sysctl_table(struct ctl_table_header * table)
2015 {
2016 }
2017
2018 void setup_sysctl_set(struct ctl_table_set *p,
2019         struct ctl_table_set *parent,
2020         int (*is_seen)(struct ctl_table_set *))
2021 {
2022 }
2023
2024 void sysctl_head_put(struct ctl_table_header *head)
2025 {
2026 }
2027
2028 #endif /* CONFIG_SYSCTL */
2029
2030 /*
2031  * /proc/sys support
2032  */
2033
2034 #ifdef CONFIG_PROC_SYSCTL
2035
2036 static int _proc_do_string(void* data, int maxlen, int write,
2037                            void __user *buffer,
2038                            size_t *lenp, loff_t *ppos)
2039 {
2040         size_t len;
2041         char __user *p;
2042         char c;
2043
2044         if (!data || !maxlen || !*lenp) {
2045                 *lenp = 0;
2046                 return 0;
2047         }
2048
2049         if (write) {
2050                 len = 0;
2051                 p = buffer;
2052                 while (len < *lenp) {
2053                         if (get_user(c, p++))
2054                                 return -EFAULT;
2055                         if (c == 0 || c == '\n')
2056                                 break;
2057                         len++;
2058                 }
2059                 if (len >= maxlen)
2060                         len = maxlen-1;
2061                 if(copy_from_user(data, buffer, len))
2062                         return -EFAULT;
2063                 ((char *) data)[len] = 0;
2064                 *ppos += *lenp;
2065         } else {
2066                 len = strlen(data);
2067                 if (len > maxlen)
2068                         len = maxlen;
2069
2070                 if (*ppos > len) {
2071                         *lenp = 0;
2072                         return 0;
2073                 }
2074
2075                 data += *ppos;
2076                 len  -= *ppos;
2077
2078                 if (len > *lenp)
2079                         len = *lenp;
2080                 if (len)
2081                         if(copy_to_user(buffer, data, len))
2082                                 return -EFAULT;
2083                 if (len < *lenp) {
2084                         if(put_user('\n', ((char __user *) buffer) + len))
2085                                 return -EFAULT;
2086                         len++;
2087                 }
2088                 *lenp = len;
2089                 *ppos += len;
2090         }
2091         return 0;
2092 }
2093
2094 /**
2095  * proc_dostring - read a string sysctl
2096  * @table: the sysctl table
2097  * @write: %TRUE if this is a write to the sysctl file
2098  * @buffer: the user buffer
2099  * @lenp: the size of the user buffer
2100  * @ppos: file position
2101  *
2102  * Reads/writes a string from/to the user buffer. If the kernel
2103  * buffer provided is not large enough to hold the string, the
2104  * string is truncated. The copied string is %NULL-terminated.
2105  * If the string is being read by the user process, it is copied
2106  * and a newline '\n' is added. It is truncated if the buffer is
2107  * not large enough.
2108  *
2109  * Returns 0 on success.
2110  */
2111 int proc_dostring(struct ctl_table *table, int write,
2112                   void __user *buffer, size_t *lenp, loff_t *ppos)
2113 {
2114         return _proc_do_string(table->data, table->maxlen, write,
2115                                buffer, lenp, ppos);
2116 }
2117
2118 static size_t proc_skip_spaces(char **buf)
2119 {
2120         size_t ret;
2121         char *tmp = skip_spaces(*buf);
2122         ret = tmp - *buf;
2123         *buf = tmp;
2124         return ret;
2125 }
2126
2127 static void proc_skip_char(char **buf, size_t *size, const char v)
2128 {
2129         while (*size) {
2130                 if (**buf != v)
2131                         break;
2132                 (*size)--;
2133                 (*buf)++;
2134         }
2135 }
2136
2137 #define TMPBUFLEN 22
2138 /**
2139  * proc_get_long - reads an ASCII formatted integer from a user buffer
2140  *
2141  * @buf: a kernel buffer
2142  * @size: size of the kernel buffer
2143  * @val: this is where the number will be stored
2144  * @neg: set to %TRUE if number is negative
2145  * @perm_tr: a vector which contains the allowed trailers
2146  * @perm_tr_len: size of the perm_tr vector
2147  * @tr: pointer to store the trailer character
2148  *
2149  * In case of success %0 is returned and @buf and @size are updated with
2150  * the amount of bytes read. If @tr is non-NULL and a trailing
2151  * character exists (size is non-zero after returning from this
2152  * function), @tr is updated with the trailing character.
2153  */
2154 static int proc_get_long(char **buf, size_t *size,
2155                           unsigned long *val, bool *neg,
2156                           const char *perm_tr, unsigned perm_tr_len, char *tr)
2157 {
2158         int len;
2159         char *p, tmp[TMPBUFLEN];
2160
2161         if (!*size)
2162                 return -EINVAL;
2163
2164         len = *size;
2165         if (len > TMPBUFLEN - 1)
2166                 len = TMPBUFLEN - 1;
2167
2168         memcpy(tmp, *buf, len);
2169
2170         tmp[len] = 0;
2171         p = tmp;
2172         if (*p == '-' && *size > 1) {
2173                 *neg = true;
2174                 p++;
2175         } else
2176                 *neg = false;
2177         if (!isdigit(*p))
2178                 return -EINVAL;
2179
2180         *val = simple_strtoul(p, &p, 0);
2181
2182         len = p - tmp;
2183
2184         /* We don't know if the next char is whitespace thus we may accept
2185          * invalid integers (e.g. 1234...a) or two integers instead of one
2186          * (e.g. 123...1). So lets not allow such large numbers. */
2187         if (len == TMPBUFLEN - 1)
2188                 return -EINVAL;
2189
2190         if (len < *size && perm_tr_len && !memchr(perm_tr, *p, perm_tr_len))
2191                 return -EINVAL;
2192
2193         if (tr && (len < *size))
2194                 *tr = *p;
2195
2196         *buf += len;
2197         *size -= len;
2198
2199         return 0;
2200 }
2201
2202 /**
2203  * proc_put_long - converts an integer to a decimal ASCII formatted string
2204  *
2205  * @buf: the user buffer
2206  * @size: the size of the user buffer
2207  * @val: the integer to be converted
2208  * @neg: sign of the number, %TRUE for negative
2209  *
2210  * In case of success %0 is returned and @buf and @size are updated with
2211  * the amount of bytes written.
2212  */
2213 static int proc_put_long(void __user **buf, size_t *size, unsigned long val,
2214                           bool neg)
2215 {
2216         int len;
2217         char tmp[TMPBUFLEN], *p = tmp;
2218
2219         sprintf(p, "%s%lu", neg ? "-" : "", val);
2220         len = strlen(tmp);
2221         if (len > *size)
2222                 len = *size;
2223         if (copy_to_user(*buf, tmp, len))
2224                 return -EFAULT;
2225         *size -= len;
2226         *buf += len;
2227         return 0;
2228 }
2229 #undef TMPBUFLEN
2230
2231 static int proc_put_char(void __user **buf, size_t *size, char c)
2232 {
2233         if (*size) {
2234                 char __user **buffer = (char __user **)buf;
2235                 if (put_user(c, *buffer))
2236                         return -EFAULT;
2237                 (*size)--, (*buffer)++;
2238                 *buf = *buffer;
2239         }
2240         return 0;
2241 }
2242
2243 static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
2244                                  int *valp,
2245                                  int write, void *data)
2246 {
2247         if (write) {
2248                 *valp = *negp ? -*lvalp : *lvalp;
2249         } else {
2250                 int val = *valp;
2251                 if (val < 0) {
2252                         *negp = true;
2253                         *lvalp = (unsigned long)-val;
2254                 } else {
2255                         *negp = false;
2256                         *lvalp = (unsigned long)val;
2257                 }
2258         }
2259         return 0;
2260 }
2261
2262 static const char proc_wspace_sep[] = { ' ', '\t', '\n' };
2263
2264 static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table,
2265                   int write, void __user *buffer,
2266                   size_t *lenp, loff_t *ppos,
2267                   int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
2268                               int write, void *data),
2269                   void *data)
2270 {
2271         int *i, vleft, first = 1, err = 0;
2272         unsigned long page = 0;
2273         size_t left;
2274         char *kbuf;
2275         
2276         if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
2277                 *lenp = 0;
2278                 return 0;
2279         }
2280         
2281         i = (int *) tbl_data;
2282         vleft = table->maxlen / sizeof(*i);
2283         left = *lenp;
2284
2285         if (!conv)
2286                 conv = do_proc_dointvec_conv;
2287
2288         if (write) {
2289                 if (left > PAGE_SIZE - 1)
2290                         left = PAGE_SIZE - 1;
2291                 page = __get_free_page(GFP_TEMPORARY);
2292                 kbuf = (char *) page;
2293                 if (!kbuf)
2294                         return -ENOMEM;
2295                 if (copy_from_user(kbuf, buffer, left)) {
2296                         err = -EFAULT;
2297                         goto free;
2298                 }
2299                 kbuf[left] = 0;
2300         }
2301
2302         for (; left && vleft--; i++, first=0) {
2303                 unsigned long lval;
2304                 bool neg;
2305
2306                 if (write) {
2307                         left -= proc_skip_spaces(&kbuf);
2308
2309                         if (!left)
2310                                 break;
2311                         err = proc_get_long(&kbuf, &left, &lval, &neg,
2312                                              proc_wspace_sep,
2313                                              sizeof(proc_wspace_sep), NULL);
2314                         if (err)
2315                                 break;
2316                         if (conv(&neg, &lval, i, 1, data)) {
2317                                 err = -EINVAL;
2318                                 break;
2319                         }
2320                 } else {
2321                         if (conv(&neg, &lval, i, 0, data)) {
2322                                 err = -EINVAL;
2323                                 break;
2324                         }
2325                         if (!first)
2326                                 err = proc_put_char(&buffer, &left, '\t');
2327                         if (err)
2328                                 break;
2329                         err = proc_put_long(&buffer, &left, lval, neg);
2330                         if (err)
2331                                 break;
2332                 }
2333         }
2334
2335         if (!write && !first && left && !err)
2336                 err = proc_put_char(&buffer, &left, '\n');
2337         if (write && !err && left)
2338                 left -= proc_skip_spaces(&kbuf);
2339 free:
2340         if (write) {
2341                 free_page(page);
2342                 if (first)
2343                         return err ? : -EINVAL;
2344         }
2345         *lenp -= left;
2346         *ppos += *lenp;
2347         return err;
2348 }
2349
2350 static int do_proc_dointvec(struct ctl_table *table, int write,
2351                   void __user *buffer, size_t *lenp, loff_t *ppos,
2352                   int (*conv)(bool *negp, unsigned long *lvalp, int *valp,
2353                               int write, void *data),
2354                   void *data)
2355 {
2356         return __do_proc_dointvec(table->data, table, write,
2357                         buffer, lenp, ppos, conv, data);
2358 }
2359
2360 /**
2361  * proc_dointvec - read a vector of integers
2362  * @table: the sysctl table
2363  * @write: %TRUE if this is a write to the sysctl file
2364  * @buffer: the user buffer
2365  * @lenp: the size of the user buffer
2366  * @ppos: file position
2367  *
2368  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2369  * values from/to the user buffer, treated as an ASCII string. 
2370  *
2371  * Returns 0 on success.
2372  */
2373 int proc_dointvec(struct ctl_table *table, int write,
2374                      void __user *buffer, size_t *lenp, loff_t *ppos)
2375 {
2376     return do_proc_dointvec(table,write,buffer,lenp,ppos,
2377                             NULL,NULL);
2378 }
2379
2380 /*
2381  * Taint values can only be increased
2382  * This means we can safely use a temporary.
2383  */
2384 static int proc_taint(struct ctl_table *table, int write,
2385                                void __user *buffer, size_t *lenp, loff_t *ppos)
2386 {
2387         struct ctl_table t;
2388         unsigned long tmptaint = get_taint();
2389         int err;
2390
2391         if (write && !capable(CAP_SYS_ADMIN))
2392                 return -EPERM;
2393
2394         t = *table;
2395         t.data = &tmptaint;
2396         err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
2397         if (err < 0)
2398                 return err;
2399
2400         if (write) {
2401                 /*
2402                  * Poor man's atomic or. Not worth adding a primitive
2403                  * to everyone's atomic.h for this
2404                  */
2405                 int i;
2406                 for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) {
2407                         if ((tmptaint >> i) & 1)
2408                                 add_taint(i);
2409                 }
2410         }
2411
2412         return err;
2413 }
2414
2415 #ifdef CONFIG_PRINTK
2416 static int proc_dmesg_restrict(struct ctl_table *table, int write,
2417                                 void __user *buffer, size_t *lenp, loff_t *ppos)
2418 {
2419         if (write && !capable(CAP_SYS_ADMIN))
2420                 return -EPERM;
2421
2422         return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2423 }
2424 #endif
2425
2426 struct do_proc_dointvec_minmax_conv_param {
2427         int *min;
2428         int *max;
2429 };
2430
2431 static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,
2432                                         int *valp,
2433                                         int write, void *data)
2434 {
2435         struct do_proc_dointvec_minmax_conv_param *param = data;
2436         if (write) {
2437                 int val = *negp ? -*lvalp : *lvalp;
2438                 if ((param->min && *param->min > val) ||
2439                     (param->max && *param->max < val))
2440                         return -EINVAL;
2441                 *valp = val;
2442         } else {
2443                 int val = *valp;
2444                 if (val < 0) {
2445                         *negp = true;
2446                         *lvalp = (unsigned long)-val;
2447                 } else {
2448                         *negp = false;
2449                         *lvalp = (unsigned long)val;
2450                 }
2451         }
2452         return 0;
2453 }
2454
2455 /**
2456  * proc_dointvec_minmax - read a vector of integers with min/max values
2457  * @table: the sysctl table
2458  * @write: %TRUE if this is a write to the sysctl file
2459  * @buffer: the user buffer
2460  * @lenp: the size of the user buffer
2461  * @ppos: file position
2462  *
2463  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2464  * values from/to the user buffer, treated as an ASCII string.
2465  *
2466  * This routine will ensure the values are within the range specified by
2467  * table->extra1 (min) and table->extra2 (max).
2468  *
2469  * Returns 0 on success.
2470  */
2471 int proc_dointvec_minmax(struct ctl_table *table, int write,
2472                   void __user *buffer, size_t *lenp, loff_t *ppos)
2473 {
2474         struct do_proc_dointvec_minmax_conv_param param = {
2475                 .min = (int *) table->extra1,
2476                 .max = (int *) table->extra2,
2477         };
2478         return do_proc_dointvec(table, write, buffer, lenp, ppos,
2479                                 do_proc_dointvec_minmax_conv, &param);
2480 }
2481
2482 static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int write,
2483                                      void __user *buffer,
2484                                      size_t *lenp, loff_t *ppos,
2485                                      unsigned long convmul,
2486                                      unsigned long convdiv)
2487 {
2488         unsigned long *i, *min, *max;
2489         int vleft, first = 1, err = 0;
2490         unsigned long page = 0;
2491         size_t left;
2492         char *kbuf;
2493
2494         if (!data || !table->maxlen || !*lenp || (*ppos && !write)) {
2495                 *lenp = 0;
2496                 return 0;
2497         }
2498
2499         i = (unsigned long *) data;
2500         min = (unsigned long *) table->extra1;
2501         max = (unsigned long *) table->extra2;
2502         vleft = table->maxlen / sizeof(unsigned long);
2503         left = *lenp;
2504
2505         if (write) {
2506                 if (left > PAGE_SIZE - 1)
2507                         left = PAGE_SIZE - 1;
2508                 page = __get_free_page(GFP_TEMPORARY);
2509                 kbuf = (char *) page;
2510                 if (!kbuf)
2511                         return -ENOMEM;
2512                 if (copy_from_user(kbuf, buffer, left)) {
2513                         err = -EFAULT;
2514                         goto free;
2515                 }
2516                 kbuf[left] = 0;
2517         }
2518
2519         for (; left && vleft--; i++, first = 0) {
2520                 unsigned long val;
2521
2522                 if (write) {
2523                         bool neg;
2524
2525                         left -= proc_skip_spaces(&kbuf);
2526
2527                         err = proc_get_long(&kbuf, &left, &val, &neg,
2528                                              proc_wspace_sep,
2529                                              sizeof(proc_wspace_sep), NULL);
2530                         if (err)
2531                                 break;
2532                         if (neg)
2533                                 continue;
2534                         if ((min && val < *min) || (max && val > *max))
2535                                 continue;
2536                         *i = val;
2537                 } else {
2538                         val = convdiv * (*i) / convmul;
2539                         if (!first)
2540                                 err = proc_put_char(&buffer, &left, '\t');
2541                         err = proc_put_long(&buffer, &left, val, false);
2542                         if (err)
2543                                 break;
2544                 }
2545         }
2546
2547         if (!write && !first && left && !err)
2548                 err = proc_put_char(&buffer, &left, '\n');
2549         if (write && !err)
2550                 left -= proc_skip_spaces(&kbuf);
2551 free:
2552         if (write) {
2553                 free_page(page);
2554                 if (first)
2555                         return err ? : -EINVAL;
2556         }
2557         *lenp -= left;
2558         *ppos += *lenp;
2559         return err;
2560 }
2561
2562 static int do_proc_doulongvec_minmax(struct ctl_table *table, int write,
2563                                      void __user *buffer,
2564                                      size_t *lenp, loff_t *ppos,
2565                                      unsigned long convmul,
2566                                      unsigned long convdiv)
2567 {
2568         return __do_proc_doulongvec_minmax(table->data, table, write,
2569                         buffer, lenp, ppos, convmul, convdiv);
2570 }
2571
2572 /**
2573  * proc_doulongvec_minmax - read a vector of long integers with min/max values
2574  * @table: the sysctl table
2575  * @write: %TRUE if this is a write to the sysctl file
2576  * @buffer: the user buffer
2577  * @lenp: the size of the user buffer
2578  * @ppos: file position
2579  *
2580  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2581  * values from/to the user buffer, treated as an ASCII string.
2582  *
2583  * This routine will ensure the values are within the range specified by
2584  * table->extra1 (min) and table->extra2 (max).
2585  *
2586  * Returns 0 on success.
2587  */
2588 int proc_doulongvec_minmax(struct ctl_table *table, int write,
2589                            void __user *buffer, size_t *lenp, loff_t *ppos)
2590 {
2591     return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
2592 }
2593
2594 /**
2595  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
2596  * @table: the sysctl table
2597  * @write: %TRUE if this is a write to the sysctl file
2598  * @buffer: the user buffer
2599  * @lenp: the size of the user buffer
2600  * @ppos: file position
2601  *
2602  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
2603  * values from/to the user buffer, treated as an ASCII string. The values
2604  * are treated as milliseconds, and converted to jiffies when they are stored.
2605  *
2606  * This routine will ensure the values are within the range specified by
2607  * table->extra1 (min) and table->extra2 (max).
2608  *
2609  * Returns 0 on success.
2610  */
2611 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
2612                                       void __user *buffer,
2613                                       size_t *lenp, loff_t *ppos)
2614 {
2615     return do_proc_doulongvec_minmax(table, write, buffer,
2616                                      lenp, ppos, HZ, 1000l);
2617 }
2618
2619
2620 static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,
2621                                          int *valp,
2622                                          int write, void *data)
2623 {
2624         if (write) {
2625                 if (*lvalp > LONG_MAX / HZ)
2626                         return 1;
2627                 *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
2628         } else {
2629                 int val = *valp;
2630                 unsigned long lval;
2631                 if (val < 0) {
2632                         *negp = true;
2633                         lval = (unsigned long)-val;
2634                 } else {
2635                         *negp = false;
2636                         lval = (unsigned long)val;
2637                 }
2638                 *lvalp = lval / HZ;
2639         }
2640         return 0;
2641 }
2642
2643 static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalp,
2644                                                 int *valp,
2645                                                 int write, void *data)
2646 {
2647         if (write) {
2648                 if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
2649                         return 1;
2650                 *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
2651         } else {
2652                 int val = *valp;
2653                 unsigned long lval;
2654                 if (val < 0) {
2655                         *negp = true;
2656                         lval = (unsigned long)-val;
2657                 } else {
2658                         *negp = false;
2659                         lval = (unsigned long)val;
2660                 }
2661                 *lvalp = jiffies_to_clock_t(lval);
2662         }
2663         return 0;
2664 }
2665
2666 static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,
2667                                             int *valp,
2668                                             int write, void *data)
2669 {
2670         if (write) {
2671                 *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
2672         } else {
2673                 int val = *valp;
2674                 unsigned long lval;
2675                 if (val < 0) {
2676                         *negp = true;
2677                         lval = (unsigned long)-val;
2678                 } else {
2679                         *negp = false;
2680                         lval = (unsigned long)val;
2681                 }
2682                 *lvalp = jiffies_to_msecs(lval);
2683         }
2684         return 0;
2685 }
2686
2687 /**
2688  * proc_dointvec_jiffies - read a vector of integers as seconds
2689  * @table: the sysctl table
2690  * @write: %TRUE if this is a write to the sysctl file
2691  * @buffer: the user buffer
2692  * @lenp: the size of the user buffer
2693  * @ppos: file position
2694  *
2695  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2696  * values from/to the user buffer, treated as an ASCII string. 
2697  * The values read are assumed to be in seconds, and are converted into
2698  * jiffies.
2699  *
2700  * Returns 0 on success.
2701  */
2702 int proc_dointvec_jiffies(struct ctl_table *table, int write,
2703                           void __user *buffer, size_t *lenp, loff_t *ppos)
2704 {
2705     return do_proc_dointvec(table,write,buffer,lenp,ppos,
2706                             do_proc_dointvec_jiffies_conv,NULL);
2707 }
2708
2709 /**
2710  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
2711  * @table: the sysctl table
2712  * @write: %TRUE if this is a write to the sysctl file
2713  * @buffer: the user buffer
2714  * @lenp: the size of the user buffer
2715  * @ppos: pointer to the file position
2716  *
2717  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2718  * values from/to the user buffer, treated as an ASCII string. 
2719  * The values read are assumed to be in 1/USER_HZ seconds, and 
2720  * are converted into jiffies.
2721  *
2722  * Returns 0 on success.
2723  */
2724 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
2725                                  void __user *buffer, size_t *lenp, loff_t *ppos)
2726 {
2727     return do_proc_dointvec(table,write,buffer,lenp,ppos,
2728                             do_proc_dointvec_userhz_jiffies_conv,NULL);
2729 }
2730
2731 /**
2732  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
2733  * @table: the sysctl table
2734  * @write: %TRUE if this is a write to the sysctl file
2735  * @buffer: the user buffer
2736  * @lenp: the size of the user buffer
2737  * @ppos: file position
2738  * @ppos: the current position in the file
2739  *
2740  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
2741  * values from/to the user buffer, treated as an ASCII string. 
2742  * The values read are assumed to be in 1/1000 seconds, and 
2743  * are converted into jiffies.
2744  *
2745  * Returns 0 on success.
2746  */
2747 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
2748                              void __user *buffer, size_t *lenp, loff_t *ppos)
2749 {
2750         return do_proc_dointvec(table, write, buffer, lenp, ppos,
2751                                 do_proc_dointvec_ms_jiffies_conv, NULL);
2752 }
2753
2754 static int proc_do_cad_pid(struct ctl_table *table, int write,
2755                            void __user *buffer, size_t *lenp, loff_t *ppos)
2756 {
2757         struct pid *new_pid;
2758         pid_t tmp;
2759         int r;
2760
2761         tmp = pid_vnr(cad_pid);
2762
2763         r = __do_proc_dointvec(&tmp, table, write, buffer,
2764                                lenp, ppos, NULL, NULL);
2765         if (r || !write)
2766                 return r;
2767
2768         new_pid = find_get_pid(tmp);
2769         if (!new_pid)
2770                 return -ESRCH;
2771
2772         put_pid(xchg(&cad_pid, new_pid));
2773         return 0;
2774 }
2775
2776 /**
2777  * proc_do_large_bitmap - read/write from/to a large bitmap
2778  * @table: the sysctl table
2779  * @write: %TRUE if this is a write to the sysctl file
2780  * @buffer: the user buffer
2781  * @lenp: the size of the user buffer
2782  * @ppos: file position
2783  *
2784  * The bitmap is stored at table->data and the bitmap length (in bits)
2785  * in table->maxlen.
2786  *
2787  * We use a range comma separated format (e.g. 1,3-4,10-10) so that
2788  * large bitmaps may be represented in a compact manner. Writing into
2789  * the file will clear the bitmap then update it with the given input.
2790  *
2791  * Returns 0 on success.
2792  */
2793 int proc_do_large_bitmap(struct ctl_table *table, int write,
2794                          void __user *buffer, size_t *lenp, loff_t *ppos)
2795 {
2796         int err = 0;
2797         bool first = 1;
2798         size_t left = *lenp;
2799         unsigned long bitmap_len = table->maxlen;
2800         unsigned long *bitmap = (unsigned long *) table->data;
2801         unsigned long *tmp_bitmap = NULL;
2802         char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
2803
2804         if (!bitmap_len || !left || (*ppos && !write)) {
2805                 *lenp = 0;
2806                 return 0;
2807         }
2808
2809         if (write) {
2810                 unsigned long page = 0;
2811                 char *kbuf;
2812
2813                 if (left > PAGE_SIZE - 1)
2814                         left = PAGE_SIZE - 1;
2815
2816                 page = __get_free_page(GFP_TEMPORARY);
2817                 kbuf = (char *) page;
2818                 if (!kbuf)
2819                         return -ENOMEM;
2820                 if (copy_from_user(kbuf, buffer, left)) {
2821                         free_page(page);
2822                         return -EFAULT;
2823                 }
2824                 kbuf[left] = 0;
2825
2826                 tmp_bitmap = kzalloc(BITS_TO_LONGS(bitmap_len) * sizeof(unsigned long),
2827                                      GFP_KERNEL);
2828                 if (!tmp_bitmap) {
2829                         free_page(page);
2830                         return -ENOMEM;
2831                 }
2832                 proc_skip_char(&kbuf, &left, '\n');
2833                 while (!err && left) {
2834                         unsigned long val_a, val_b;
2835                         bool neg;
2836
2837                         err = proc_get_long(&kbuf, &left, &val_a, &neg, tr_a,
2838                                              sizeof(tr_a), &c);
2839                         if (err)
2840                                 break;
2841                         if (val_a >= bitmap_len || neg) {
2842                                 err = -EINVAL;
2843                                 break;
2844                         }
2845
2846                         val_b = val_a;
2847                         if (left) {
2848                                 kbuf++;
2849                                 left--;
2850                         }
2851
2852                         if (c == '-') {
2853                                 err = proc_get_long(&kbuf, &left, &val_b,
2854                                                      &neg, tr_b, sizeof(tr_b),
2855                                                      &c);
2856                                 if (err)
2857                                         break;
2858                                 if (val_b >= bitmap_len || neg ||
2859                                     val_a > val_b) {
2860                                         err = -EINVAL;
2861                                         break;
2862                                 }
2863                                 if (left) {
2864                                         kbuf++;
2865                                         left--;
2866                                 }
2867                         }
2868
2869                         while (val_a <= val_b)
2870                                 set_bit(val_a++, tmp_bitmap);
2871
2872                         first = 0;
2873                         proc_skip_char(&kbuf, &left, '\n');
2874                 }
2875                 free_page(page);
2876         } else {
2877                 unsigned long bit_a, bit_b = 0;
2878
2879                 while (left) {
2880                         bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
2881                         if (bit_a >= bitmap_len)
2882                                 break;
2883                         bit_b = find_next_zero_bit(bitmap, bitmap_len,
2884                                                    bit_a + 1) - 1;
2885
2886                         if (!first) {
2887                                 err = proc_put_char(&buffer, &left, ',');
2888                                 if (err)
2889                                         break;
2890                         }
2891                         err = proc_put_long(&buffer, &left, bit_a, false);
2892                         if (err)
2893                                 break;
2894                         if (bit_a != bit_b) {
2895                                 err = proc_put_char(&buffer, &left, '-');
2896                                 if (err)
2897                                         break;
2898                                 err = proc_put_long(&buffer, &left, bit_b, false);
2899                                 if (err)
2900                                         break;
2901                         }
2902
2903                         first = 0; bit_b++;
2904                 }
2905                 if (!err)
2906                         err = proc_put_char(&buffer, &left, '\n');
2907         }
2908
2909         if (!err) {
2910                 if (write) {
2911                         if (*ppos)
2912                                 bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
2913                         else
2914                                 memcpy(bitmap, tmp_bitmap,
2915                                         BITS_TO_LONGS(bitmap_len) * sizeof(unsigned long));
2916                 }
2917                 kfree(tmp_bitmap);
2918                 *lenp -= left;
2919                 *ppos += *lenp;
2920                 return 0;
2921         } else {
2922                 kfree(tmp_bitmap);
2923                 return err;
2924         }
2925 }
2926
2927 #else /* CONFIG_PROC_SYSCTL */
2928
2929 int proc_dostring(struct ctl_table *table, int write,
2930                   void __user *buffer, size_t *lenp, loff_t *ppos)
2931 {
2932         return -ENOSYS;
2933 }
2934
2935 int proc_dointvec(struct ctl_table *table, int write,
2936                   void __user *buffer, size_t *lenp, loff_t *ppos)
2937 {
2938         return -ENOSYS;
2939 }
2940
2941 int proc_dointvec_minmax(struct ctl_table *table, int write,
2942                     void __user *buffer, size_t *lenp, loff_t *ppos)
2943 {
2944         return -ENOSYS;
2945 }
2946
2947 int proc_dointvec_jiffies(struct ctl_table *table, int write,
2948                     void __user *buffer, size_t *lenp, loff_t *ppos)
2949 {
2950         return -ENOSYS;
2951 }
2952
2953 int proc_dointvec_userhz_jiffies(struct ctl_table *table, int write,
2954                     void __user *buffer, size_t *lenp, loff_t *ppos)
2955 {
2956         return -ENOSYS;
2957 }
2958
2959 int proc_dointvec_ms_jiffies(struct ctl_table *table, int write,
2960                              void __user *buffer, size_t *lenp, loff_t *ppos)
2961 {
2962         return -ENOSYS;
2963 }
2964
2965 int proc_doulongvec_minmax(struct ctl_table *table, int write,
2966                     void __user *buffer, size_t *lenp, loff_t *ppos)
2967 {
2968         return -ENOSYS;
2969 }
2970
2971 int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
2972                                       void __user *buffer,
2973                                       size_t *lenp, loff_t *ppos)
2974 {
2975     return -ENOSYS;
2976 }
2977
2978
2979 #endif /* CONFIG_PROC_SYSCTL */
2980
2981 /*
2982  * No sense putting this after each symbol definition, twice,
2983  * exception granted :-)
2984  */
2985 EXPORT_SYMBOL(proc_dointvec);
2986 EXPORT_SYMBOL(proc_dointvec_jiffies);
2987 EXPORT_SYMBOL(proc_dointvec_minmax);
2988 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
2989 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2990 EXPORT_SYMBOL(proc_dostring);
2991 EXPORT_SYMBOL(proc_doulongvec_minmax);
2992 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2993 EXPORT_SYMBOL(register_sysctl_table);
2994 EXPORT_SYMBOL(register_sysctl_paths);
2995 EXPORT_SYMBOL(unregister_sysctl_table);