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