Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[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 /*
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/autoconf.h>
39 #include <linux/sysctl.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/sysctl.h>
43 #include <linux/version.h>
44 #include <linux/proc_fs.h>
45 #include <linux/slab.h>
46 #include <linux/stat.h>
47 #include <linux/ctype.h>
48 #include <asm/bitops.h>
49 #include <asm/uaccess.h>
50 #include <linux/utsname.h>
51
52 #define DEBUG_SUBSYSTEM S_CLASS
53
54 #include <obd_support.h>
55 #include <lprocfs_status.h>
56
57 cfs_sysctl_table_header_t *obd_table_header = NULL;
58
59 #ifndef HAVE_SYSCTL_UNNUMBERED
60
61 #define OBD_SYSCTL 300
62
63 enum {
64         OBD_FAIL_LOC = 1,       /* control test failures instrumentation */
65         OBD_FAIL_VAL,           /* userdata for fail loc */
66         OBD_TIMEOUT,            /* RPC timeout before recovery/intr */
67         OBD_DUMP_ON_TIMEOUT,    /* dump kernel debug log upon eviction */
68         OBD_MEMUSED,            /* bytes currently OBD_ALLOCated */
69         OBD_PAGESUSED,          /* pages currently OBD_PAGE_ALLOCated */
70         OBD_MAXMEMUSED,         /* maximum bytes OBD_ALLOCated concurrently */
71         OBD_MAXPAGESUSED,       /* maximum pages OBD_PAGE_ALLOCated concurrently */
72         OBD_SYNCFILTER,         /* XXX temporary, as we play with sync osts.. */
73         OBD_LDLM_TIMEOUT,       /* LDLM timeout for ASTs before client eviction */
74         OBD_DUMP_ON_EVICTION,   /* dump kernel debug log upon eviction */
75         OBD_DEBUG_PEER_ON_TIMEOUT, /* dump peer debug when RPC times out */
76         OBD_ALLOC_FAIL_RATE,    /* memory allocation random failure rate */
77         OBD_MAX_DIRTY_PAGES,    /* maximum dirty pages */
78         OBD_AT_MIN,             /* Adaptive timeouts params */
79         OBD_AT_MAX,
80         OBD_AT_EXTRA,
81         OBD_AT_EARLY_MARGIN,
82         OBD_AT_HISTORY,
83 };
84
85 #else
86
87 #define OBD_SYSCTL              CTL_UNNUMBERED
88
89 #define OBD_FAIL_LOC            CTL_UNNUMBERED
90 #define OBD_FAIL_VAL            CTL_UNNUMBERED
91 #define OBD_TIMEOUT             CTL_UNNUMBERED
92 #define OBD_DUMP_ON_TIMEOUT     CTL_UNNUMBERED
93 #define OBD_MEMUSED             CTL_UNNUMBERED
94 #define OBD_PAGESUSED           CTL_UNNUMBERED
95 #define OBD_MAXMEMUSED          CTL_UNNUMBERED
96 #define OBD_MAXPAGESUSED        CTL_UNNUMBERED
97 #define OBD_SYNCFILTER          CTL_UNNUMBERED
98 #define OBD_LDLM_TIMEOUT        CTL_UNNUMBERED
99 #define OBD_DUMP_ON_EVICTION    CTL_UNNUMBERED
100 #define OBD_DEBUG_PEER_ON_TIMEOUT CTL_UNNUMBERED
101 #define OBD_ALLOC_FAIL_RATE     CTL_UNNUMBERED
102 #define OBD_MAX_DIRTY_PAGES     CTL_UNNUMBERED
103 #define OBD_AT_MIN              CTL_UNNUMBERED
104 #define OBD_AT_MAX              CTL_UNNUMBERED
105 #define OBD_AT_EXTRA            CTL_UNNUMBERED
106 #define OBD_AT_EARLY_MARGIN     CTL_UNNUMBERED
107 #define OBD_AT_HISTORY          CTL_UNNUMBERED
108
109 #endif
110
111
112 int LL_PROC_PROTO(proc_fail_loc)
113 {
114         int rc;
115         long old_fail_loc = obd_fail_loc;
116
117         rc = ll_proc_dolongvec(table, write, filp, buffer, lenp, ppos);
118         if (old_fail_loc != obd_fail_loc)
119                 cfs_waitq_signal(&obd_race_waitq);
120         return rc;
121 }
122
123 int LL_PROC_PROTO(proc_set_timeout)
124 {
125         int rc;
126
127         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
128         if (ldlm_timeout >= obd_timeout)
129                 ldlm_timeout = max(obd_timeout / 3, 1U);
130         return rc;
131 }
132
133 int LL_PROC_PROTO(proc_memory_alloc)
134 {
135         char buf[22];
136         int len;
137         DECLARE_LL_PROC_PPOS_DECL;
138
139         if (!*lenp || (*ppos && !write)) {
140                 *lenp = 0;
141                 return 0;
142         }
143         if (write)
144                 return -EINVAL;
145
146         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
147         if (len > *lenp)
148                 len = *lenp;
149         buf[len] = '\0';
150         if (cfs_copy_to_user(buffer, buf, len))
151                 return -EFAULT;
152         *lenp = len;
153         *ppos += *lenp;
154         return 0;
155 }
156
157 int LL_PROC_PROTO(proc_pages_alloc)
158 {
159         char buf[22];
160         int len;
161         DECLARE_LL_PROC_PPOS_DECL;
162
163         if (!*lenp || (*ppos && !write)) {
164                 *lenp = 0;
165                 return 0;
166         }
167         if (write)
168                 return -EINVAL;
169
170         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
171         if (len > *lenp)
172                 len = *lenp;
173         buf[len] = '\0';
174         if (cfs_copy_to_user(buffer, buf, len))
175                 return -EFAULT;
176         *lenp = len;
177         *ppos += *lenp;
178         return 0;
179 }
180
181 int LL_PROC_PROTO(proc_mem_max)
182 {
183         char buf[22];
184         int len;
185         DECLARE_LL_PROC_PPOS_DECL;
186
187         if (!*lenp || (*ppos && !write)) {
188                 *lenp = 0;
189                 return 0;
190         }
191         if (write)
192                 return -EINVAL;
193
194         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
195         if (len > *lenp)
196                 len = *lenp;
197         buf[len] = '\0';
198         if (cfs_copy_to_user(buffer, buf, len))
199                 return -EFAULT;
200         *lenp = len;
201         *ppos += *lenp;
202         return 0;
203 }
204
205 int LL_PROC_PROTO(proc_pages_max)
206 {
207         char buf[22];
208         int len;
209         DECLARE_LL_PROC_PPOS_DECL;
210
211         if (!*lenp || (*ppos && !write)) {
212                 *lenp = 0;
213                 return 0;
214         }
215         if (write)
216                 return -EINVAL;
217
218         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
219         if (len > *lenp)
220                 len = *lenp;
221         buf[len] = '\0';
222         if (cfs_copy_to_user(buffer, buf, len))
223                 return -EFAULT;
224         *lenp = len;
225         *ppos += *lenp;
226         return 0;
227 }
228
229 int LL_PROC_PROTO(proc_max_dirty_pages_in_mb)
230 {
231         int rc = 0;
232         DECLARE_LL_PROC_PPOS_DECL;
233
234         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
235                 *lenp = 0;
236                 return 0;
237         }
238         if (write) {
239                 rc = lprocfs_write_frac_helper(buffer, *lenp,
240                                                (unsigned int*)table->data,
241                                                1 << (20 - CFS_PAGE_SHIFT));
242                 /* Don't allow them to let dirty pages exceed 90% of system
243                  * memory and set a hard minimum of 4MB. */
244                 if (obd_max_dirty_pages > ((cfs_num_physpages / 10) * 9)) {
245                         CERROR("Refusing to set max dirty pages to %u, which "
246                                "is more than 90%% of available RAM; setting "
247                                "to %lu\n", obd_max_dirty_pages,
248                                ((cfs_num_physpages / 10) * 9));
249                         obd_max_dirty_pages = ((cfs_num_physpages / 10) * 9);
250                 } else if (obd_max_dirty_pages < 4 << (20 - CFS_PAGE_SHIFT)) {
251                         obd_max_dirty_pages = 4 << (20 - CFS_PAGE_SHIFT);
252                 }
253         } else {
254                 char buf[21];
255                 int len;
256
257                 len = lprocfs_read_frac_helper(buf, sizeof(buf),
258                                                *(unsigned int*)table->data,
259                                                1 << (20 - CFS_PAGE_SHIFT));
260                 if (len > *lenp)
261                         len = *lenp;
262                 buf[len] = '\0';
263                 if (cfs_copy_to_user(buffer, buf, len))
264                         return -EFAULT;
265                 *lenp = len;
266         }
267         *ppos += *lenp;
268         return rc;
269 }
270
271 #ifdef RANDOM_FAIL_ALLOC
272 int LL_PROC_PROTO(proc_alloc_fail_rate)
273 {
274         int rc          = 0;
275         DECLARE_LL_PROC_PPOS_DECL;
276
277         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
278                 *lenp = 0;
279                 return 0;
280         }
281         if (write) {
282                 rc = lprocfs_write_frac_helper(buffer, *lenp,
283                                                (unsigned int*)table->data,
284                                                OBD_ALLOC_FAIL_MULT);
285         } else {
286                 char buf[21];
287                 int  len;
288
289                 len = lprocfs_read_frac_helper(buf, 21,
290                                                *(unsigned int*)table->data,
291                                                OBD_ALLOC_FAIL_MULT);
292                 if (len > *lenp)
293                         len = *lenp;
294                 buf[len] = '\0';
295                 if (cfs_copy_to_user(buffer, buf, len))
296                         return -EFAULT;
297                 *lenp = len;
298         }
299         *ppos += *lenp;
300         return rc;
301 }
302 #endif
303
304 int LL_PROC_PROTO(proc_at_min)
305 {
306         return ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
307 }
308 int LL_PROC_PROTO(proc_at_max)
309 {
310         return ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
311 }
312 int LL_PROC_PROTO(proc_at_extra)
313 {
314         return ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
315 }
316 int LL_PROC_PROTO(proc_at_early_margin)
317 {
318         return ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
319 }
320 int LL_PROC_PROTO(proc_at_history)
321 {
322         return ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
323 }
324
325 static cfs_sysctl_table_t obd_table[] = {
326         {
327                 .ctl_name = OBD_FAIL_LOC,
328                 .procname = "fail_loc",
329                 .data     = &obd_fail_loc,
330                 .maxlen   = sizeof(obd_fail_loc),
331                 .mode     = 0644,
332                 .proc_handler = &proc_fail_loc
333         },
334         {
335                 .ctl_name = OBD_FAIL_VAL,
336                 .procname = "fail_val",
337                 .data     = &obd_fail_val,
338                 .maxlen   = sizeof(int),
339                 .mode     = 0644,
340                 .proc_handler = &proc_dointvec
341         },
342         {
343                 .ctl_name = OBD_TIMEOUT,
344                 .procname = "timeout",
345                 .data     = &obd_timeout,
346                 .maxlen   = sizeof(int),
347                 .mode     = 0644,
348                 .proc_handler = &proc_set_timeout
349         },
350         {
351                 .ctl_name = OBD_DEBUG_PEER_ON_TIMEOUT,
352                 .procname = "debug_peer_on_timeout",
353                 .data     = &obd_debug_peer_on_timeout,
354                 .maxlen   = sizeof(int),
355                 .mode     = 0644,
356                 .proc_handler = &proc_dointvec
357         },
358         {
359                 .ctl_name = OBD_DUMP_ON_TIMEOUT,
360                 .procname = "dump_on_timeout",
361                 .data     = &obd_dump_on_timeout,
362                 .maxlen   = sizeof(int),
363                 .mode     = 0644,
364                 .proc_handler = &proc_dointvec
365         },
366         {
367                 .ctl_name = OBD_DUMP_ON_EVICTION,
368                 .procname = "dump_on_eviction",
369                 .data     = &obd_dump_on_eviction,
370                 .maxlen   = sizeof(int),
371                 .mode     = 0644,
372                 .proc_handler = &proc_dointvec
373         },
374         {
375                 .ctl_name = OBD_MEMUSED,
376                 .procname = "memused",
377                 .data     = NULL,
378                 .maxlen   = 0,
379                 .mode     = 0444,
380                 .proc_handler = &proc_memory_alloc
381         },
382         {
383                 .ctl_name = OBD_PAGESUSED,
384                 .procname = "pagesused",
385                 .data     = NULL,
386                 .maxlen   = 0,
387                 .mode     = 0444,
388                 .proc_handler = &proc_pages_alloc
389         },
390         {
391                 .ctl_name = OBD_MAXMEMUSED,
392                 .procname = "memused_max",
393                 .data     = NULL,
394                 .maxlen   = 0,
395                 .mode     = 0444,
396                 .proc_handler = &proc_mem_max
397         },
398         {
399                 .ctl_name = OBD_MAXPAGESUSED,
400                 .procname = "pagesused_max",
401                 .data     = NULL,
402                 .maxlen   = 0,
403                 .mode     = 0444,
404                 .proc_handler = &proc_pages_max
405         },
406         {
407                 .ctl_name = OBD_LDLM_TIMEOUT,
408                 .procname = "ldlm_timeout",
409                 .data     = &ldlm_timeout,
410                 .maxlen   = sizeof(int),
411                 .mode     = 0644,
412                 .proc_handler = &proc_set_timeout
413         },
414 #ifdef RANDOM_FAIL_ALLOC
415         {
416                 .ctl_name = OBD_ALLOC_FAIL_RATE,
417                 .procname = "alloc_fail_rate",
418                 .data     = &obd_alloc_fail_rate,
419                 .maxlen   = sizeof(int),
420                 .mode     = 0644,
421                 .proc_handler = &proc_alloc_fail_rate
422         },
423 #endif
424         {
425                 .ctl_name = OBD_MAX_DIRTY_PAGES,
426                 .procname = "max_dirty_mb",
427                 .data     = &obd_max_dirty_pages,
428                 .maxlen   = sizeof(int),
429                 .mode     = 0644,
430                 .proc_handler = &proc_max_dirty_pages_in_mb
431         },
432         {
433                 .ctl_name = OBD_AT_MIN,
434                 .procname = "at_min",
435                 .data     = &at_min,
436                 .maxlen   = sizeof(int),
437                 .mode     = 0644,
438                 .proc_handler = &proc_at_min
439         },
440         {
441                 .ctl_name = OBD_AT_MAX,
442                 .procname = "at_max",
443                 .data     = &at_max,
444                 .maxlen   = sizeof(int),
445                 .mode     = 0644,
446                 .proc_handler = &proc_at_max
447         },
448         {
449                 .ctl_name = OBD_AT_EXTRA,
450                 .procname = "at_extra",
451                 .data     = &at_extra,
452                 .maxlen   = sizeof(int),
453                 .mode     = 0644,
454                 .proc_handler = &proc_at_extra
455         },
456         {
457                 .ctl_name = OBD_AT_EARLY_MARGIN,
458                 .procname = "at_early_margin",
459                 .data     = &at_early_margin,
460                 .maxlen   = sizeof(int),
461                 .mode     = 0644,
462                 .proc_handler = &proc_at_early_margin
463         },
464         {
465                 .ctl_name = OBD_AT_HISTORY,
466                 .procname = "at_history",
467                 .data     = &at_history,
468                 .maxlen   = sizeof(int),
469                 .mode     = 0644,
470                 .proc_handler = &proc_at_history
471         },
472         { 0 }
473 };
474
475 static cfs_sysctl_table_t parent_table[] = {
476         {
477                 .ctl_name = OBD_SYSCTL,
478                 .procname = "lustre",
479                 .data     = NULL,
480                 .maxlen   = 0,
481                 .mode     = 0555,
482                 .child    = obd_table
483         },
484         {0}
485 };
486
487 void obd_sysctl_init (void)
488 {
489 #ifdef CONFIG_SYSCTL
490         if ( !obd_table_header )
491                 obd_table_header = cfs_register_sysctl_table(parent_table, 0);
492 #endif
493 }
494
495 void obd_sysctl_clean (void)
496 {
497 #ifdef CONFIG_SYSCTL
498         if ( obd_table_header )
499                 cfs_unregister_sysctl_table(obd_table_header);
500         obd_table_header = NULL;
501 #endif
502 }