Whamcloud - gitweb
121fdf99b4634a97014b70f9049a3690142a665a
[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  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/autoconf.h>
28 #include <linux/sysctl.h>
29 #include <linux/sched.h>
30 #include <linux/mm.h>
31 #include <linux/sysctl.h>
32 #include <linux/version.h>
33 #include <linux/proc_fs.h>
34 #include <linux/slab.h>
35 #include <linux/stat.h>
36 #include <linux/ctype.h>
37 #include <asm/bitops.h>
38 #include <asm/segment.h>
39 #include <asm/uaccess.h>
40 #include <linux/utsname.h>
41
42 #define DEBUG_SUBSYSTEM S_CLASS
43
44 #include <obd_support.h>
45 #include <lprocfs_status.h>
46
47 cfs_sysctl_table_header_t *obd_table_header = NULL;
48
49 #define OBD_SYSCTL 300
50
51 enum {
52         OBD_FAIL_LOC = 1,       /* control test failures instrumentation */
53         OBD_FAIL_VAL,           /* userdata for fail loc */
54         OBD_TIMEOUT,            /* RPC timeout before recovery/intr */
55         OBD_DUMP_ON_TIMEOUT,    /* dump kernel debug log upon eviction */
56         OBD_MEMUSED,            /* bytes currently OBD_ALLOCated */
57         OBD_PAGESUSED,          /* pages currently OBD_PAGE_ALLOCated */
58         OBD_MAXMEMUSED,         /* maximum bytes OBD_ALLOCated concurrently */
59         OBD_MAXPAGESUSED,       /* maximum pages OBD_PAGE_ALLOCated concurrently */
60         OBD_SYNCFILTER,         /* XXX temporary, as we play with sync osts.. */
61         OBD_LDLM_TIMEOUT,       /* LDLM timeout for ASTs before client eviction */
62         OBD_DUMP_ON_EVICTION,   /* dump kernel debug log upon eviction */
63         OBD_DEBUG_PEER_ON_TIMEOUT, /* dump peer debug when RPC times out */
64         OBD_ALLOC_FAIL_RATE,    /* memory allocation random failure rate */
65 };
66
67 int LL_PROC_PROTO(proc_fail_loc)
68 {
69         int rc;
70         int old_fail_loc = obd_fail_loc;
71
72         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
73         if (old_fail_loc != obd_fail_loc)
74                 wake_up(&obd_race_waitq);
75         return rc;
76 }
77
78 int LL_PROC_PROTO(proc_set_timeout)
79 {
80         int rc;
81
82         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
83         if (ldlm_timeout >= obd_timeout)
84                 ldlm_timeout = max(obd_timeout / 3, 1U);
85         return rc;
86 }
87
88 int LL_PROC_PROTO(proc_memory_alloc)
89 {
90         char buf[22];
91         int len;
92         DECLARE_LL_PROC_PPOS_DECL;
93
94         if (!*lenp || (*ppos && !write)) {
95                 *lenp = 0;
96                 return 0;
97         }
98         if (write) 
99                 return -EINVAL;
100         
101         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
102         if (len > *lenp)
103                 len = *lenp;
104         buf[len] = '\0';
105         if (copy_to_user(buffer, buf, len))
106                 return -EFAULT;
107         *lenp = len;
108         *ppos += *lenp;
109         return 0;
110 }
111
112 int LL_PROC_PROTO(proc_pages_alloc)
113 {
114         char buf[22];
115         int len;
116         DECLARE_LL_PROC_PPOS_DECL;
117
118         if (!*lenp || (*ppos && !write)) {
119                 *lenp = 0;
120                 return 0;
121         }
122         if (write)
123                 return -EINVAL;
124
125         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
126         if (len > *lenp)
127                 len = *lenp;
128         buf[len] = '\0';
129         if (copy_to_user(buffer, buf, len))
130                 return -EFAULT;
131         *lenp = len;
132         *ppos += *lenp;
133         return 0;
134 }
135
136 int LL_PROC_PROTO(proc_mem_max)
137 {
138         char buf[22];
139         int len;
140         DECLARE_LL_PROC_PPOS_DECL;
141
142         if (!*lenp || (*ppos && !write)) {
143                 *lenp = 0;
144                 return 0;
145         }
146         if (write)
147                 return -EINVAL;
148
149         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
150         if (len > *lenp)
151                 len = *lenp;
152         buf[len] = '\0';
153         if (copy_to_user(buffer, buf, len))
154                 return -EFAULT;
155         *lenp = len;
156         *ppos += *lenp;
157         return 0;
158 }
159
160 int LL_PROC_PROTO(proc_pages_max)
161 {
162         char buf[22];
163         int len;
164         DECLARE_LL_PROC_PPOS_DECL;
165
166         if (!*lenp || (*ppos && !write)) {
167                 *lenp = 0;
168                 return 0;
169         }
170         if (write)
171                 return -EINVAL;
172
173         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
174         if (len > *lenp)
175                 len = *lenp;
176         buf[len] = '\0';
177         if (copy_to_user(buffer, buf, len))
178                 return -EFAULT;
179         *lenp = len;
180         *ppos += *lenp;
181         return 0;
182 }
183
184 #ifdef RANDOM_FAIL_ALLOC
185 int LL_PROC_PROTO(proc_alloc_fail_rate)
186 {
187         int rc          = 0;
188         DECLARE_LL_PROC_PPOS_DECL;
189
190         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
191                 *lenp = 0;
192                 return 0;
193         }
194         if (write) {
195                 rc = lprocfs_write_frac_helper(buffer, *lenp, 
196                                                (unsigned int*)table->data,
197                                                OBD_ALLOC_FAIL_MULT);
198         } else {
199                 char buf[21];
200                 int  len;
201
202                 len = lprocfs_read_frac_helper(buf, 21,
203                                                *(unsigned int*)table->data,
204                                                OBD_ALLOC_FAIL_MULT);
205                 if (len > *lenp)
206                         len = *lenp;
207                 buf[len] = '\0';
208                 if (copy_to_user(buffer, buf, len))
209                         return -EFAULT;
210                 *lenp = len;
211         }
212         *ppos += *lenp;
213         return rc;
214 }
215 #endif
216
217 static cfs_sysctl_table_t obd_table[] = {
218         {
219                 .ctl_name = OBD_FAIL_LOC,
220                 .procname = "fail_loc",
221                 .data     = &obd_fail_loc,
222                 .maxlen   = sizeof(int),
223                 .mode     = 0644,
224                 .proc_handler = &proc_fail_loc
225         },
226         {
227                 .ctl_name = OBD_FAIL_VAL,
228                 .procname = "fail_val",
229                 .data     = &obd_fail_val,
230                 .maxlen   = sizeof(int),
231                 .mode     = 0644,
232                 .proc_handler = &proc_dointvec
233         },
234         {
235                 .ctl_name = OBD_TIMEOUT,
236                 .procname = "timeout",
237                 .data     = &obd_timeout,
238                 .maxlen   = sizeof(int),
239                 .mode     = 0644,
240                 .proc_handler = &proc_set_timeout
241         },
242         {
243                 .ctl_name = OBD_DEBUG_PEER_ON_TIMEOUT,
244                 .procname = "debug_peer_on_timeout",
245                 .data     = &obd_debug_peer_on_timeout,
246                 .maxlen   = sizeof(int),
247                 .mode     = 0644,
248                 .proc_handler = &proc_dointvec
249         },
250         {
251                 .ctl_name = OBD_DUMP_ON_TIMEOUT,
252                 .procname = "dump_on_timeout",
253                 .data     = &obd_dump_on_timeout,
254                 .maxlen   = sizeof(int),
255                 .mode     = 0644,
256                 .proc_handler = &proc_dointvec
257         },
258         {
259                 .ctl_name = OBD_DUMP_ON_EVICTION,
260                 .procname = "dump_on_eviction",
261                 .data     = &obd_dump_on_eviction,
262                 .maxlen   = sizeof(int),
263                 .mode     = 0644,
264                 .proc_handler = &proc_dointvec
265         },
266         {
267                 .ctl_name = OBD_MEMUSED,
268                 .procname = "memused",
269                 .data     = NULL,
270                 .maxlen   = 0,
271                 .mode     = 0444,
272                 .proc_handler = &proc_memory_alloc
273         },
274         {
275                 .ctl_name = OBD_PAGESUSED,
276                 .procname = "pagesused",
277                 .data     = NULL,
278                 .maxlen   = 0,
279                 .mode     = 0444,
280                 .proc_handler = &proc_pages_alloc
281         },
282         {
283                 .ctl_name = OBD_MAXMEMUSED,
284                 .procname = "memused_max",
285                 .data     = NULL,
286                 .maxlen   = 0,
287                 .mode     = 0444,
288                 .proc_handler = &proc_mem_max
289         },
290         {
291                 .ctl_name = OBD_MAXPAGESUSED,
292                 .procname = "pagesused_max",
293                 .data     = NULL,
294                 .maxlen   = 0,
295                 .mode     = 0444,
296                 .proc_handler = &proc_pages_max
297         },
298         {
299                 .ctl_name = OBD_LDLM_TIMEOUT,
300                 .procname = "ldlm_timeout",
301                 .data     = &ldlm_timeout,
302                 .maxlen   = sizeof(int),
303                 .mode     = 0644,
304                 .proc_handler = &proc_set_timeout
305         },
306 #ifdef RANDOM_FAIL_LOC
307         {
308                 .ctl_name = OBD_ALLOC_FAIL_RATE,
309                 .procname = "alloc_fail_rate",
310                 .data     = &obd_alloc_fail_rate,
311                 .maxlen   = sizeof(int),
312                 .mode     = 0644,
313                 .proc_handler = &proc_alloc_fail_rate
314         },
315 #endif
316         { 0 }
317 };
318
319 static cfs_sysctl_table_t parent_table[] = {
320         {
321                 .ctl_name = OBD_SYSCTL,
322                 .procname = "lustre",
323                 .data     = NULL,
324                 .maxlen   = 0,
325                 .mode     = 0555,
326                 .child    = obd_table
327         },
328         {0}
329 };
330
331 void obd_sysctl_init (void)
332 {
333 #ifdef CONFIG_SYSCTL
334         if ( !obd_table_header )
335                 obd_table_header = cfs_register_sysctl_table(parent_table, 0);
336 #endif
337 }
338
339 void obd_sysctl_clean (void)
340 {
341 #ifdef CONFIG_SYSCTL
342         if ( obd_table_header )
343                 cfs_unregister_sysctl_table(obd_table_header);
344         obd_table_header = NULL;
345 #endif
346 }