Whamcloud - gitweb
c6c81e41a33d24bdc0fccc082d59291c15880fa8
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-proc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #ifdef CONFIG_SYSCTL
80 static cfs_sysctl_table_header_t *lnet_table_header = NULL;
81 #endif
82 extern char lnet_upcall[1024];
83 /**
84  * The path of debug log dump upcall script.
85  */
86 extern char lnet_debug_log_upcall[1024];
87
88 #ifndef HAVE_SYSCTL_UNNUMBERED
89 #define CTL_LNET        (0x100)
90 enum {
91         PSDEV_DEBUG = 1,          /* control debugging */
92         PSDEV_SUBSYSTEM_DEBUG,    /* control debugging */
93         PSDEV_PRINTK,             /* force all messages to console */
94         PSDEV_CONSOLE_RATELIMIT,  /* ratelimit console messages */
95         PSDEV_CONSOLE_MAX_DELAY_CS, /* maximum delay over which we skip messages */
96         PSDEV_CONSOLE_MIN_DELAY_CS, /* initial delay over which we skip messages */
97         PSDEV_CONSOLE_BACKOFF,    /* delay increase factor */
98         PSDEV_DEBUG_PATH,         /* crashdump log location */
99         PSDEV_DEBUG_DUMP_PATH,    /* crashdump tracelog location */
100         PSDEV_LNET_UPCALL,        /* User mode upcall script  */
101         PSDEV_LNET_MEMUSED,       /* bytes currently PORTAL_ALLOCated */
102         PSDEV_LNET_CATASTROPHE,   /* if we have LBUGged or panic'd */
103         PSDEV_LNET_PANIC_ON_LBUG, /* flag to panic on LBUG */
104         PSDEV_LNET_DUMP_KERNEL,   /* snapshot kernel debug buffer to file */
105         PSDEV_LNET_DAEMON_FILE,   /* spool kernel debug buffer to file */
106         PSDEV_LNET_DEBUG_MB,      /* size of debug buffer */
107         PSDEV_LNET_DEBUG_LOG_UPCALL, /* debug log upcall script */
108         PSDEV_LNET_WATCHDOG_RATELIMIT,  /* ratelimit watchdog messages  */
109         PSDEV_LNET_FORCE_LBUG,    /* hook to force an LBUG */
110         PSDEV_LNET_FAIL_LOC,      /* control test failures instrumentation */
111         PSDEV_LNET_FAIL_VAL,      /* userdata for fail loc */
112 };
113 #else
114 #define CTL_LNET                        CTL_UNNUMBERED
115 #define PSDEV_DEBUG                     CTL_UNNUMBERED
116 #define PSDEV_SUBSYSTEM_DEBUG           CTL_UNNUMBERED
117 #define PSDEV_PRINTK                    CTL_UNNUMBERED
118 #define PSDEV_CONSOLE_RATELIMIT         CTL_UNNUMBERED
119 #define PSDEV_CONSOLE_MAX_DELAY_CS      CTL_UNNUMBERED
120 #define PSDEV_CONSOLE_MIN_DELAY_CS      CTL_UNNUMBERED
121 #define PSDEV_CONSOLE_BACKOFF           CTL_UNNUMBERED
122 #define PSDEV_DEBUG_PATH                CTL_UNNUMBERED
123 #define PSDEV_DEBUG_DUMP_PATH           CTL_UNNUMBERED
124 #define PSDEV_LNET_UPCALL               CTL_UNNUMBERED
125 #define PSDEV_LNET_MEMUSED              CTL_UNNUMBERED
126 #define PSDEV_LNET_CATASTROPHE          CTL_UNNUMBERED
127 #define PSDEV_LNET_PANIC_ON_LBUG        CTL_UNNUMBERED
128 #define PSDEV_LNET_DUMP_KERNEL          CTL_UNNUMBERED
129 #define PSDEV_LNET_DAEMON_FILE          CTL_UNNUMBERED
130 #define PSDEV_LNET_DEBUG_MB             CTL_UNNUMBERED
131 #define PSDEV_LNET_DEBUG_LOG_UPCALL     CTL_UNNUMBERED
132 #define PSDEV_LNET_WATCHDOG_RATELIMIT   CTL_UNNUMBERED
133 #define PSDEV_LNET_FORCE_LBUG           CTL_UNNUMBERED
134 #define PSDEV_LNET_FAIL_LOC             CTL_UNNUMBERED
135 #define PSDEV_LNET_FAIL_VAL             CTL_UNNUMBERED
136 #endif
137
138 int
139 proc_call_handler(void *data, int write,
140                   loff_t *ppos, void *buffer, size_t *lenp,
141                   int (*handler)(void *data, int write,
142                                  loff_t pos, void *buffer, int len))
143 {
144         int rc = handler(data, write, *ppos, buffer, *lenp);
145
146         if (rc < 0)
147                 return rc;
148
149         if (write) {
150                 *ppos += *lenp;
151         } else {
152                 *lenp = rc;
153                 *ppos += rc;
154         }
155         return 0;
156 }
157 EXPORT_SYMBOL(proc_call_handler);
158
159 static int __proc_dobitmasks(void *data, int write,
160                              loff_t pos, void *buffer, int nob)
161 {
162         const int     tmpstrlen = 512;
163         char         *tmpstr;
164         int           rc;
165         unsigned int *mask = data;
166         int           is_subsys = (mask == &libcfs_subsystem_debug) ? 1 : 0;
167         int           is_printk = (mask == &libcfs_printk) ? 1 : 0;
168
169         rc = cfs_trace_allocate_string_buffer(&tmpstr, tmpstrlen);
170         if (rc < 0)
171                 return rc;
172
173         if (!write) {
174                 libcfs_debug_mask2str(tmpstr, tmpstrlen, *mask, is_subsys);
175                 rc = strlen(tmpstr);
176
177                 if (pos >= rc) {
178                         rc = 0;
179                 } else {
180                         rc = cfs_trace_copyout_string(buffer, nob,
181                                                       tmpstr + pos, "\n");
182                 }
183         } else {
184                 rc = cfs_trace_copyin_string(tmpstr, tmpstrlen, buffer, nob);
185                 if (rc < 0)
186                         return rc;
187
188                 rc = libcfs_debug_str2mask(mask, tmpstr, is_subsys);
189                 /* Always print LBUG/LASSERT to console, so keep this mask */
190                 if (is_printk)
191                         *mask |= D_EMERG;
192         }
193
194         cfs_trace_free_string_buffer(tmpstr, tmpstrlen);
195         return rc;
196 }
197
198 DECLARE_PROC_HANDLER(proc_dobitmasks)
199
200 static int min_watchdog_ratelimit = 0;          /* disable ratelimiting */
201 static int max_watchdog_ratelimit = (24*60*60); /* limit to once per day */
202
203 static int __proc_dump_kernel(void *data, int write,
204                               loff_t pos, void *buffer, int nob)
205 {
206         if (!write)
207                 return 0;
208
209         return cfs_trace_dump_debug_buffer_usrstr(buffer, nob);
210 }
211
212 DECLARE_PROC_HANDLER(proc_dump_kernel)
213
214 static int __proc_daemon_file(void *data, int write,
215                               loff_t pos, void *buffer, int nob)
216 {
217         if (!write) {
218                 int len = strlen(cfs_tracefile);
219
220                 if (pos >= len)
221                         return 0;
222
223                 return cfs_trace_copyout_string(buffer, nob,
224                                                 cfs_tracefile + pos, "\n");
225         }
226
227         return cfs_trace_daemon_command_usrstr(buffer, nob);
228 }
229
230 DECLARE_PROC_HANDLER(proc_daemon_file)
231
232 static int __proc_debug_mb(void *data, int write,
233                            loff_t pos, void *buffer, int nob)
234 {
235         if (!write) {
236                 char tmpstr[32];
237                 int  len = snprintf(tmpstr, sizeof(tmpstr), "%d",
238                                     cfs_trace_get_debug_mb());
239
240                 if (pos >= len)
241                         return 0;
242
243                 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
244                        "\n");
245         }
246
247         return cfs_trace_set_debug_mb_usrstr(buffer, nob);
248 }
249
250 DECLARE_PROC_HANDLER(proc_debug_mb)
251
252 int LL_PROC_PROTO(proc_console_max_delay_cs)
253 {
254         int rc, max_delay_cs;
255         cfs_sysctl_table_t dummy = *table;
256         cfs_duration_t d;
257
258         dummy.data = &max_delay_cs;
259         dummy.proc_handler = &proc_dointvec;
260
261         if (!write) { /* read */
262                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
263                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
264                 return rc;
265         }
266
267         /* write */
268         max_delay_cs = 0;
269         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
270         if (rc < 0)
271                 return rc;
272         if (max_delay_cs <= 0)
273                 return -EINVAL;
274
275         d = cfs_time_seconds(max_delay_cs) / 100;
276         if (d == 0 || d < libcfs_console_min_delay)
277                 return -EINVAL;
278         libcfs_console_max_delay = d;
279
280         return rc;
281 }
282
283 int LL_PROC_PROTO(proc_console_min_delay_cs)
284 {
285         int rc, min_delay_cs;
286         cfs_sysctl_table_t dummy = *table;
287         cfs_duration_t d;
288
289         dummy.data = &min_delay_cs;
290         dummy.proc_handler = &proc_dointvec;
291
292         if (!write) { /* read */
293                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
294                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
295                 return rc;
296         }
297
298         /* write */
299         min_delay_cs = 0;
300         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
301         if (rc < 0)
302                 return rc;
303         if (min_delay_cs <= 0)
304                 return -EINVAL;
305
306         d = cfs_time_seconds(min_delay_cs) / 100;
307         if (d == 0 || d > libcfs_console_max_delay)
308                 return -EINVAL;
309         libcfs_console_min_delay = d;
310
311         return rc;
312 }
313
314 int LL_PROC_PROTO(proc_console_backoff)
315 {
316         int rc, backoff;
317         cfs_sysctl_table_t dummy = *table;
318
319         dummy.data = &backoff;
320         dummy.proc_handler = &proc_dointvec;
321
322         if (!write) { /* read */
323                 backoff= libcfs_console_backoff;
324                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
325                 return rc;
326         }
327
328         /* write */
329         backoff = 0;
330         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
331         if (rc < 0)
332                 return rc;
333         if (backoff <= 0)
334                 return -EINVAL;
335
336         libcfs_console_backoff = backoff;
337
338         return rc;
339 }
340
341 int LL_PROC_PROTO(libcfs_force_lbug)
342 {
343         if (write)
344                 LBUG();
345         return 0;
346 }
347
348 int LL_PROC_PROTO(proc_fail_loc)
349 {
350         int rc;
351         long old_fail_loc = cfs_fail_loc;
352
353         rc = ll_proc_dolongvec(table, write, filp, buffer, lenp, ppos);
354         if (old_fail_loc != cfs_fail_loc)
355                 cfs_waitq_signal(&cfs_race_waitq);
356         return rc;
357 }
358
359 static cfs_sysctl_table_t lnet_table[] = {
360         /*
361          * NB No .strategy entries have been provided since sysctl(8) prefers
362          * to go via /proc for portability.
363          */
364         {
365                 INIT_CTL_NAME(PSDEV_DEBUG)
366                 .procname = "debug",
367                 .data     = &libcfs_debug,
368                 .maxlen   = sizeof(int),
369                 .mode     = 0644,
370                 .proc_handler = &proc_dobitmasks,
371         },
372         {
373                 INIT_CTL_NAME(PSDEV_SUBSYSTEM_DEBUG)
374                 .procname = "subsystem_debug",
375                 .data     = &libcfs_subsystem_debug,
376                 .maxlen   = sizeof(int),
377                 .mode     = 0644,
378                 .proc_handler = &proc_dobitmasks,
379         },
380         {
381                 INIT_CTL_NAME(PSDEV_PRINTK)
382                 .procname = "printk",
383                 .data     = &libcfs_printk,
384                 .maxlen   = sizeof(int),
385                 .mode     = 0644,
386                 .proc_handler = &proc_dobitmasks,
387         },
388         {
389                 INIT_CTL_NAME(PSDEV_CONSOLE_RATELIMIT)
390                 .procname = "console_ratelimit",
391                 .data     = &libcfs_console_ratelimit,
392                 .maxlen   = sizeof(int),
393                 .mode     = 0644,
394                 .proc_handler = &proc_dointvec
395         },
396         {
397                 INIT_CTL_NAME(PSDEV_CONSOLE_MAX_DELAY_CS)
398                 .procname = "console_max_delay_centisecs",
399                 .maxlen   = sizeof(int),
400                 .mode     = 0644,
401                 .proc_handler = &proc_console_max_delay_cs
402         },
403         {
404                 INIT_CTL_NAME(PSDEV_CONSOLE_MIN_DELAY_CS)
405                 .procname = "console_min_delay_centisecs",
406                 .maxlen   = sizeof(int),
407                 .mode     = 0644,
408                 .proc_handler = &proc_console_min_delay_cs
409         },
410         {
411                 INIT_CTL_NAME(PSDEV_CONSOLE_BACKOFF)
412                 .procname = "console_backoff",
413                 .maxlen   = sizeof(int),
414                 .mode     = 0644,
415                 .proc_handler = &proc_console_backoff
416         },
417
418         {
419                 INIT_CTL_NAME(PSDEV_DEBUG_PATH)
420                 .procname = "debug_path",
421                 .data     = libcfs_debug_file_path_arr,
422                 .maxlen   = sizeof(libcfs_debug_file_path_arr),
423                 .mode     = 0644,
424                 .proc_handler = &proc_dostring,
425         },
426
427         {
428                 INIT_CTL_NAME(PSDEV_LNET_UPCALL)
429                 .procname = "upcall",
430                 .data     = lnet_upcall,
431                 .maxlen   = sizeof(lnet_upcall),
432                 .mode     = 0644,
433                 .proc_handler = &proc_dostring,
434         },
435         {
436                 INIT_CTL_NAME(PSDEV_LNET_DEBUG_LOG_UPCALL)
437                 .procname = "debug_log_upcall",
438                 .data     = lnet_debug_log_upcall,
439                 .maxlen   = sizeof(lnet_debug_log_upcall),
440                 .mode     = 0644,
441                 .proc_handler = &proc_dostring,
442         },
443         {
444                 INIT_CTL_NAME(PSDEV_LNET_MEMUSED)
445                 .procname = "lnet_memused",
446                 .data     = (int *)&libcfs_kmemory.counter,
447                 .maxlen   = sizeof(int),
448                 .mode     = 0444,
449                 .proc_handler = &proc_dointvec,
450                 INIT_STRATEGY(&sysctl_intvec)
451         },
452         {
453                 INIT_CTL_NAME(PSDEV_LNET_CATASTROPHE)
454                 .procname = "catastrophe",
455                 .data     = &libcfs_catastrophe,
456                 .maxlen   = sizeof(int),
457                 .mode     = 0444,
458                 .proc_handler = &proc_dointvec,
459                 INIT_STRATEGY(&sysctl_intvec)
460         },
461         {
462                 INIT_CTL_NAME(PSDEV_LNET_PANIC_ON_LBUG)
463                 .procname = "panic_on_lbug",
464                 .data     = &libcfs_panic_on_lbug,
465                 .maxlen   = sizeof(int),
466                 .mode     = 0644,
467                 .proc_handler = &proc_dointvec,
468                 INIT_STRATEGY(&sysctl_intvec)
469         },
470         {
471                 INIT_CTL_NAME(PSDEV_LNET_DUMP_KERNEL)
472                 .procname = "dump_kernel",
473                 .maxlen   = 256,
474                 .mode     = 0200,
475                 .proc_handler = &proc_dump_kernel,
476         },
477         {
478                 INIT_CTL_NAME(PSDEV_LNET_DAEMON_FILE)
479                 .procname = "daemon_file",
480                 .mode     = 0644,
481                 .maxlen   = 256,
482                 .proc_handler = &proc_daemon_file,
483         },
484         {
485                 INIT_CTL_NAME(PSDEV_LNET_DEBUG_MB)
486                 .procname = "debug_mb",
487                 .mode     = 0644,
488                 .proc_handler = &proc_debug_mb,
489         },
490         {
491                 INIT_CTL_NAME(PSDEV_LNET_WATCHDOG_RATELIMIT)
492                 .procname = "watchdog_ratelimit",
493                 .data     = &libcfs_watchdog_ratelimit,
494                 .maxlen   = sizeof(int),
495                 .mode     = 0644,
496                 .proc_handler = &proc_dointvec_minmax,
497                 .extra1   = &min_watchdog_ratelimit,
498                 .extra2   = &max_watchdog_ratelimit,
499         },
500         {       INIT_CTL_NAME(PSDEV_LNET_FORCE_LBUG)
501                 .procname = "force_lbug",
502                 .data     = NULL,
503                 .maxlen   = 0,
504                 .mode     = 0200,
505                 .proc_handler = &libcfs_force_lbug
506         },
507         {
508                 INIT_CTL_NAME(PSDEV_LNET_FAIL_LOC)
509                 .procname = "fail_loc",
510                 .data     = &cfs_fail_loc,
511                 .maxlen   = sizeof(cfs_fail_loc),
512                 .mode     = 0644,
513                 .proc_handler = &proc_fail_loc
514         },
515         {
516                 INIT_CTL_NAME(PSDEV_LNET_FAIL_VAL)
517                 .procname = "fail_val",
518                 .data     = &cfs_fail_val,
519                 .maxlen   = sizeof(int),
520                 .mode     = 0644,
521                 .proc_handler = &proc_dointvec
522         },
523         {
524                 INIT_CTL_NAME(0)
525         }
526 };
527
528 #ifdef CONFIG_SYSCTL
529 static cfs_sysctl_table_t top_table[] = {
530         {
531                 INIT_CTL_NAME(CTL_LNET)
532                 .procname = "lnet",
533                 .mode     = 0555,
534                 .data     = NULL,
535                 .maxlen   = 0,
536                 .child    = lnet_table,
537         },
538         {
539                 INIT_CTL_NAME(0)
540         }
541 };
542
543 int insert_proc(void)
544 {
545         if (lnet_table_header == NULL)
546                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
547 #endif
548         return 0;
549 }
550
551 void remove_proc(void)
552 {
553 #ifdef CONFIG_SYSCTL
554         if (lnet_table_header != NULL)
555                 cfs_unregister_sysctl_table(lnet_table_header);
556
557         lnet_table_header = NULL;
558 #endif
559 }