Whamcloud - gitweb
Drop slow OSCs if we can, but not for requested start idx.
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-proc.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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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  * libcfs/libcfs/linux/linux-proc.c
37  *
38  * Author: Zach Brown <zab@zabbo.net>
39  * Author: Peter J. Braam <braam@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
49 #endif
50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/mm.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/smp_lock.h>
57 #include <linux/unistd.h>
58 #include <net/sock.h>
59 #include <linux/uio.h>
60
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63
64 #include <linux/fs.h>
65 #include <linux/file.h>
66 #include <linux/stat.h>
67 #include <linux/list.h>
68 #include <asm/uaccess.h>
69
70 #include <linux/proc_fs.h>
71 #include <linux/sysctl.h>
72
73 # define DEBUG_SUBSYSTEM S_LNET
74
75 #include <libcfs/libcfs.h>
76 #include <asm/div64.h>
77 #include "tracefile.h"
78
79 static cfs_sysctl_table_header_t *lnet_table_header = NULL;
80 extern char lnet_upcall[1024];
81 /**
82  * The path of debug log dump upcall script.
83  */
84 extern char lnet_debug_log_upcall[1024];
85
86 #ifndef HAVE_SYSCTL_UNNUMBERED
87 #define CTL_LNET        (0x100)
88 enum {
89         PSDEV_DEBUG = 1,          /* control debugging */
90         PSDEV_SUBSYSTEM_DEBUG,    /* control debugging */
91         PSDEV_PRINTK,             /* force all messages to console */
92         PSDEV_CONSOLE_RATELIMIT,  /* ratelimit console messages */
93         PSDEV_CONSOLE_MAX_DELAY_CS, /* maximum delay over which we skip messages */
94         PSDEV_CONSOLE_MIN_DELAY_CS, /* initial delay over which we skip messages */
95         PSDEV_CONSOLE_BACKOFF,    /* delay increase factor */
96         PSDEV_DEBUG_PATH,         /* crashdump log location */
97         PSDEV_DEBUG_DUMP_PATH,    /* crashdump tracelog location */
98         PSDEV_LNET_UPCALL,        /* User mode upcall script  */
99         PSDEV_LNET_MEMUSED,       /* bytes currently PORTAL_ALLOCated */
100         PSDEV_LNET_CATASTROPHE,   /* if we have LBUGged or panic'd */
101         PSDEV_LNET_PANIC_ON_LBUG, /* flag to panic on LBUG */
102         PSDEV_LNET_DUMP_KERNEL,   /* snapshot kernel debug buffer to file */
103         PSDEV_LNET_DAEMON_FILE,   /* spool kernel debug buffer to file */
104         PSDEV_LNET_DEBUG_MB,      /* size of debug buffer */
105         PSDEV_LNET_DEBUG_LOG_UPCALL, /* debug log upcall script */
106 };
107 #else
108 #define CTL_LNET                        CTL_UNNUMBERED
109 #define PSDEV_DEBUG                     CTL_UNNUMBERED
110 #define PSDEV_SUBSYSTEM_DEBUG           CTL_UNNUMBERED
111 #define PSDEV_PRINTK                    CTL_UNNUMBERED
112 #define PSDEV_CONSOLE_RATELIMIT         CTL_UNNUMBERED
113 #define PSDEV_CONSOLE_MAX_DELAY_CS      CTL_UNNUMBERED
114 #define PSDEV_CONSOLE_MIN_DELAY_CS      CTL_UNNUMBERED
115 #define PSDEV_CONSOLE_BACKOFF           CTL_UNNUMBERED
116 #define PSDEV_DEBUG_PATH                CTL_UNNUMBERED
117 #define PSDEV_DEBUG_DUMP_PATH           CTL_UNNUMBERED
118 #define PSDEV_LNET_UPCALL               CTL_UNNUMBERED
119 #define PSDEV_LNET_MEMUSED              CTL_UNNUMBERED
120 #define PSDEV_LNET_CATASTROPHE          CTL_UNNUMBERED
121 #define PSDEV_LNET_PANIC_ON_LBUG        CTL_UNNUMBERED
122 #define PSDEV_LNET_DUMP_KERNEL          CTL_UNNUMBERED
123 #define PSDEV_LNET_DAEMON_FILE          CTL_UNNUMBERED
124 #define PSDEV_LNET_DEBUG_MB             CTL_UNNUMBERED
125 #define PSDEV_LNET_DEBUG_LOG_UPCALL     CTL_UNNUMBERED
126 #endif
127
128
129 static int
130 proc_call_handler(void *data, int write,
131                   loff_t *ppos, void *buffer, size_t *lenp,
132                   int (*handler)(void *data, int write,
133                                  loff_t pos, void *buffer, int len))
134 {
135         int rc = handler(data, write, *ppos, buffer, *lenp);
136
137         if (rc < 0)
138                 return rc;
139
140         if (write) {
141                 *ppos += *lenp;
142         } else {
143                 *lenp = rc;
144                 *ppos += rc;
145         }
146         return 0;
147 }
148 EXPORT_SYMBOL(proc_call_handler);
149
150 static int __proc_dobitmasks(void *data, int write,
151                              loff_t pos, void *buffer, int nob)
152 {
153         const int     tmpstrlen = 512;
154         char         *tmpstr;
155         int           rc;
156         unsigned int *mask = data;
157         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
158         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
159
160         rc = trace_allocate_string_buffer(&tmpstr, tmpstrlen);
161         if (rc < 0)
162                 return rc;
163
164         if (!write) {
165                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
166                 rc = strlen(tmpstr);
167
168                 if (pos >= rc) {
169                         rc = 0;
170                 } else {
171                         rc = trace_copyout_string(buffer, nob,
172                                                   tmpstr + pos, "\n");
173                 }
174         } else {
175                 rc = trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
176                 if (rc < 0)
177                         return rc;
178
179                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
180                 /* Always print LBUG/LASSERT to console, so keep this mask */
181                 if (is_printk)
182                         *mask |= D_EMERG;
183         }
184
185         trace_free_string_buffer(tmpstr, tmpstrlen);
186         return rc;
187 }
188
189 DECLARE_PROC_HANDLER(proc_dobitmasks)
190
191 static int __proc_dump_kernel(void *data, int write,
192                               loff_t pos, void *buffer, int nob)
193 {
194         if (!write)
195                 return 0;
196
197         return trace_dump_debug_buffer_usrstr(buffer, nob);
198 }
199
200 DECLARE_PROC_HANDLER(proc_dump_kernel)
201
202 static int __proc_daemon_file(void *data, int write,
203                               loff_t pos, void *buffer, int nob)
204 {
205         if (!write) {
206                 int len = strlen(tracefile);
207
208                 if (pos >= len)
209                         return 0;
210
211                 return trace_copyout_string(buffer, nob,
212                                             tracefile + pos, "\n");
213         }
214
215         return trace_daemon_command_usrstr(buffer, nob);
216 }
217
218 DECLARE_PROC_HANDLER(proc_daemon_file)
219
220 static int __proc_debug_mb(void *data, int write,
221                            loff_t pos, void *buffer, int nob)
222 {
223         if (!write) {
224                 char tmpstr[32];
225                 int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
226                                     trace_get_debug_mb());
227
228                 if (pos >= len)
229                         return 0;
230
231                 return trace_copyout_string(buffer, nob, tmpstr + pos, "\n");
232         }
233
234         return trace_set_debug_mb_usrstr(buffer, nob);
235 }
236
237 DECLARE_PROC_HANDLER(proc_debug_mb)
238
239 int LL_PROC_PROTO(proc_console_max_delay_cs)
240 {
241         int rc, max_delay_cs;
242         cfs_sysctl_table_t dummy = *table;
243         cfs_duration_t d;
244
245         dummy.data = &max_delay_cs;
246         dummy.proc_handler = &proc_dointvec;
247
248         if (!write) { /* read */
249                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
250                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
251                 return rc;
252         }
253
254         /* write */
255         max_delay_cs = 0;
256         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
257         if (rc < 0)
258                 return rc;
259         if (max_delay_cs <= 0)
260                 return -EINVAL;
261
262         d = cfs_time_seconds(max_delay_cs) / 100;
263         if (d == 0 || d < libcfs_console_min_delay)
264                 return -EINVAL;
265         libcfs_console_max_delay = d;
266
267         return rc;
268 }
269
270 int LL_PROC_PROTO(proc_console_min_delay_cs)
271 {
272         int rc, min_delay_cs;
273         cfs_sysctl_table_t dummy = *table;
274         cfs_duration_t d;
275
276         dummy.data = &min_delay_cs;
277         dummy.proc_handler = &proc_dointvec;
278
279         if (!write) { /* read */
280                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
281                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
282                 return rc;
283         }
284
285         /* write */
286         min_delay_cs = 0;
287         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
288         if (rc < 0)
289                 return rc;
290         if (min_delay_cs <= 0)
291                 return -EINVAL;
292
293         d = cfs_time_seconds(min_delay_cs) / 100;
294         if (d == 0 || d > libcfs_console_max_delay)
295                 return -EINVAL;
296         libcfs_console_min_delay = d;
297
298         return rc;
299 }
300
301 int LL_PROC_PROTO(proc_console_backoff)
302 {
303         int rc, backoff;
304         cfs_sysctl_table_t dummy = *table;
305
306         dummy.data = &backoff;
307         dummy.proc_handler = &proc_dointvec;
308
309         if (!write) { /* read */
310                 backoff= libcfs_console_backoff;
311                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
312                 return rc;
313         }
314
315         /* write */
316         backoff = 0;
317         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
318         if (rc < 0)
319                 return rc;
320         if (backoff <= 0)
321                 return -EINVAL;
322
323         libcfs_console_backoff = backoff;
324
325         return rc;
326 }
327
328 static cfs_sysctl_table_t lnet_table[] = {
329         /*
330          * NB No .strategy entries have been provided since sysctl(8) prefers
331          * to go via /proc for portability.
332          */
333         {
334                 .ctl_name = PSDEV_DEBUG,
335                 .procname = "debug",
336                 .data     = &libcfs_debug,
337                 .maxlen   = sizeof(int),
338                 .mode     = 0644,
339                 .proc_handler = &proc_dobitmasks,
340         },
341         {
342                 .ctl_name = PSDEV_SUBSYSTEM_DEBUG,
343                 .procname = "subsystem_debug",
344                 .data     = &libcfs_subsystem_debug,
345                 .maxlen   = sizeof(int),
346                 .mode     = 0644,
347                 .proc_handler = &proc_dobitmasks,
348         },
349         {
350                 .ctl_name = PSDEV_PRINTK,
351                 .procname = "printk",
352                 .data     = &libcfs_printk,
353                 .maxlen   = sizeof(int),
354                 .mode     = 0644,
355                 .proc_handler = &proc_dobitmasks,
356         },
357         {
358                 .ctl_name = PSDEV_CONSOLE_RATELIMIT,
359                 .procname = "console_ratelimit",
360                 .data     = &libcfs_console_ratelimit,
361                 .maxlen   = sizeof(int),
362                 .mode     = 0644,
363                 .proc_handler = &proc_dointvec
364         },
365         {
366                 .ctl_name = PSDEV_CONSOLE_MAX_DELAY_CS,
367                 .procname = "console_max_delay_centisecs",
368                 .maxlen   = sizeof(int),
369                 .mode     = 0644,
370                 .proc_handler = &proc_console_max_delay_cs
371         },
372         {
373                 .ctl_name = PSDEV_CONSOLE_MIN_DELAY_CS,
374                 .procname = "console_min_delay_centisecs",
375                 .maxlen   = sizeof(int),
376                 .mode     = 0644,
377                 .proc_handler = &proc_console_min_delay_cs
378         },
379         {
380                 .ctl_name = PSDEV_CONSOLE_BACKOFF,
381                 .procname = "console_backoff",
382                 .maxlen   = sizeof(int),
383                 .mode     = 0644,
384                 .proc_handler = &proc_console_backoff
385         },
386
387         {
388                 .ctl_name = PSDEV_DEBUG_PATH,
389                 .procname = "debug_path",
390                 .data     = debug_file_path_arr,
391                 .maxlen   = sizeof(debug_file_path_arr),
392                 .mode     = 0644,
393                 .proc_handler = &proc_dostring,
394         },
395
396         {
397                 .ctl_name = PSDEV_LNET_UPCALL,
398                 .procname = "upcall",
399                 .data     = lnet_upcall,
400                 .maxlen   = sizeof(lnet_upcall),
401                 .mode     = 0644,
402                 .proc_handler = &proc_dostring,
403         },
404         {
405                 .ctl_name = PSDEV_LNET_DEBUG_LOG_UPCALL,
406                 .procname = "debug_log_upcall",
407                 .data     = lnet_debug_log_upcall,
408                 .maxlen   = sizeof(lnet_debug_log_upcall),
409                 .mode     = 0644,
410                 .proc_handler = &proc_dostring,
411         },
412         {
413                 .ctl_name = PSDEV_LNET_MEMUSED,
414                 .procname = "memused",
415                 .data     = (int *)&libcfs_kmemory.counter,
416                 .maxlen   = sizeof(int),
417                 .mode     = 0444,
418                 .proc_handler = &proc_dointvec,
419                 .strategy = &sysctl_intvec,
420         },
421         {
422                 .ctl_name = PSDEV_LNET_CATASTROPHE,
423                 .procname = "catastrophe",
424                 .data     = &libcfs_catastrophe,
425                 .maxlen   = sizeof(int),
426                 .mode     = 0444,
427                 .proc_handler = &proc_dointvec,
428                 .strategy = &sysctl_intvec,
429         },
430         {
431                 .ctl_name = PSDEV_LNET_PANIC_ON_LBUG,
432                 .procname = "panic_on_lbug",
433                 .data     = &libcfs_panic_on_lbug,
434                 .maxlen   = sizeof(int),
435                 .mode     = 0644,
436                 .proc_handler = &proc_dointvec,
437                 .strategy = &sysctl_intvec,
438         },
439         {
440                 .ctl_name = PSDEV_LNET_DUMP_KERNEL,
441                 .procname = "dump_kernel",
442                 .maxlen   = 256,
443                 .mode     = 0200,
444                 .proc_handler = &proc_dump_kernel,
445         },
446         {
447                 .ctl_name = PSDEV_LNET_DAEMON_FILE,
448                 .procname = "daemon_file",
449                 .mode     = 0644,
450                 .maxlen   = 256,
451                 .proc_handler = &proc_daemon_file,
452         },
453         {
454                 .ctl_name = PSDEV_LNET_DEBUG_MB,
455                 .procname = "debug_mb",
456                 .mode     = 0644,
457                 .proc_handler = &proc_debug_mb,
458         },
459         {0}
460 };
461
462 static cfs_sysctl_table_t top_table[] = {
463         {
464                 .ctl_name = CTL_LNET,
465                 .procname = "lnet",
466                 .mode     = 0555,
467                 .data     = NULL,
468                 .maxlen   = 0,
469                 .child    = lnet_table,
470         },
471         {
472                 .ctl_name = 0
473         }
474 };
475
476 int insert_proc(void)
477 {
478 #ifdef CONFIG_SYSCTL
479         if (lnet_table_header == NULL)
480                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
481 #endif
482         return 0;
483 }
484
485 void remove_proc(void)
486 {
487 #ifdef CONFIG_SYSCTL
488         if (lnet_table_header != NULL)
489                 cfs_unregister_sysctl_table(lnet_table_header);
490
491         lnet_table_header = NULL;
492 #endif
493 }