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