Whamcloud - gitweb
ef946331a537d28bea40076861c1801853472464
[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 #ifdef HAVE_SEGMENT_H
39 # include <asm/segment.h>
40 #endif
41 #include <asm/uaccess.h>
42 #include <linux/utsname.h>
43
44 #define DEBUG_SUBSYSTEM S_CLASS
45
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
48
49 cfs_sysctl_table_header_t *obd_table_header = NULL;
50
51 #define OBD_SYSCTL 300
52
53 enum {
54         OBD_FAIL_LOC = 1,       /* control test failures instrumentation */
55         OBD_FAIL_VAL,           /* userdata for fail loc */
56         OBD_TIMEOUT,            /* RPC timeout before recovery/intr */
57         OBD_DUMP_ON_TIMEOUT,    /* dump kernel debug log upon eviction */
58         OBD_MEMUSED,            /* bytes currently OBD_ALLOCated */
59         OBD_PAGESUSED,          /* pages currently OBD_PAGE_ALLOCated */
60         OBD_MAXMEMUSED,         /* maximum bytes OBD_ALLOCated concurrently */
61         OBD_MAXPAGESUSED,       /* maximum pages OBD_PAGE_ALLOCated concurrently */
62         OBD_SYNCFILTER,         /* XXX temporary, as we play with sync osts.. */
63         OBD_LDLM_TIMEOUT,       /* LDLM timeout for ASTs before client eviction */
64         OBD_DUMP_ON_EVICTION,   /* dump kernel debug log upon eviction */
65         OBD_DEBUG_PEER_ON_TIMEOUT, /* dump peer debug when RPC times out */
66         OBD_ALLOC_FAIL_RATE,    /* memory allocation random failure rate */
67 };
68
69 int LL_PROC_PROTO(proc_fail_loc)
70 {
71         int rc;
72         long old_fail_loc = obd_fail_loc;
73
74         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
75         if (old_fail_loc != obd_fail_loc)
76                 wake_up(&obd_race_waitq);
77         return rc;
78 }
79
80 int LL_PROC_PROTO(proc_set_timeout)
81 {
82         int rc;
83
84         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
85         if (ldlm_timeout >= obd_timeout)
86                 ldlm_timeout = max(obd_timeout / 3, 1U);
87         return rc;
88 }
89
90 int LL_PROC_PROTO(proc_memory_alloc)
91 {
92         char buf[22];
93         int len;
94         DECLARE_LL_PROC_PPOS_DECL;
95
96         if (!*lenp || (*ppos && !write)) {
97                 *lenp = 0;
98                 return 0;
99         }
100         if (write) 
101                 return -EINVAL;
102         
103         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
104         if (len > *lenp)
105                 len = *lenp;
106         buf[len] = '\0';
107         if (copy_to_user(buffer, buf, len))
108                 return -EFAULT;
109         *lenp = len;
110         *ppos += *lenp;
111         return 0;
112 }
113
114 int LL_PROC_PROTO(proc_pages_alloc)
115 {
116         char buf[22];
117         int len;
118         DECLARE_LL_PROC_PPOS_DECL;
119
120         if (!*lenp || (*ppos && !write)) {
121                 *lenp = 0;
122                 return 0;
123         }
124         if (write)
125                 return -EINVAL;
126
127         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
128         if (len > *lenp)
129                 len = *lenp;
130         buf[len] = '\0';
131         if (copy_to_user(buffer, buf, len))
132                 return -EFAULT;
133         *lenp = len;
134         *ppos += *lenp;
135         return 0;
136 }
137
138 int LL_PROC_PROTO(proc_mem_max)
139 {
140         char buf[22];
141         int len;
142         DECLARE_LL_PROC_PPOS_DECL;
143
144         if (!*lenp || (*ppos && !write)) {
145                 *lenp = 0;
146                 return 0;
147         }
148         if (write)
149                 return -EINVAL;
150
151         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
152         if (len > *lenp)
153                 len = *lenp;
154         buf[len] = '\0';
155         if (copy_to_user(buffer, buf, len))
156                 return -EFAULT;
157         *lenp = len;
158         *ppos += *lenp;
159         return 0;
160 }
161
162 int LL_PROC_PROTO(proc_pages_max)
163 {
164         char buf[22];
165         int len;
166         DECLARE_LL_PROC_PPOS_DECL;
167
168         if (!*lenp || (*ppos && !write)) {
169                 *lenp = 0;
170                 return 0;
171         }
172         if (write)
173                 return -EINVAL;
174
175         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
176         if (len > *lenp)
177                 len = *lenp;
178         buf[len] = '\0';
179         if (copy_to_user(buffer, buf, len))
180                 return -EFAULT;
181         *lenp = len;
182         *ppos += *lenp;
183         return 0;
184 }
185
186 #ifdef RANDOM_FAIL_ALLOC
187 int LL_PROC_PROTO(proc_alloc_fail_rate)
188 {
189         int rc          = 0;
190         DECLARE_LL_PROC_PPOS_DECL;
191
192         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
193                 *lenp = 0;
194                 return 0;
195         }
196         if (write) {
197                 rc = lprocfs_write_frac_helper(buffer, *lenp, 
198                                                (unsigned int*)table->data,
199                                                OBD_ALLOC_FAIL_MULT);
200         } else {
201                 char buf[21];
202                 int  len;
203
204                 len = lprocfs_read_frac_helper(buf, 21,
205                                                *(unsigned int*)table->data,
206                                                OBD_ALLOC_FAIL_MULT);
207                 if (len > *lenp)
208                         len = *lenp;
209                 buf[len] = '\0';
210                 if (copy_to_user(buffer, buf, len))
211                         return -EFAULT;
212                 *lenp = len;
213         }
214         *ppos += *lenp;
215         return rc;
216 }
217 #endif
218
219 static cfs_sysctl_table_t obd_table[] = {
220         {
221                 .ctl_name = OBD_FAIL_LOC,
222                 .procname = "fail_loc",
223                 .data     = &obd_fail_loc,
224                 .maxlen   = sizeof(int),
225                 .mode     = 0644,
226                 .proc_handler = &proc_fail_loc
227         },
228         {
229                 .ctl_name = OBD_FAIL_VAL,
230                 .procname = "fail_val",
231                 .data     = &obd_fail_val,
232                 .maxlen   = sizeof(int),
233                 .mode     = 0644,
234                 .proc_handler = &proc_dointvec
235         },
236         {
237                 .ctl_name = OBD_TIMEOUT,
238                 .procname = "timeout",
239                 .data     = &obd_timeout,
240                 .maxlen   = sizeof(int),
241                 .mode     = 0644,
242                 .proc_handler = &proc_set_timeout
243         },
244         {
245                 .ctl_name = OBD_DEBUG_PEER_ON_TIMEOUT,
246                 .procname = "debug_peer_on_timeout",
247                 .data     = &obd_debug_peer_on_timeout,
248                 .maxlen   = sizeof(int),
249                 .mode     = 0644,
250                 .proc_handler = &proc_dointvec
251         },
252         {
253                 .ctl_name = OBD_DUMP_ON_TIMEOUT,
254                 .procname = "dump_on_timeout",
255                 .data     = &obd_dump_on_timeout,
256                 .maxlen   = sizeof(int),
257                 .mode     = 0644,
258                 .proc_handler = &proc_dointvec
259         },
260         {
261                 .ctl_name = OBD_DUMP_ON_EVICTION,
262                 .procname = "dump_on_eviction",
263                 .data     = &obd_dump_on_eviction,
264                 .maxlen   = sizeof(int),
265                 .mode     = 0644,
266                 .proc_handler = &proc_dointvec
267         },
268         {
269                 .ctl_name = OBD_MEMUSED,
270                 .procname = "memused",
271                 .data     = NULL,
272                 .maxlen   = 0,
273                 .mode     = 0444,
274                 .proc_handler = &proc_memory_alloc
275         },
276         {
277                 .ctl_name = OBD_PAGESUSED,
278                 .procname = "pagesused",
279                 .data     = NULL,
280                 .maxlen   = 0,
281                 .mode     = 0444,
282                 .proc_handler = &proc_pages_alloc
283         },
284         {
285                 .ctl_name = OBD_MAXMEMUSED,
286                 .procname = "memused_max",
287                 .data     = NULL,
288                 .maxlen   = 0,
289                 .mode     = 0444,
290                 .proc_handler = &proc_mem_max
291         },
292         {
293                 .ctl_name = OBD_MAXPAGESUSED,
294                 .procname = "pagesused_max",
295                 .data     = NULL,
296                 .maxlen   = 0,
297                 .mode     = 0444,
298                 .proc_handler = &proc_pages_max
299         },
300         {
301                 .ctl_name = OBD_LDLM_TIMEOUT,
302                 .procname = "ldlm_timeout",
303                 .data     = &ldlm_timeout,
304                 .maxlen   = sizeof(int),
305                 .mode     = 0644,
306                 .proc_handler = &proc_set_timeout
307         },
308 #ifdef RANDOM_FAIL_LOC
309         {
310                 .ctl_name = OBD_ALLOC_FAIL_RATE,
311                 .procname = "alloc_fail_rate",
312                 .data     = &obd_alloc_fail_rate,
313                 .maxlen   = sizeof(int),
314                 .mode     = 0644,
315                 .proc_handler = &proc_alloc_fail_rate
316         },
317 #endif
318         { 0 }
319 };
320
321 static cfs_sysctl_table_t parent_table[] = {
322         {
323                 .ctl_name = OBD_SYSCTL,
324                 .procname = "lustre",
325                 .data     = NULL,
326                 .maxlen   = 0,
327                 .mode     = 0555,
328                 .child    = obd_table
329         },
330         {0}
331 };
332
333 void obd_sysctl_init (void)
334 {
335 #ifdef CONFIG_SYSCTL
336         if ( !obd_table_header )
337                 obd_table_header = cfs_register_sysctl_table(parent_table, 0);
338 #endif
339 }
340
341 void obd_sysctl_clean (void)
342 {
343 #ifdef CONFIG_SYSCTL
344         if ( obd_table_header )
345                 cfs_unregister_sysctl_table(obd_table_header);
346         obd_table_header = NULL;
347 #endif
348 }