staging/lustre/libcfs: get rid of debugfs/lnet/console_backoff
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / lustre / libcfs / debug.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, 2012, Intel Corporation.
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/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  *
40  */
41
42 # define DEBUG_SUBSYSTEM S_LNET
43
44 #include "../../include/linux/libcfs/libcfs.h"
45 #include "tracefile.h"
46
47 static char debug_file_name[1024];
48
49 unsigned int libcfs_subsystem_debug = ~0;
50 module_param(libcfs_subsystem_debug, int, 0644);
51 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
52 EXPORT_SYMBOL(libcfs_subsystem_debug);
53
54 unsigned int libcfs_debug = (D_CANTMASK |
55                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
56 module_param(libcfs_debug, int, 0644);
57 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
58 EXPORT_SYMBOL(libcfs_debug);
59
60 static unsigned int libcfs_debug_mb;
61 module_param(libcfs_debug_mb, uint, 0644);
62 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
63 EXPORT_SYMBOL(libcfs_debug_mb);
64
65 unsigned int libcfs_printk = D_CANTMASK;
66 module_param(libcfs_printk, uint, 0644);
67 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
68 EXPORT_SYMBOL(libcfs_printk);
69
70 unsigned int libcfs_console_ratelimit = 1;
71 module_param(libcfs_console_ratelimit, uint, 0644);
72 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
73 EXPORT_SYMBOL(libcfs_console_ratelimit);
74
75 unsigned int libcfs_console_max_delay;
76 module_param(libcfs_console_max_delay, uint, 0644);
77 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
78 EXPORT_SYMBOL(libcfs_console_max_delay);
79
80 unsigned int libcfs_console_min_delay;
81 module_param(libcfs_console_min_delay, uint, 0644);
82 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
83 EXPORT_SYMBOL(libcfs_console_min_delay);
84
85 static int param_set_uint_minmax(const char *val,
86                                  const struct kernel_param *kp,
87                                  unsigned int min, unsigned int max)
88 {
89         unsigned int num;
90         int ret;
91
92         if (!val)
93                 return -EINVAL;
94         ret = kstrtouint(val, 0, &num);
95         if (ret == -EINVAL || num < min || num > max)
96                 return -EINVAL;
97         *((unsigned int *)kp->arg) = num;
98         return 0;
99 }
100
101 static int param_set_uintpos(const char *val, const struct kernel_param *kp)
102 {
103         return param_set_uint_minmax(val, kp, 1, -1);
104 }
105
106 static struct kernel_param_ops param_ops_uintpos = {
107         .set = param_set_uintpos,
108         .get = param_get_uint,
109 };
110
111 #define param_check_uintpos(name, p) \
112                 __param_check(name, p, unsigned int)
113
114 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
115 module_param(libcfs_console_backoff, uintpos, 0644);
116 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
117 EXPORT_SYMBOL(libcfs_console_backoff);
118
119 unsigned int libcfs_debug_binary = 1;
120 EXPORT_SYMBOL(libcfs_debug_binary);
121
122 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
123 EXPORT_SYMBOL(libcfs_stack);
124
125 static unsigned int portal_enter_debugger;
126 EXPORT_SYMBOL(portal_enter_debugger);
127
128 unsigned int libcfs_catastrophe;
129 EXPORT_SYMBOL(libcfs_catastrophe);
130
131 unsigned int libcfs_watchdog_ratelimit = 300;
132 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
133
134 unsigned int libcfs_panic_on_lbug = 1;
135 module_param(libcfs_panic_on_lbug, uint, 0644);
136 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
137 EXPORT_SYMBOL(libcfs_panic_on_lbug);
138
139 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
140 EXPORT_SYMBOL(libcfs_kmemory);
141
142 static wait_queue_head_t debug_ctlwq;
143
144 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
145
146 /* We need to pass a pointer here, but elsewhere this must be a const */
147 static char *libcfs_debug_file_path;
148 module_param(libcfs_debug_file_path, charp, 0644);
149 MODULE_PARM_DESC(libcfs_debug_file_path,
150                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
151
152 int libcfs_panic_in_progress;
153
154 /* libcfs_debug_token2mask() expects the returned
155  * string in lower-case */
156 static const char *
157 libcfs_debug_subsys2str(int subsys)
158 {
159         switch (1 << subsys) {
160         default:
161                 return NULL;
162         case S_UNDEFINED:
163                 return "undefined";
164         case S_MDC:
165                 return "mdc";
166         case S_MDS:
167                 return "mds";
168         case S_OSC:
169                 return "osc";
170         case S_OST:
171                 return "ost";
172         case S_CLASS:
173                 return "class";
174         case S_LOG:
175                 return "log";
176         case S_LLITE:
177                 return "llite";
178         case S_RPC:
179                 return "rpc";
180         case S_LNET:
181                 return "lnet";
182         case S_LND:
183                 return "lnd";
184         case S_PINGER:
185                 return "pinger";
186         case S_FILTER:
187                 return "filter";
188         case S_ECHO:
189                 return "echo";
190         case S_LDLM:
191                 return "ldlm";
192         case S_LOV:
193                 return "lov";
194         case S_LQUOTA:
195                 return "lquota";
196         case S_OSD:
197                 return "osd";
198         case S_LMV:
199                 return "lmv";
200         case S_SEC:
201                 return "sec";
202         case S_GSS:
203                 return "gss";
204         case S_MGC:
205                 return "mgc";
206         case S_MGS:
207                 return "mgs";
208         case S_FID:
209                 return "fid";
210         case S_FLD:
211                 return "fld";
212         }
213 }
214
215 /* libcfs_debug_token2mask() expects the returned
216  * string in lower-case */
217 static const char *
218 libcfs_debug_dbg2str(int debug)
219 {
220         switch (1 << debug) {
221         default:
222                 return NULL;
223         case D_TRACE:
224                 return "trace";
225         case D_INODE:
226                 return "inode";
227         case D_SUPER:
228                 return "super";
229         case D_EXT2:
230                 return "ext2";
231         case D_MALLOC:
232                 return "malloc";
233         case D_CACHE:
234                 return "cache";
235         case D_INFO:
236                 return "info";
237         case D_IOCTL:
238                 return "ioctl";
239         case D_NETERROR:
240                 return "neterror";
241         case D_NET:
242                 return "net";
243         case D_WARNING:
244                 return "warning";
245         case D_BUFFS:
246                 return "buffs";
247         case D_OTHER:
248                 return "other";
249         case D_DENTRY:
250                 return "dentry";
251         case D_NETTRACE:
252                 return "nettrace";
253         case D_PAGE:
254                 return "page";
255         case D_DLMTRACE:
256                 return "dlmtrace";
257         case D_ERROR:
258                 return "error";
259         case D_EMERG:
260                 return "emerg";
261         case D_HA:
262                 return "ha";
263         case D_RPCTRACE:
264                 return "rpctrace";
265         case D_VFSTRACE:
266                 return "vfstrace";
267         case D_READA:
268                 return "reada";
269         case D_MMAP:
270                 return "mmap";
271         case D_CONFIG:
272                 return "config";
273         case D_CONSOLE:
274                 return "console";
275         case D_QUOTA:
276                 return "quota";
277         case D_SEC:
278                 return "sec";
279         case D_LFSCK:
280                 return "lfsck";
281         }
282 }
283
284 int
285 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
286 {
287         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
288                                                  libcfs_debug_dbg2str;
289         int        len = 0;
290         const char   *token;
291         int        i;
292
293         if (mask == 0) {                        /* "0" */
294                 if (size > 0)
295                         str[0] = '0';
296                 len = 1;
297         } else {                                /* space-separated tokens */
298                 for (i = 0; i < 32; i++) {
299                         if ((mask & (1 << i)) == 0)
300                                 continue;
301
302                         token = fn(i);
303                         if (token == NULL)            /* unused bit */
304                                 continue;
305
306                         if (len > 0) {            /* separator? */
307                                 if (len < size)
308                                         str[len] = ' ';
309                                 len++;
310                         }
311
312                         while (*token != 0) {
313                                 if (len < size)
314                                         str[len] = *token;
315                                 token++;
316                                 len++;
317                         }
318                 }
319         }
320
321         /* terminate 'str' */
322         if (len < size)
323                 str[len] = 0;
324         else
325                 str[size - 1] = 0;
326
327         return len;
328 }
329
330 int
331 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
332 {
333         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
334                                                  libcfs_debug_dbg2str;
335         int      m = 0;
336         int      matched;
337         int      n;
338         int      t;
339
340         /* Allow a number for backwards compatibility */
341
342         for (n = strlen(str); n > 0; n--)
343                 if (!isspace(str[n-1]))
344                         break;
345         matched = n;
346         t = sscanf(str, "%i%n", &m, &matched);
347         if (t >= 1 && matched == n) {
348                 /* don't print warning for lctl set_param debug=0 or -1 */
349                 if (m != 0 && m != -1)
350                         CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
351                 *mask = m;
352                 return 0;
353         }
354
355         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
356                             0xffffffff);
357 }
358
359 /**
360  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
361  */
362 void libcfs_debug_dumplog_internal(void *arg)
363 {
364         void *journal_info;
365
366         journal_info = current->journal_info;
367         current->journal_info = NULL;
368
369         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
370                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
371                          "%s.%ld.%ld", libcfs_debug_file_path_arr,
372                          get_seconds(), (long_ptr_t)arg);
373                 pr_alert("LustreError: dumping log to %s\n",
374                        debug_file_name);
375                 cfs_tracefile_dump_all_pages(debug_file_name);
376                 libcfs_run_debug_log_upcall(debug_file_name);
377         }
378
379         current->journal_info = journal_info;
380 }
381
382 static int libcfs_debug_dumplog_thread(void *arg)
383 {
384         libcfs_debug_dumplog_internal(arg);
385         wake_up(&debug_ctlwq);
386         return 0;
387 }
388
389 void libcfs_debug_dumplog(void)
390 {
391         wait_queue_t wait;
392         struct task_struct *dumper;
393
394         /* we're being careful to ensure that the kernel thread is
395          * able to set our state to running as it exits before we
396          * get to schedule() */
397         init_waitqueue_entry(&wait, current);
398         set_current_state(TASK_INTERRUPTIBLE);
399         add_wait_queue(&debug_ctlwq, &wait);
400
401         dumper = kthread_run(libcfs_debug_dumplog_thread,
402                              (void *)(long)current_pid(),
403                              "libcfs_debug_dumper");
404         if (IS_ERR(dumper))
405                 pr_err("LustreError: cannot start log dump thread: %ld\n",
406                        PTR_ERR(dumper));
407         else
408                 schedule();
409
410         /* be sure to teardown if cfs_create_thread() failed */
411         remove_wait_queue(&debug_ctlwq, &wait);
412         set_current_state(TASK_RUNNING);
413 }
414 EXPORT_SYMBOL(libcfs_debug_dumplog);
415
416 int libcfs_debug_init(unsigned long bufsize)
417 {
418         int    rc = 0;
419         unsigned int max = libcfs_debug_mb;
420
421         init_waitqueue_head(&debug_ctlwq);
422
423         if (libcfs_console_max_delay <= 0 || /* not set by user or */
424             libcfs_console_min_delay <= 0 || /* set to invalid values */
425             libcfs_console_min_delay >= libcfs_console_max_delay) {
426                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
427                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
428         }
429
430         if (libcfs_debug_file_path != NULL) {
431                 strncpy(libcfs_debug_file_path_arr,
432                         libcfs_debug_file_path, PATH_MAX-1);
433                 libcfs_debug_file_path_arr[PATH_MAX - 1] = '\0';
434         }
435
436         /* If libcfs_debug_mb is set to an invalid value or uninitialized
437          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
438         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
439                 max = TCD_MAX_PAGES;
440         } else {
441                 max = max / num_possible_cpus();
442                 max <<= (20 - PAGE_CACHE_SHIFT);
443         }
444         rc = cfs_tracefile_init(max);
445
446         if (rc == 0)
447                 libcfs_register_panic_notifier();
448
449         return rc;
450 }
451
452 int libcfs_debug_cleanup(void)
453 {
454         libcfs_unregister_panic_notifier();
455         cfs_tracefile_exit();
456         return 0;
457 }
458
459 int libcfs_debug_clear_buffer(void)
460 {
461         cfs_trace_flush_pages();
462         return 0;
463 }
464
465 /* Debug markers, although printed by S_LNET
466  * should not be be marked as such. */
467 #undef DEBUG_SUBSYSTEM
468 #define DEBUG_SUBSYSTEM S_UNDEFINED
469 int libcfs_debug_mark_buffer(const char *text)
470 {
471         CDEBUG(D_TRACE,
472                "***************************************************\n");
473         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
474         CDEBUG(D_TRACE,
475                "***************************************************\n");
476
477         return 0;
478 }
479 #undef DEBUG_SUBSYSTEM
480 #define DEBUG_SUBSYSTEM S_LNET
481
482 void libcfs_debug_set_level(unsigned int debug_level)
483 {
484         pr_warn("Lustre: Setting portals debug level to %08x\n",
485                debug_level);
486         libcfs_debug = debug_level;
487 }
488
489 EXPORT_SYMBOL(libcfs_debug_set_level);