Whamcloud - gitweb
Branch HEAD
[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         PSDEV_LNET_WATCHDOG_RATELIMIT,  /* ratelimit watchdog messages  */
107 };
108 #else
109 #define CTL_LNET                        CTL_UNNUMBERED
110 #define PSDEV_DEBUG                     CTL_UNNUMBERED
111 #define PSDEV_SUBSYSTEM_DEBUG           CTL_UNNUMBERED
112 #define PSDEV_PRINTK                    CTL_UNNUMBERED
113 #define PSDEV_CONSOLE_RATELIMIT         CTL_UNNUMBERED
114 #define PSDEV_CONSOLE_MAX_DELAY_CS      CTL_UNNUMBERED
115 #define PSDEV_CONSOLE_MIN_DELAY_CS      CTL_UNNUMBERED
116 #define PSDEV_CONSOLE_BACKOFF           CTL_UNNUMBERED
117 #define PSDEV_DEBUG_PATH                CTL_UNNUMBERED
118 #define PSDEV_DEBUG_DUMP_PATH           CTL_UNNUMBERED
119 #define PSDEV_LNET_UPCALL               CTL_UNNUMBERED
120 #define PSDEV_LNET_MEMUSED              CTL_UNNUMBERED
121 #define PSDEV_LNET_CATASTROPHE          CTL_UNNUMBERED
122 #define PSDEV_LNET_PANIC_ON_LBUG        CTL_UNNUMBERED
123 #define PSDEV_LNET_DUMP_KERNEL          CTL_UNNUMBERED
124 #define PSDEV_LNET_DAEMON_FILE          CTL_UNNUMBERED
125 #define PSDEV_LNET_DEBUG_MB             CTL_UNNUMBERED
126 #define PSDEV_LNET_DEBUG_LOG_UPCALL     CTL_UNNUMBERED
127 #endif
128
129
130 static int
131 proc_call_handler(void *data, int write,
132                   loff_t *ppos, void *buffer, size_t *lenp,
133                   int (*handler)(void *data, int write,
134                                  loff_t pos, void *buffer, int len))
135 {
136         int rc = handler(data, write, *ppos, buffer, *lenp);
137
138         if (rc < 0)
139                 return rc;
140
141         if (write) {
142                 *ppos += *lenp;
143         } else {
144                 *lenp = rc;
145                 *ppos += rc;
146         }
147         return 0;
148 }
149 EXPORT_SYMBOL(proc_call_handler);
150
151 static int __proc_dobitmasks(void *data, int write,
152                              loff_t pos, void *buffer, int nob)
153 {
154         const int     tmpstrlen = 512;
155         char         *tmpstr;
156         int           rc;
157         unsigned int *mask = data;
158         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
159         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
160
161         rc = trace_allocate_string_buffer(&tmpstr, tmpstrlen);
162         if (rc < 0)
163                 return rc;
164
165         if (!write) {
166                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
167                 rc = strlen(tmpstr);
168
169                 if (pos >= rc) {
170                         rc = 0;
171                 } else {
172                         rc = trace_copyout_string(buffer, nob,
173                                                   tmpstr + pos, "\n");
174                 }
175         } else {
176                 rc = trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
177                 if (rc < 0)
178                         return rc;
179
180                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
181                 /* Always print LBUG/LASSERT to console, so keep this mask */
182                 if (is_printk)
183                         *mask |= D_EMERG;
184         }
185
186         trace_free_string_buffer(tmpstr, tmpstrlen);
187         return rc;
188 }
189
190 DECLARE_PROC_HANDLER(proc_dobitmasks)
191
192 static int min_watchdog_ratelimit = 0;          /* disable ratelimiting */
193 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
194
195 static int __proc_dump_kernel(void *data, int write,
196                               loff_t pos, void *buffer, int nob)
197 {
198         if (!write)
199                 return 0;
200
201         return trace_dump_debug_buffer_usrstr(buffer, nob);
202 }
203
204 DECLARE_PROC_HANDLER(proc_dump_kernel)
205
206 static int __proc_daemon_file(void *data, int write,
207                               loff_t pos, void *buffer, int nob)
208 {
209         if (!write) {
210                 int len = strlen(tracefile);
211
212                 if (pos >= len)
213                         return 0;
214
215                 return trace_copyout_string(buffer, nob,
216                                             tracefile + pos, "\n");
217         }
218
219         return trace_daemon_command_usrstr(buffer, nob);
220 }
221
222 DECLARE_PROC_HANDLER(proc_daemon_file)
223
224 static int __proc_debug_mb(void *data, int write,
225                            loff_t pos, void *buffer, int nob)
226 {
227         if (!write) {
228                 char tmpstr[32];
229                 int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
230                                     trace_get_debug_mb());
231
232                 if (pos >= len)
233                         return 0;
234
235                 return trace_copyout_string(buffer, nob, tmpstr + pos, "\n");
236         }
237
238         return trace_set_debug_mb_usrstr(buffer, nob);
239 }
240
241 DECLARE_PROC_HANDLER(proc_debug_mb)
242
243 int LL_PROC_PROTO(proc_console_max_delay_cs)
244 {
245         int rc, max_delay_cs;
246         cfs_sysctl_table_t dummy = *table;
247         cfs_duration_t d;
248
249         dummy.data = &max_delay_cs;
250         dummy.proc_handler = &proc_dointvec;
251
252         if (!write) { /* read */
253                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
254                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
255                 return rc;
256         }
257
258         /* write */
259         max_delay_cs = 0;
260         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
261         if (rc < 0)
262                 return rc;
263         if (max_delay_cs <= 0)
264                 return -EINVAL;
265
266         d = cfs_time_seconds(max_delay_cs) / 100;
267         if (d == 0 || d < libcfs_console_min_delay)
268                 return -EINVAL;
269         libcfs_console_max_delay = d;
270
271         return rc;
272 }
273
274 int LL_PROC_PROTO(proc_console_min_delay_cs)
275 {
276         int rc, min_delay_cs;
277         cfs_sysctl_table_t dummy = *table;
278         cfs_duration_t d;
279
280         dummy.data = &min_delay_cs;
281         dummy.proc_handler = &proc_dointvec;
282
283         if (!write) { /* read */
284                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
285                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
286                 return rc;
287         }
288
289         /* write */
290         min_delay_cs = 0;
291         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
292         if (rc < 0)
293                 return rc;
294         if (min_delay_cs <= 0)
295                 return -EINVAL;
296
297         d = cfs_time_seconds(min_delay_cs) / 100;
298         if (d == 0 || d > libcfs_console_max_delay)
299                 return -EINVAL;
300         libcfs_console_min_delay = d;
301
302         return rc;
303 }
304
305 int LL_PROC_PROTO(proc_console_backoff)
306 {
307         int rc, backoff;
308         cfs_sysctl_table_t dummy = *table;
309
310         dummy.data = &backoff;
311         dummy.proc_handler = &proc_dointvec;
312
313         if (!write) { /* read */
314                 backoff= libcfs_console_backoff;
315                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
316                 return rc;
317         }
318
319         /* write */
320         backoff = 0;
321         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
322         if (rc < 0)
323                 return rc;
324         if (backoff <= 0)
325                 return -EINVAL;
326
327         libcfs_console_backoff = backoff;
328
329         return rc;
330 }
331
332 static cfs_sysctl_table_t lnet_table[] = {
333         /*
334          * NB No .strategy entries have been provided since sysctl(8) prefers
335          * to go via /proc for portability.
336          */
337         {
338                 .ctl_name = PSDEV_DEBUG,
339                 .procname = "debug",
340                 .data     = &libcfs_debug,
341                 .maxlen   = sizeof(int),
342                 .mode     = 0644,
343                 .proc_handler = &proc_dobitmasks,
344         },
345         {
346                 .ctl_name = PSDEV_SUBSYSTEM_DEBUG,
347                 .procname = "subsystem_debug",
348                 .data     = &libcfs_subsystem_debug,
349                 .maxlen   = sizeof(int),
350                 .mode     = 0644,
351                 .proc_handler = &proc_dobitmasks,
352         },
353         {
354                 .ctl_name = PSDEV_PRINTK,
355                 .procname = "printk",
356                 .data     = &libcfs_printk,
357                 .maxlen   = sizeof(int),
358                 .mode     = 0644,
359                 .proc_handler = &proc_dobitmasks,
360         },
361         {
362                 .ctl_name = PSDEV_CONSOLE_RATELIMIT,
363                 .procname = "console_ratelimit",
364                 .data     = &libcfs_console_ratelimit,
365                 .maxlen   = sizeof(int),
366                 .mode     = 0644,
367                 .proc_handler = &proc_dointvec
368         },
369         {
370                 .ctl_name = PSDEV_CONSOLE_MAX_DELAY_CS,
371                 .procname = "console_max_delay_centisecs",
372                 .maxlen   = sizeof(int),
373                 .mode     = 0644,
374                 .proc_handler = &proc_console_max_delay_cs
375         },
376         {
377                 .ctl_name = PSDEV_CONSOLE_MIN_DELAY_CS,
378                 .procname = "console_min_delay_centisecs",
379                 .maxlen   = sizeof(int),
380                 .mode     = 0644,
381                 .proc_handler = &proc_console_min_delay_cs
382         },
383         {
384                 .ctl_name = PSDEV_CONSOLE_BACKOFF,
385                 .procname = "console_backoff",
386                 .maxlen   = sizeof(int),
387                 .mode     = 0644,
388                 .proc_handler = &proc_console_backoff
389         },
390
391         {
392                 .ctl_name = PSDEV_DEBUG_PATH,
393                 .procname = "debug_path",
394                 .data     = debug_file_path_arr,
395                 .maxlen   = sizeof(debug_file_path_arr),
396                 .mode     = 0644,
397                 .proc_handler = &proc_dostring,
398         },
399
400         {
401                 .ctl_name = PSDEV_LNET_UPCALL,
402                 .procname = "upcall",
403                 .data     = lnet_upcall,
404                 .maxlen   = sizeof(lnet_upcall),
405                 .mode     = 0644,
406                 .proc_handler = &proc_dostring,
407         },
408         {
409                 .ctl_name = PSDEV_LNET_DEBUG_LOG_UPCALL,
410                 .procname = "debug_log_upcall",
411                 .data     = lnet_debug_log_upcall,
412                 .maxlen   = sizeof(lnet_debug_log_upcall),
413                 .mode     = 0644,
414                 .proc_handler = &proc_dostring,
415         },
416         {
417                 .ctl_name = PSDEV_LNET_MEMUSED,
418                 .procname = "memused",
419                 .data     = (int *)&libcfs_kmemory.counter,
420                 .maxlen   = sizeof(int),
421                 .mode     = 0444,
422                 .proc_handler = &proc_dointvec,
423                 .strategy = &sysctl_intvec,
424         },
425         {
426                 .ctl_name = PSDEV_LNET_CATASTROPHE,
427                 .procname = "catastrophe",
428                 .data     = &libcfs_catastrophe,
429                 .maxlen   = sizeof(int),
430                 .mode     = 0444,
431                 .proc_handler = &proc_dointvec,
432                 .strategy = &sysctl_intvec,
433         },
434         {
435                 .ctl_name = PSDEV_LNET_PANIC_ON_LBUG,
436                 .procname = "panic_on_lbug",
437                 .data     = &libcfs_panic_on_lbug,
438                 .maxlen   = sizeof(int),
439                 .mode     = 0644,
440                 .proc_handler = &proc_dointvec,
441                 .strategy = &sysctl_intvec,
442         },
443         {
444                 .ctl_name = PSDEV_LNET_DUMP_KERNEL,
445                 .procname = "dump_kernel",
446                 .maxlen   = 256,
447                 .mode     = 0200,
448                 .proc_handler = &proc_dump_kernel,
449         },
450         {
451                 .ctl_name = PSDEV_LNET_DAEMON_FILE,
452                 .procname = "daemon_file",
453                 .mode     = 0644,
454                 .maxlen   = 256,
455                 .proc_handler = &proc_daemon_file,
456         },
457         {
458                 .ctl_name = PSDEV_LNET_DEBUG_MB,
459                 .procname = "debug_mb",
460                 .mode     = 0644,
461                 .proc_handler = &proc_debug_mb,
462         },
463         {
464                 .ctl_name = PSDEV_LNET_WATCHDOG_RATELIMIT,
465                 .procname = "watchdog_ratelimit",
466                 .data     = &libcfs_watchdog_ratelimit,
467                 .maxlen   = sizeof(int),
468                 .mode     = 0644,
469                 .proc_handler = &proc_dointvec_minmax,
470                 .extra1   = &min_watchdog_ratelimit,
471                 .extra2   = &max_watchdog_ratelimit,
472         },
473         {0}
474 };
475
476 static cfs_sysctl_table_t top_table[] = {
477         {
478                 .ctl_name = CTL_LNET,
479                 .procname = "lnet",
480                 .mode     = 0555,
481                 .data     = NULL,
482                 .maxlen   = 0,
483                 .child    = lnet_table,
484         },
485         {
486                 .ctl_name = 0
487         }
488 };
489
490 int insert_proc(void)
491 {
492 #ifdef CONFIG_SYSCTL
493         if (lnet_table_header == NULL)
494                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
495 #endif
496         return 0;
497 }
498
499 void remove_proc(void)
500 {
501 #ifdef CONFIG_SYSCTL
502         if (lnet_table_header != NULL)
503                 cfs_unregister_sysctl_table(lnet_table_header);
504
505         lnet_table_header = NULL;
506 #endif
507 }