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