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