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