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  * 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         OBD_MAX_DIRTY_PAGES,    /* maximum dirty pages */
76 };
77
78 int LL_PROC_PROTO(proc_fail_loc)
79 {
80         int rc;
81         long old_fail_loc = obd_fail_loc;
82
83         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
84         if (old_fail_loc != obd_fail_loc)
85                 wake_up(&obd_race_waitq);
86         return rc;
87 }
88
89 int LL_PROC_PROTO(proc_set_timeout)
90 {
91         int rc;
92
93         rc = ll_proc_dointvec(table, write, filp, buffer, lenp, ppos);
94         if (ldlm_timeout >= obd_timeout)
95                 ldlm_timeout = max(obd_timeout / 3, 1U);
96         return rc;
97 }
98
99 int LL_PROC_PROTO(proc_memory_alloc)
100 {
101         char buf[22];
102         int len;
103         DECLARE_LL_PROC_PPOS_DECL;
104
105         if (!*lenp || (*ppos && !write)) {
106                 *lenp = 0;
107                 return 0;
108         }
109         if (write) 
110                 return -EINVAL;
111         
112         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_sum());
113         if (len > *lenp)
114                 len = *lenp;
115         buf[len] = '\0';
116         if (copy_to_user(buffer, buf, len))
117                 return -EFAULT;
118         *lenp = len;
119         *ppos += *lenp;
120         return 0;
121 }
122
123 int LL_PROC_PROTO(proc_pages_alloc)
124 {
125         char buf[22];
126         int len;
127         DECLARE_LL_PROC_PPOS_DECL;
128
129         if (!*lenp || (*ppos && !write)) {
130                 *lenp = 0;
131                 return 0;
132         }
133         if (write)
134                 return -EINVAL;
135
136         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_sum());
137         if (len > *lenp)
138                 len = *lenp;
139         buf[len] = '\0';
140         if (copy_to_user(buffer, buf, len))
141                 return -EFAULT;
142         *lenp = len;
143         *ppos += *lenp;
144         return 0;
145 }
146
147 int LL_PROC_PROTO(proc_mem_max)
148 {
149         char buf[22];
150         int len;
151         DECLARE_LL_PROC_PPOS_DECL;
152
153         if (!*lenp || (*ppos && !write)) {
154                 *lenp = 0;
155                 return 0;
156         }
157         if (write)
158                 return -EINVAL;
159
160         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_memory_max());
161         if (len > *lenp)
162                 len = *lenp;
163         buf[len] = '\0';
164         if (copy_to_user(buffer, buf, len))
165                 return -EFAULT;
166         *lenp = len;
167         *ppos += *lenp;
168         return 0;
169 }
170
171 int LL_PROC_PROTO(proc_pages_max)
172 {
173         char buf[22];
174         int len;
175         DECLARE_LL_PROC_PPOS_DECL;
176
177         if (!*lenp || (*ppos && !write)) {
178                 *lenp = 0;
179                 return 0;
180         }
181         if (write)
182                 return -EINVAL;
183
184         len = snprintf(buf, sizeof(buf), LPU64"\n", obd_pages_max());
185         if (len > *lenp)
186                 len = *lenp;
187         buf[len] = '\0';
188         if (copy_to_user(buffer, buf, len))
189                 return -EFAULT;
190         *lenp = len;
191         *ppos += *lenp;
192         return 0;
193 }
194
195 int LL_PROC_PROTO(proc_max_dirty_pages_in_mb)
196 {
197         int rc = 0;
198
199         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
200                 *lenp = 0;
201                 return 0;
202         }
203         if (write) {
204                 rc = lprocfs_write_frac_helper(buffer, *lenp,
205                                                (unsigned int*)table->data,
206                                                1 << (20 - CFS_PAGE_SHIFT));
207                 /* Don't allow them to let dirty pages exceed 90% of system memory,
208                  * and set a hard minimum of 4MB. */
209                 if (obd_max_dirty_pages > ((num_physpages / 10) * 9)) {
210                         CERROR("Refusing to set max dirty pages to %u, which "
211                                "is more than 90%% of available RAM; setting to %lu\n",
212                                obd_max_dirty_pages, ((num_physpages / 10) * 9));
213                         obd_max_dirty_pages = ((num_physpages / 10) * 9);
214                 } else if (obd_max_dirty_pages < 4 << (20 - CFS_PAGE_SHIFT)) {
215                         obd_max_dirty_pages = 4 << (20 - CFS_PAGE_SHIFT);
216                 }
217         } else {
218                 char buf[21];
219                 int len;
220
221                 len = lprocfs_read_frac_helper(buf, sizeof(buf),
222                                                *(unsigned int*)table->data,
223                                                1 << (20 - CFS_PAGE_SHIFT));
224                 if (len > *lenp) 
225                         len = *lenp;
226                 buf[len] = '\0';
227                 if (copy_to_user(buffer, buf, len))
228                         return -EFAULT;
229                 *lenp = len;
230         }
231         *ppos += *lenp;
232         return rc;
233 }
234
235 #ifdef RANDOM_FAIL_ALLOC
236 int LL_PROC_PROTO(proc_alloc_fail_rate)
237 {
238         int rc          = 0;
239         DECLARE_LL_PROC_PPOS_DECL;
240
241         if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) {
242                 *lenp = 0;
243                 return 0;
244         }
245         if (write) {
246                 rc = lprocfs_write_frac_helper(buffer, *lenp, 
247                                                (unsigned int*)table->data,
248                                                OBD_ALLOC_FAIL_MULT);
249         } else {
250                 char buf[21];
251                 int  len;
252
253                 len = lprocfs_read_frac_helper(buf, 21,
254                                                *(unsigned int*)table->data,
255                                                OBD_ALLOC_FAIL_MULT);
256                 if (len > *lenp)
257                         len = *lenp;
258                 buf[len] = '\0';
259                 if (copy_to_user(buffer, buf, len))
260                         return -EFAULT;
261                 *lenp = len;
262         }
263         *ppos += *lenp;
264         return rc;
265 }
266 #endif
267
268 static cfs_sysctl_table_t obd_table[] = {
269         {
270                 .ctl_name = OBD_FAIL_LOC,
271                 .procname = "fail_loc",
272                 .data     = &obd_fail_loc,
273                 .maxlen   = sizeof(int),
274                 .mode     = 0644,
275                 .proc_handler = &proc_fail_loc
276         },
277         {
278                 .ctl_name = OBD_FAIL_VAL,
279                 .procname = "fail_val",
280                 .data     = &obd_fail_val,
281                 .maxlen   = sizeof(int),
282                 .mode     = 0644,
283                 .proc_handler = &proc_dointvec
284         },
285         {
286                 .ctl_name = OBD_TIMEOUT,
287                 .procname = "timeout",
288                 .data     = &obd_timeout,
289                 .maxlen   = sizeof(int),
290                 .mode     = 0644,
291                 .proc_handler = &proc_set_timeout
292         },
293         {
294                 .ctl_name = OBD_DEBUG_PEER_ON_TIMEOUT,
295                 .procname = "debug_peer_on_timeout",
296                 .data     = &obd_debug_peer_on_timeout,
297                 .maxlen   = sizeof(int),
298                 .mode     = 0644,
299                 .proc_handler = &proc_dointvec
300         },
301         {
302                 .ctl_name = OBD_DUMP_ON_TIMEOUT,
303                 .procname = "dump_on_timeout",
304                 .data     = &obd_dump_on_timeout,
305                 .maxlen   = sizeof(int),
306                 .mode     = 0644,
307                 .proc_handler = &proc_dointvec
308         },
309         {
310                 .ctl_name = OBD_DUMP_ON_EVICTION,
311                 .procname = "dump_on_eviction",
312                 .data     = &obd_dump_on_eviction,
313                 .maxlen   = sizeof(int),
314                 .mode     = 0644,
315                 .proc_handler = &proc_dointvec
316         },
317         {
318                 .ctl_name = OBD_MEMUSED,
319                 .procname = "memused",
320                 .data     = NULL,
321                 .maxlen   = 0,
322                 .mode     = 0444,
323                 .proc_handler = &proc_memory_alloc
324         },
325         {
326                 .ctl_name = OBD_PAGESUSED,
327                 .procname = "pagesused",
328                 .data     = NULL,
329                 .maxlen   = 0,
330                 .mode     = 0444,
331                 .proc_handler = &proc_pages_alloc
332         },
333         {
334                 .ctl_name = OBD_MAXMEMUSED,
335                 .procname = "memused_max",
336                 .data     = NULL,
337                 .maxlen   = 0,
338                 .mode     = 0444,
339                 .proc_handler = &proc_mem_max
340         },
341         {
342                 .ctl_name = OBD_MAXPAGESUSED,
343                 .procname = "pagesused_max",
344                 .data     = NULL,
345                 .maxlen   = 0,
346                 .mode     = 0444,
347                 .proc_handler = &proc_pages_max
348         },
349         {
350                 .ctl_name = OBD_LDLM_TIMEOUT,
351                 .procname = "ldlm_timeout",
352                 .data     = &ldlm_timeout,
353                 .maxlen   = sizeof(int),
354                 .mode     = 0644,
355                 .proc_handler = &proc_set_timeout
356         },
357 #ifdef RANDOM_FAIL_LOC
358         {
359                 .ctl_name = OBD_ALLOC_FAIL_RATE,
360                 .procname = "alloc_fail_rate",
361                 .data     = &obd_alloc_fail_rate,
362                 .maxlen   = sizeof(int),
363                 .mode     = 0644,
364                 .proc_handler = &proc_alloc_fail_rate
365         },
366 #endif
367         {
368                 .ctl_name = OBD_MAX_DIRTY_PAGES,
369                 .procname = "max_dirty_mb",
370                 .data     = &obd_max_dirty_pages,
371                 .maxlen   = sizeof(int),
372                 .mode     = 0644,
373                 .proc_handler = &proc_max_dirty_pages_in_mb
374         },
375         { 0 }
376 };
377
378 static cfs_sysctl_table_t parent_table[] = {
379         {
380                 .ctl_name = OBD_SYSCTL,
381                 .procname = "lustre",
382                 .data     = NULL,
383                 .maxlen   = 0,
384                 .mode     = 0555,
385                 .child    = obd_table
386         },
387         {0}
388 };
389
390 void obd_sysctl_init (void)
391 {
392 #ifdef CONFIG_SYSCTL
393         if ( !obd_table_header )
394                 obd_table_header = cfs_register_sysctl_table(parent_table, 0);
395 #endif
396 }
397
398 void obd_sysctl_clean (void)
399 {
400 #ifdef CONFIG_SYSCTL
401         if ( obd_table_header )
402                 cfs_unregister_sysctl_table(obd_table_header);
403         obd_table_header = NULL;
404 #endif
405 }