Whamcloud - gitweb
LU-2800 autoconf: remove LN_5ARGS_SYSCTL_PROC_HANDLER test
[fs/lustre-release.git] / lustre / obdclass / linux / linux-sysctl.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/module.h>
38 #include <linux/sysctl.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/sysctl.h>
42 #include <linux/version.h>
43 #include <linux/proc_fs.h>
44 #include <linux/slab.h>
45 #include <linux/stat.h>
46 #include <linux/ctype.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
49 #include <linux/utsname.h>
50
51 #define DEBUG_SUBSYSTEM S_CLASS
52
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55
56 #ifdef CONFIG_SYSCTL
57 struct ctl_table_header *obd_table_header = NULL;
58 #endif
59
60 #ifndef HAVE_SYSCTL_UNNUMBERED
61
62 #define OBD_SYSCTL 300
63
64 enum {
65         OBD_TIMEOUT = 3,        /* RPC timeout before recovery/intr */
66         OBD_DUMP_ON_TIMEOUT,    /* dump kernel debug log upon eviction */
67         OBD_MEMUSED,            /* bytes currently OBD_ALLOCated */
68         OBD_PAGESUSED,          /* pages currently OBD_PAGE_ALLOCated */
69         OBD_MAXMEMUSED,         /* maximum bytes OBD_ALLOCated concurrently */
70         OBD_MAXPAGESUSED,       /* maximum pages OBD_PAGE_ALLOCated concurrently */
71         OBD_SYNCFILTER,         /* XXX temporary, as we play with sync osts.. */
72         OBD_LDLM_TIMEOUT,       /* LDLM timeout for ASTs before client eviction */
73         OBD_DUMP_ON_EVICTION,   /* dump kernel debug log upon eviction */
74         OBD_DEBUG_PEER_ON_TIMEOUT, /* dump peer debug when RPC times out */
75         OBD_ALLOC_FAIL_RATE,    /* memory allocation random failure rate */
76         OBD_MAX_DIRTY_PAGES,    /* maximum dirty pages */
77         OBD_AT_MIN,             /* Adaptive timeouts params */
78         OBD_AT_MAX,
79         OBD_AT_EXTRA,
80         OBD_AT_EARLY_MARGIN,
81         OBD_AT_HISTORY,
82 };
83
84 #else
85
86 #define OBD_SYSCTL              CTL_UNNUMBERED
87
88 #define OBD_FAIL_LOC            CTL_UNNUMBERED
89 #define OBD_FAIL_VAL            CTL_UNNUMBERED
90 #define OBD_TIMEOUT             CTL_UNNUMBERED
91 #define OBD_DUMP_ON_TIMEOUT     CTL_UNNUMBERED
92 #define OBD_MEMUSED             CTL_UNNUMBERED
93 #define OBD_PAGESUSED           CTL_UNNUMBERED
94 #define OBD_MAXMEMUSED          CTL_UNNUMBERED
95 #define OBD_MAXPAGESUSED        CTL_UNNUMBERED
96 #define OBD_SYNCFILTER          CTL_UNNUMBERED
97 #define OBD_LDLM_TIMEOUT        CTL_UNNUMBERED
98 #define OBD_DUMP_ON_EVICTION    CTL_UNNUMBERED
99 #define OBD_DEBUG_PEER_ON_TIMEOUT CTL_UNNUMBERED
100 #define OBD_ALLOC_FAIL_RATE     CTL_UNNUMBERED
101 #define OBD_MAX_DIRTY_PAGES     CTL_UNNUMBERED
102 #define OBD_AT_MIN              CTL_UNNUMBERED
103 #define OBD_AT_MAX              CTL_UNNUMBERED
104 #define OBD_AT_EXTRA            CTL_UNNUMBERED
105 #define OBD_AT_EARLY_MARGIN     CTL_UNNUMBERED
106 #define OBD_AT_HISTORY          CTL_UNNUMBERED
107
108 #endif
109
110 int LL_PROC_PROTO(proc_set_timeout)
111 {
112         int rc;
113
114         rc = proc_dointvec(table, write, buffer, lenp, ppos);
115         if (ldlm_timeout >= obd_timeout)
116                 ldlm_timeout = max(obd_timeout / 3, 1U);
117         return rc;
118 }
119
120 int LL_PROC_PROTO(proc_memory_alloc)
121 {
122         char buf[22];
123         int len;
124
125         if (!*lenp || (*ppos && !write)) {
126                 *lenp = 0;
127                 return 0;
128         }
129         if (write)
130                 return -EINVAL;
131
132         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
133         if (len > *lenp)
134                 len = *lenp;
135         buf[len] = '\0';
136         if (copy_to_user(buffer, buf, len))
137                 return -EFAULT;
138         *lenp = len;
139         *ppos += *lenp;
140         return 0;
141 }
142
143 int LL_PROC_PROTO(proc_pages_alloc)
144 {
145         char buf[22];
146         int len;
147
148         if (!*lenp || (*ppos && !write)) {
149                 *lenp = 0;
150                 return 0;
151         }
152         if (write)
153                 return -EINVAL;
154
155         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
156         if (len > *lenp)
157                 len = *lenp;
158         buf[len] = '\0';
159         if (copy_to_user(buffer, buf, len))
160                 return -EFAULT;
161         *lenp = len;
162         *ppos += *lenp;
163         return 0;
164 }
165
166 int LL_PROC_PROTO(proc_mem_max)
167 {
168         char buf[22];
169         int len;
170
171         if (!*lenp || (*ppos && !write)) {
172                 *lenp = 0;
173                 return 0;
174         }
175         if (write)
176                 return -EINVAL;
177
178         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
179         if (len > *lenp)
180                 len = *lenp;
181         buf[len] = '\0';
182         if (copy_to_user(buffer, buf, len))
183                 return -EFAULT;
184         *lenp = len;
185         *ppos += *lenp;
186         return 0;
187 }
188
189 int LL_PROC_PROTO(proc_pages_max)
190 {
191         char buf[22];
192         int len;
193
194         if (!*lenp || (*ppos && !write)) {
195                 *lenp = 0;
196                 return 0;
197         }
198         if (write)
199                 return -EINVAL;
200
201         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
202         if (len > *lenp)
203                 len = *lenp;
204         buf[len] = '\0';
205         if (copy_to_user(buffer, buf, len))
206                 return -EFAULT;
207         *lenp = len;
208         *ppos += *lenp;
209         return 0;
210 }
211
212 int LL_PROC_PROTO(proc_max_dirty_pages_in_mb)
213 {
214         int rc = 0;
215
216         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
217                 *lenp = 0;
218                 return 0;
219         }
220         if (write) {
221                 rc = lprocfs_write_frac_helper(buffer, *lenp,
222                                                (unsigned int *)table->data,
223                                                1 << (20 - PAGE_CACHE_SHIFT));
224                 /* Don't allow them to let dirty pages exceed 90% of system
225                  * memory and set a hard minimum of 4MB. */
226                 if (obd_max_dirty_pages > ((totalram_pages / 10) * 9)) {
227                         CERROR("Refusing to set max dirty pages to %u, which "
228                                "is more than 90%% of available RAM; setting "
229                                "to %lu\n", obd_max_dirty_pages,
230                                ((totalram_pages / 10) * 9));
231                         obd_max_dirty_pages = ((totalram_pages / 10) * 9);
232                 } else if (obd_max_dirty_pages < 4 << (20 - PAGE_CACHE_SHIFT)) {
233                         obd_max_dirty_pages = 4 << (20 - PAGE_CACHE_SHIFT);
234                 }
235         } else {
236                 char buf[21];
237                 int len;
238
239                 len = lprocfs_read_frac_helper(buf, sizeof(buf),
240                                                *(unsigned int *)table->data,
241                                                1 << (20 - PAGE_CACHE_SHIFT));
242                 if (len > *lenp)
243                         len = *lenp;
244                 buf[len] = '\0';
245                 if (copy_to_user(buffer, buf, len))
246                         return -EFAULT;
247                 *lenp = len;
248         }
249         *ppos += *lenp;
250         return rc;
251 }
252
253 #ifdef RANDOM_FAIL_ALLOC
254 int proc_alloc_fail_rate(struct ctl_table *table, int write,
255                          void __user *buffer, size_t *lenp, loff_t *ppos)
256 {
257         int rc          = 0;
258
259         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
260                 *lenp = 0;
261                 return 0;
262         }
263         if (write) {
264                 rc = lprocfs_write_frac_helper(buffer, *lenp,
265                                                (unsigned int*)table->data,
266                                                OBD_ALLOC_FAIL_MULT);
267         } else {
268                 char buf[21];
269                 int  len;
270
271                 len = lprocfs_read_frac_helper(buf, 21,
272                                                *(unsigned int*)table->data,
273                                                OBD_ALLOC_FAIL_MULT);
274                 if (len > *lenp)
275                         len = *lenp;
276                 buf[len] = '\0';
277                 if (copy_to_user(buffer, buf, len))
278                         return -EFAULT;
279                 *lenp = len;
280         }
281         *ppos += *lenp;
282         return rc;
283 }
284 #endif
285
286 int LL_PROC_PROTO(proc_at_min)
287 {
288         return proc_dointvec(table, write, buffer, lenp, ppos);
289 }
290 int LL_PROC_PROTO(proc_at_max)
291 {
292         return proc_dointvec(table, write, buffer, lenp, ppos);
293 }
294 int LL_PROC_PROTO(proc_at_extra)
295 {
296         return proc_dointvec(table, write, buffer, lenp, ppos);
297 }
298 int LL_PROC_PROTO(proc_at_early_margin)
299 {
300         return proc_dointvec(table, write, buffer, lenp, ppos);
301 }
302 int LL_PROC_PROTO(proc_at_history)
303 {
304         return proc_dointvec(table, write, buffer, lenp, ppos);
305 }
306
307 #ifdef CONFIG_SYSCTL
308 static struct ctl_table obd_table[] = {
309         {
310                 INIT_CTL_NAME(OBD_TIMEOUT)
311                 .procname = "timeout",
312                 .data     = &obd_timeout,
313                 .maxlen   = sizeof(int),
314                 .mode     = 0644,
315                 .proc_handler = &proc_set_timeout
316         },
317         {
318                 INIT_CTL_NAME(OBD_DEBUG_PEER_ON_TIMEOUT)
319                 .procname = "debug_peer_on_timeout",
320                 .data     = &obd_debug_peer_on_timeout,
321                 .maxlen   = sizeof(int),
322                 .mode     = 0644,
323                 .proc_handler = &proc_dointvec
324         },
325         {
326                 INIT_CTL_NAME(OBD_DUMP_ON_TIMEOUT)
327                 .procname = "dump_on_timeout",
328                 .data     = &obd_dump_on_timeout,
329                 .maxlen   = sizeof(int),
330                 .mode     = 0644,
331                 .proc_handler = &proc_dointvec
332         },
333         {
334                 INIT_CTL_NAME(OBD_DUMP_ON_EVICTION)
335                 .procname = "dump_on_eviction",
336                 .data     = &obd_dump_on_eviction,
337                 .maxlen   = sizeof(int),
338                 .mode     = 0644,
339                 .proc_handler = &proc_dointvec
340         },
341         {
342                 INIT_CTL_NAME(OBD_MEMUSED)
343                 .procname = "memused",
344                 .data     = NULL,
345                 .maxlen   = 0,
346                 .mode     = 0444,
347                 .proc_handler = &proc_memory_alloc
348         },
349         {
350                 INIT_CTL_NAME(OBD_PAGESUSED)
351                 .procname = "pagesused",
352                 .data     = NULL,
353                 .maxlen   = 0,
354                 .mode     = 0444,
355                 .proc_handler = &proc_pages_alloc
356         },
357         {
358                 INIT_CTL_NAME(OBD_MAXMEMUSED)
359                 .procname = "memused_max",
360                 .data     = NULL,
361                 .maxlen   = 0,
362                 .mode     = 0444,
363                 .proc_handler = &proc_mem_max
364         },
365         {
366                 INIT_CTL_NAME(OBD_MAXPAGESUSED)
367                 .procname = "pagesused_max",
368                 .data     = NULL,
369                 .maxlen   = 0,
370                 .mode     = 0444,
371                 .proc_handler = &proc_pages_max
372         },
373         {
374                 INIT_CTL_NAME(OBD_LDLM_TIMEOUT)
375                 .procname = "ldlm_timeout",
376                 .data     = &ldlm_timeout,
377                 .maxlen   = sizeof(int),
378                 .mode     = 0644,
379                 .proc_handler = &proc_set_timeout
380         },
381 #ifdef RANDOM_FAIL_ALLOC
382         {
383                 INIT_CTL_NAME(OBD_ALLOC_FAIL_RATE)
384                 .procname = "alloc_fail_rate",
385                 .data     = &obd_alloc_fail_rate,
386                 .maxlen   = sizeof(int),
387                 .mode     = 0644,
388                 .proc_handler = &proc_alloc_fail_rate
389         },
390 #endif
391         {
392                 INIT_CTL_NAME(OBD_MAX_DIRTY_PAGES)
393                 .procname = "max_dirty_mb",
394                 .data     = &obd_max_dirty_pages,
395                 .maxlen   = sizeof(int),
396                 .mode     = 0644,
397                 .proc_handler = &proc_max_dirty_pages_in_mb
398         },
399         {
400                 INIT_CTL_NAME(OBD_AT_MIN)
401                 .procname = "at_min",
402                 .data     = &at_min,
403                 .maxlen   = sizeof(int),
404                 .mode     = 0644,
405                 .proc_handler = &proc_at_min
406         },
407         {
408                 INIT_CTL_NAME(OBD_AT_MAX)
409                 .procname = "at_max",
410                 .data     = &at_max,
411                 .maxlen   = sizeof(int),
412                 .mode     = 0644,
413                 .proc_handler = &proc_at_max
414         },
415         {
416                 INIT_CTL_NAME(OBD_AT_EXTRA)
417                 .procname = "at_extra",
418                 .data     = &at_extra,
419                 .maxlen   = sizeof(int),
420                 .mode     = 0644,
421                 .proc_handler = &proc_at_extra
422         },
423         {
424                 INIT_CTL_NAME(OBD_AT_EARLY_MARGIN)
425                 .procname = "at_early_margin",
426                 .data     = &at_early_margin,
427                 .maxlen   = sizeof(int),
428                 .mode     = 0644,
429                 .proc_handler = &proc_at_early_margin
430         },
431         {
432                 INIT_CTL_NAME(OBD_AT_HISTORY)
433                 .procname = "at_history",
434                 .data     = &at_history,
435                 .maxlen   = sizeof(int),
436                 .mode     = 0644,
437                 .proc_handler = &proc_at_history
438         },
439         {       INIT_CTL_NAME(0)    }
440 };
441
442 static struct ctl_table parent_table[] = {
443         {
444                 INIT_CTL_NAME(OBD_SYSCTL)
445                 .procname = "lustre",
446                 .data     = NULL,
447                 .maxlen   = 0,
448                 .mode     = 0555,
449                 .child    = obd_table
450         },
451         {       INIT_CTL_NAME(0)   }
452 };
453 #endif
454
455 void obd_sysctl_init (void)
456 {
457 #ifdef CONFIG_SYSCTL
458         if ( !obd_table_header )
459                 obd_table_header = register_sysctl_table(parent_table);
460 #endif
461 }
462
463 void obd_sysctl_clean (void)
464 {
465 #ifdef CONFIG_SYSCTL
466         if ( obd_table_header )
467                 unregister_sysctl_table(obd_table_header);
468         obd_table_header = NULL;
469 #endif
470 }