Whamcloud - gitweb
Branch HEAD
[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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
188         loff_t *ppos = &filp->f_pos;
189 #endif
190         int rc          = 0;
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 }