Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[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 = cfs_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 = cfs_trace_copyout_string(buffer, nob,
174                                                       tmpstr + pos, "\n");
175                 }
176         } else {
177                 rc = cfs_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         cfs_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 cfs_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(cfs_tracefile);
212
213                 if (pos >= len)
214                         return 0;
215
216                 return cfs_trace_copyout_string(buffer, nob,
217                                                 cfs_tracefile + pos, "\n");
218         }
219
220         return cfs_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                                     cfs_trace_get_debug_mb());
232
233                 if (pos >= len)
234                         return 0;
235
236                 return cfs_trace_copyout_string(buffer, nob, tmpstr + pos,
237                        "\n");
238         }
239
240         return cfs_trace_set_debug_mb_usrstr(buffer, nob);
241 }
242
243 DECLARE_PROC_HANDLER(proc_debug_mb)
244
245 int LL_PROC_PROTO(proc_console_max_delay_cs)
246 {
247         int rc, max_delay_cs;
248         cfs_sysctl_table_t dummy = *table;
249         cfs_duration_t d;
250
251         dummy.data = &max_delay_cs;
252         dummy.proc_handler = &proc_dointvec;
253
254         if (!write) { /* read */
255                 max_delay_cs = cfs_duration_sec(libcfs_console_max_delay * 100);
256                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
257                 return rc;
258         }
259
260         /* write */
261         max_delay_cs = 0;
262         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
263         if (rc < 0)
264                 return rc;
265         if (max_delay_cs <= 0)
266                 return -EINVAL;
267
268         d = cfs_time_seconds(max_delay_cs) / 100;
269         if (d == 0 || d < libcfs_console_min_delay)
270                 return -EINVAL;
271         libcfs_console_max_delay = d;
272
273         return rc;
274 }
275
276 int LL_PROC_PROTO(proc_console_min_delay_cs)
277 {
278         int rc, min_delay_cs;
279         cfs_sysctl_table_t dummy = *table;
280         cfs_duration_t d;
281
282         dummy.data = &min_delay_cs;
283         dummy.proc_handler = &proc_dointvec;
284
285         if (!write) { /* read */
286                 min_delay_cs = cfs_duration_sec(libcfs_console_min_delay * 100);
287                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
288                 return rc;
289         }
290
291         /* write */
292         min_delay_cs = 0;
293         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
294         if (rc < 0)
295                 return rc;
296         if (min_delay_cs <= 0)
297                 return -EINVAL;
298
299         d = cfs_time_seconds(min_delay_cs) / 100;
300         if (d == 0 || d > libcfs_console_max_delay)
301                 return -EINVAL;
302         libcfs_console_min_delay = d;
303
304         return rc;
305 }
306
307 int LL_PROC_PROTO(proc_console_backoff)
308 {
309         int rc, backoff;
310         cfs_sysctl_table_t dummy = *table;
311
312         dummy.data = &backoff;
313         dummy.proc_handler = &proc_dointvec;
314
315         if (!write) { /* read */
316                 backoff= libcfs_console_backoff;
317                 rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
318                 return rc;
319         }
320
321         /* write */
322         backoff = 0;
323         rc = ll_proc_dointvec(&dummy, write, filp, buffer, lenp, ppos);
324         if (rc < 0)
325                 return rc;
326         if (backoff <= 0)
327                 return -EINVAL;
328
329         libcfs_console_backoff = backoff;
330
331         return rc;
332 }
333
334 static cfs_sysctl_table_t lnet_table[] = {
335         /*
336          * NB No .strategy entries have been provided since sysctl(8) prefers
337          * to go via /proc for portability.
338          */
339         {
340                 .ctl_name = PSDEV_DEBUG,
341                 .procname = "debug",
342                 .data     = &libcfs_debug,
343                 .maxlen   = sizeof(int),
344                 .mode     = 0644,
345                 .proc_handler = &proc_dobitmasks,
346         },
347         {
348                 .ctl_name = PSDEV_SUBSYSTEM_DEBUG,
349                 .procname = "subsystem_debug",
350                 .data     = &libcfs_subsystem_debug,
351                 .maxlen   = sizeof(int),
352                 .mode     = 0644,
353                 .proc_handler = &proc_dobitmasks,
354         },
355         {
356                 .ctl_name = PSDEV_PRINTK,
357                 .procname = "printk",
358                 .data     = &libcfs_printk,
359                 .maxlen   = sizeof(int),
360                 .mode     = 0644,
361                 .proc_handler = &proc_dobitmasks,
362         },
363         {
364                 .ctl_name = PSDEV_CONSOLE_RATELIMIT,
365                 .procname = "console_ratelimit",
366                 .data     = &libcfs_console_ratelimit,
367                 .maxlen   = sizeof(int),
368                 .mode     = 0644,
369                 .proc_handler = &proc_dointvec
370         },
371         {
372                 .ctl_name = PSDEV_CONSOLE_MAX_DELAY_CS,
373                 .procname = "console_max_delay_centisecs",
374                 .maxlen   = sizeof(int),
375                 .mode     = 0644,
376                 .proc_handler = &proc_console_max_delay_cs
377         },
378         {
379                 .ctl_name = PSDEV_CONSOLE_MIN_DELAY_CS,
380                 .procname = "console_min_delay_centisecs",
381                 .maxlen   = sizeof(int),
382                 .mode     = 0644,
383                 .proc_handler = &proc_console_min_delay_cs
384         },
385         {
386                 .ctl_name = PSDEV_CONSOLE_BACKOFF,
387                 .procname = "console_backoff",
388                 .maxlen   = sizeof(int),
389                 .mode     = 0644,
390                 .proc_handler = &proc_console_backoff
391         },
392
393         {
394                 .ctl_name = PSDEV_DEBUG_PATH,
395                 .procname = "debug_path",
396                 .data     = libcfs_debug_file_path_arr,
397                 .maxlen   = sizeof(libcfs_debug_file_path_arr),
398                 .mode     = 0644,
399                 .proc_handler = &proc_dostring,
400         },
401
402         {
403                 .ctl_name = PSDEV_LNET_UPCALL,
404                 .procname = "upcall",
405                 .data     = lnet_upcall,
406                 .maxlen   = sizeof(lnet_upcall),
407                 .mode     = 0644,
408                 .proc_handler = &proc_dostring,
409         },
410         {
411                 .ctl_name = PSDEV_LNET_DEBUG_LOG_UPCALL,
412                 .procname = "debug_log_upcall",
413                 .data     = lnet_debug_log_upcall,
414                 .maxlen   = sizeof(lnet_debug_log_upcall),
415                 .mode     = 0644,
416                 .proc_handler = &proc_dostring,
417         },
418         {
419                 .ctl_name = PSDEV_LNET_MEMUSED,
420                 .procname = "memused",
421                 .data     = (int *)&libcfs_kmemory.counter,
422                 .maxlen   = sizeof(int),
423                 .mode     = 0444,
424                 .proc_handler = &proc_dointvec,
425                 .strategy = &sysctl_intvec,
426         },
427         {
428                 .ctl_name = PSDEV_LNET_CATASTROPHE,
429                 .procname = "catastrophe",
430                 .data     = &libcfs_catastrophe,
431                 .maxlen   = sizeof(int),
432                 .mode     = 0444,
433                 .proc_handler = &proc_dointvec,
434                 .strategy = &sysctl_intvec,
435         },
436         {
437                 .ctl_name = PSDEV_LNET_PANIC_ON_LBUG,
438                 .procname = "panic_on_lbug",
439                 .data     = &libcfs_panic_on_lbug,
440                 .maxlen   = sizeof(int),
441                 .mode     = 0644,
442                 .proc_handler = &proc_dointvec,
443                 .strategy = &sysctl_intvec,
444         },
445         {
446                 .ctl_name = PSDEV_LNET_DUMP_KERNEL,
447                 .procname = "dump_kernel",
448                 .maxlen   = 256,
449                 .mode     = 0200,
450                 .proc_handler = &proc_dump_kernel,
451         },
452         {
453                 .ctl_name = PSDEV_LNET_DAEMON_FILE,
454                 .procname = "daemon_file",
455                 .mode     = 0644,
456                 .maxlen   = 256,
457                 .proc_handler = &proc_daemon_file,
458         },
459         {
460                 .ctl_name = PSDEV_LNET_DEBUG_MB,
461                 .procname = "debug_mb",
462                 .mode     = 0644,
463                 .proc_handler = &proc_debug_mb,
464         },
465         {
466                 .ctl_name = PSDEV_LNET_WATCHDOG_RATELIMIT,
467                 .procname = "watchdog_ratelimit",
468                 .data     = &libcfs_watchdog_ratelimit,
469                 .maxlen   = sizeof(int),
470                 .mode     = 0644,
471                 .proc_handler = &proc_dointvec_minmax,
472                 .extra1   = &min_watchdog_ratelimit,
473                 .extra2   = &max_watchdog_ratelimit,
474         },
475         {0}
476 };
477
478 static cfs_sysctl_table_t top_table[] = {
479         {
480                 .ctl_name = CTL_LNET,
481                 .procname = "lnet",
482                 .mode     = 0555,
483                 .data     = NULL,
484                 .maxlen   = 0,
485                 .child    = lnet_table,
486         },
487         {
488                 .ctl_name = 0
489         }
490 };
491
492 int insert_proc(void)
493 {
494 #ifdef CONFIG_SYSCTL
495         if (lnet_table_header == NULL)
496                 lnet_table_header = cfs_register_sysctl_table(top_table, 0);
497 #endif
498         return 0;
499 }
500
501 void remove_proc(void)
502 {
503 #ifdef CONFIG_SYSCTL
504         if (lnet_table_header != NULL)
505                 cfs_unregister_sysctl_table(lnet_table_header);
506
507         lnet_table_header = NULL;
508 #endif
509 }