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