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