Whamcloud - gitweb
b=16098
[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 [sun.com URL with a
20  * copy of GPLv2].
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 };
76
77 int LL_PROC_PROTO(proc_fail_loc)
78 {
79         int rc;
80         long old_fail_loc = obd_fail_loc;
81
82         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
83         if (old_fail_loc != obd_fail_loc)
84                 wake_up(&obd_race_waitq);
85         return rc;
86 }
87
88 int LL_PROC_PROTO(proc_set_timeout)
89 {
90         int rc;
91
92         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
93         if (ldlm_timeout >= obd_timeout)
94                 ldlm_timeout = max(obd_timeout / 3, 1U);
95         return rc;
96 }
97
98 int LL_PROC_PROTO(proc_memory_alloc)
99 {
100         char buf[22];
101         int len;
102         DECLARE_LL_PROC_PPOS_DECL;
103
104         if (!*lenp || (*ppos && !write)) {
105                 *lenp = 0;
106                 return 0;
107         }
108         if (write) 
109                 return -EINVAL;
110         
111         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
112         if (len > *lenp)
113                 len = *lenp;
114         buf[len] = '\0';
115         if (copy_to_user(buffer, buf, len))
116                 return -EFAULT;
117         *lenp = len;
118         *ppos += *lenp;
119         return 0;
120 }
121
122 int LL_PROC_PROTO(proc_pages_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_pages_sum());
136         if (len > *lenp)
137                 len = *lenp;
138         buf[len] = '\0';
139         if (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_mem_max)
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_memory_max());
160         if (len > *lenp)
161                 len = *lenp;
162         buf[len] = '\0';
163         if (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_pages_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_pages_max());
184         if (len > *lenp)
185                 len = *lenp;
186         buf[len] = '\0';
187         if (copy_to_user(buffer, buf, len))
188                 return -EFAULT;
189         *lenp = len;
190         *ppos += *lenp;
191         return 0;
192 }
193
194 #ifdef RANDOM_FAIL_ALLOC
195 int LL_PROC_PROTO(proc_alloc_fail_rate)
196 {
197         int rc          = 0;
198         DECLARE_LL_PROC_PPOS_DECL;
199
200         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
201                 *lenp = 0;
202                 return 0;
203         }
204         if (write) {
205                 rc = lprocfs_write_frac_helper(buffer, *lenp, 
206                                                (unsigned int*)table->data,
207                                                OBD_ALLOC_FAIL_MULT);
208         } else {
209                 char buf[21];
210                 int  len;
211
212                 len = lprocfs_read_frac_helper(buf, 21,
213                                                *(unsigned int*)table->data,
214                                                OBD_ALLOC_FAIL_MULT);
215                 if (len > *lenp)
216                         len = *lenp;
217                 buf[len] = '\0';
218                 if (copy_to_user(buffer, buf, len))
219                         return -EFAULT;
220                 *lenp = len;
221         }
222         *ppos += *lenp;
223         return rc;
224 }
225 #endif
226
227 static cfs_sysctl_table_t obd_table[] = {
228         {
229                 .ctl_name = OBD_FAIL_LOC,
230                 .procname = "fail_loc",
231                 .data     = &obd_fail_loc,
232                 .maxlen   = sizeof(int),
233                 .mode     = 0644,
234                 .proc_handler = &proc_fail_loc
235         },
236         {
237                 .ctl_name = OBD_FAIL_VAL,
238                 .procname = "fail_val",
239                 .data     = &obd_fail_val,
240                 .maxlen   = sizeof(int),
241                 .mode     = 0644,
242                 .proc_handler = &proc_dointvec
243         },
244         {
245                 .ctl_name = OBD_TIMEOUT,
246                 .procname = "timeout",
247                 .data     = &obd_timeout,
248                 .maxlen   = sizeof(int),
249                 .mode     = 0644,
250                 .proc_handler = &proc_set_timeout
251         },
252         {
253                 .ctl_name = OBD_DEBUG_PEER_ON_TIMEOUT,
254                 .procname = "debug_peer_on_timeout",
255                 .data     = &obd_debug_peer_on_timeout,
256                 .maxlen   = sizeof(int),
257                 .mode     = 0644,
258                 .proc_handler = &proc_dointvec
259         },
260         {
261                 .ctl_name = OBD_DUMP_ON_TIMEOUT,
262                 .procname = "dump_on_timeout",
263                 .data     = &obd_dump_on_timeout,
264                 .maxlen   = sizeof(int),
265                 .mode     = 0644,
266                 .proc_handler = &proc_dointvec
267         },
268         {
269                 .ctl_name = OBD_DUMP_ON_EVICTION,
270                 .procname = "dump_on_eviction",
271                 .data     = &obd_dump_on_eviction,
272                 .maxlen   = sizeof(int),
273                 .mode     = 0644,
274                 .proc_handler = &proc_dointvec
275         },
276         {
277                 .ctl_name = OBD_MEMUSED,
278                 .procname = "memused",
279                 .data     = NULL,
280                 .maxlen   = 0,
281                 .mode     = 0444,
282                 .proc_handler = &proc_memory_alloc
283         },
284         {
285                 .ctl_name = OBD_PAGESUSED,
286                 .procname = "pagesused",
287                 .data     = NULL,
288                 .maxlen   = 0,
289                 .mode     = 0444,
290                 .proc_handler = &proc_pages_alloc
291         },
292         {
293                 .ctl_name = OBD_MAXMEMUSED,
294                 .procname = "memused_max",
295                 .data     = NULL,
296                 .maxlen   = 0,
297                 .mode     = 0444,
298                 .proc_handler = &proc_mem_max
299         },
300         {
301                 .ctl_name = OBD_MAXPAGESUSED,
302                 .procname = "pagesused_max",
303                 .data     = NULL,
304                 .maxlen   = 0,
305                 .mode     = 0444,
306                 .proc_handler = &proc_pages_max
307         },
308         {
309                 .ctl_name = OBD_LDLM_TIMEOUT,
310                 .procname = "ldlm_timeout",
311                 .data     = &ldlm_timeout,
312                 .maxlen   = sizeof(int),
313                 .mode     = 0644,
314                 .proc_handler = &proc_set_timeout
315         },
316 #ifdef RANDOM_FAIL_LOC
317         {
318                 .ctl_name = OBD_ALLOC_FAIL_RATE,
319                 .procname = "alloc_fail_rate",
320                 .data     = &obd_alloc_fail_rate,
321                 .maxlen   = sizeof(int),
322                 .mode     = 0644,
323                 .proc_handler = &proc_alloc_fail_rate
324         },
325 #endif
326         { 0 }
327 };
328
329 static cfs_sysctl_table_t parent_table[] = {
330         {
331                 .ctl_name = OBD_SYSCTL,
332                 .procname = "lustre",
333                 .data     = NULL,
334                 .maxlen   = 0,
335                 .mode     = 0555,
336                 .child    = obd_table
337         },
338         {0}
339 };
340
341 void obd_sysctl_init (void)
342 {
343 #ifdef CONFIG_SYSCTL
344         if ( !obd_table_header )
345                 obd_table_header = cfs_register_sysctl_table(parent_table, 0);
346 #endif
347 }
348
349 void obd_sysctl_clean (void)
350 {
351 #ifdef CONFIG_SYSCTL
352         if ( obd_table_header )
353                 cfs_unregister_sysctl_table(obd_table_header);
354         obd_table_header = NULL;
355 #endif
356 }